I'm having some issues trying to create a thread constructor that will accept a file name and a 2-d array of data to be written to the file. The thread run method is suppose to write that 2-d array to the file and then instantiate this thread for the courses.txt file and the students.txt file. I'm trying to do this by using multithreaded application. The 2D array will be a 2D String array, as I already created on the code. I've hard-code the data into the 2D String array but I am a bit confused as to how I'll pass that array and filename to a thread that will write the file. I know I can write one thread class with one run() method that will write either file. How do I create two instances of that thread in the main() in WriteFiles.java and then pass one the Student data and pass the other the Course data?
This is the code I have so far and would love some guidance:
import java.util.*;
import java.io.*;
class WriteFiles implements Runnable
{
static final String FILE = "courses.txt";
static final String FILE2 = "students.txt";
BufferedWriter bw;
Thread thread[] = new Thread[2];
private String studentList[][];
private String courseList[][];
public WriteFiles()
{
try
{
bw = new BufferedWriter( new FileWriter( FILE ) );
bw = new BufferedWriter( new FileWriter( FILE2 ) );
}
catch( FileNotFoundException fnfe )
{
System.out.println( fnfe );
System.exit( 1 );
}
catch( IOException ioe )
{
System.out.println( ioe );
System.exit( 2 );
}
}
public void writeFile( String str )
{
try{
bw.write( str );
} catch( IOException ioe ){
System.out.println( ioe );
}
}
public void run()
{
String studentList[][] = {
{"Python","INSY 3300","530-650 MW","1,3"},
{"Networking","INSY 3303","530-650 TR","1,3"},
{"DBMS","INSY 3304","900-950 MWF","1,3"},
{"Analysis&Design","INSY 3305","700-820 TR","1,3"},
{"Java I","INSY 4305","700-820 TR","1,3"},
{"Java II","INSY 4306","530-650 TR","1,3"},
{"Mobile App","INSY 4308","200-320 TR","1,3"},
{"Web Development","INSY 4315","1000-1050 MWF","1,3"},
{"Resource Management","INSY 4325","100-220 TR","1,3"}
};
String courseList[][] = {
{"US","NONRESIDENT","123456","Jones","123 Cooper St","Arlington","Texas","76019","12345"},
{"INT","ACTIVE","A-654789","Degrassey","18 Love Lane","Dallas","Texas","75052","67123"},
{"INT","INACTIVE","A-543891","Franco","1201 Trail Road","Euless","Texas","74032","19814"},
{"US","RESIDENT","345123","Hughes","1803 Division","Fort Worth","Texas","76034","674532"},
{"US","RESIDENT","988776","Cooper","111 Marsh Lane","Bedford","Texas","76111","90906"},
{"INT","INACTIVE","B-577463","Patel","2218 Border St","Arlington","Texas","76015","81832"}
};
}
public void writeCourses()
{
for (Student s:studentList){
System.out.print(s.toString());
}
}
public void writeStudents()
{
for (Course c:courseList){
System.out.print(c.toString());
}
}
public static void main( String arg[] )
{
WriteFiles myTread = new WriteFiles();
myTread.writeCourses();
myTread.writeStudents();
}
}
The final students.txt output is suppose to look like this with a ; split per data:
US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;76019;12345 INT;ACTIVE;A-654789;Degrassey;18 Love Lane;Dallas;Texas;75052;67123 INT;INACTIVE;A-543891;Franco;1201 Trail Road;Euless;Texas;74032;19814 US;RESIDENT;345123;Hughes;1803 Division;Fort Worth;Texas;76034;674532 US;RESIDENT;988776;Cooper;111 Marsh Lane;Bedford;Texas;76111;90906 INT;INACTIVE;B-577463;Patel;2218 Border St;Arlington;Texas;76015;81832
The final course.txt output is suppose to look like this with a ; split per data:
Python;INSY 3300;530-650 MW;1;3 Networking;INSY 3303;530-650 TR;1;3 DBMS;INSY 3304;900-950 MWF;1;3 Analysis&Design;INSY 3305;700-820 TR;1;3 Java I;INSY 4305;700-820 TR;1;3 Java II;INSY 4306;530-650 TR;1;3 Mobile App;INSY 4308;200-320 TR;1;3 Web Development;INSY 4315;1000-1050 MWF;1;3 Resource Management;INSY 4325;100-220 TR;1;3
I had no problem getting this to work a different method and was successful, but I am now trying a different approach using a multithreaded application. Here was a working output without threads:
import java.util.*;
import java.io.*;
public class WriteFiles {
private static Formatter output;
private static Scanner input;
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<Student>();
writeTextFile1();
for (Student s:studentList){
System.out.print(s.toString());
}
ArrayList<Course> courseList = new ArrayList<Course>();
writeTextFile();
for (Course c:courseList){
System.out.print(c.toString());
}
}
public static void writeTextFile(){
try{
output = new Formatter("courses.txt");
output.format("%s;%s;%s;%d;%d%n","Python","INSY 3300","530-650 MW",1,3);
output.format("%s;%s;%s;%d;%d%n","Networking","INSY 3303","530-650 TR",1,3);
output.format("%s;%s;%s;%d;%d%n","DBMS","INSY 3304","900-950 MWF",1,3);
output.format("%s;%s;%s;%d;%d%n","Analysis&Design","INSY 3305","700-820 TR",1,3);
output.format("%s;%s;%s;%d;%d%n","Java I","INSY 4305","700-820 TR",1,3);
output.format("%s;%s;%s;%d;%d%n","Java II","INSY 4306","530-650 TR",1,3);
output.format("%s;%s;%s;%d;%d%n","Mobile App","INSY 4308","200-320 TR",1,3);
output.format("%s;%s;%s;%d;%d%n","Web Development","INSY 4315","1000-1050 MWF",1,3);
output.format("%s;%s;%s;%d;%d%n","Resource Management","INSY 4325","100-220 TR",1,3);
output.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void writeTextFile1(){
try{
output = new Formatter("students.txt");
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","US","NONRESIDENT","123456","Jones","123 Cooper St","Arlington","Texas",76019,12345);
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","INT","ACTIVE","A-654789","Degrassey","18 Love Lane","Dallas","Texas",75052,67123);
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","INT","INACTIVE","A-543891","Franco","1201 Trail Road","Euless","Texas",74032,19814);
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","US","RESIDENT","345123","Hughes","1803 Division","Fort Worth","Texas",76034,674532);
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","US","RESIDENT","988776","Cooper","111 Marsh Lane","Bedford","Texas",76111,90906);
output.format("%s;%s;%s;%s;%s;%s;%s;%d;%d%n","INT","INACTIVE","B-577463","Patel","2218 Border St","Arlington","Texas",76015,81832);
output.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}