This is the content in the file.
1, 2, 3, 4, 5, 6, 7, 8
8, 7, 6, 5, 4, 3, 2, 1
One, two, three, four, five ,,,,
5, 4, 3, 2, 1 ,,,,
The Code starts.
Public static string totalfilesrcpath = "D:/file /";
Public static string enterfilesrcpath = "D:/file/file1 ";
Public static arraylist <filedata> filereq = new arraylist <filedata> ();
/**
* Reading files in behavior units
*/
Public static void readfilebylines (){
File [] ftpfiles = new file (totalfilesrcpath). listfiles (); // all files in this directory
If (ftpfiles! = NULL & ftpfiles. length> 0 ){
For (File ftpfile: ftpfiles ){
String yfilename = ftpfile. getname ();
If (yfilename. Equals ("file ")){
Bufferedreader reader = NULL;
Try {
System. Out. println ("\ n reads the file content in the unit of action, and reads the entire row at a time :");
Reader = new bufferedreader (New filereader (ftpfile ));
String tempstring = NULL; // assign the file read from each row to the temporary variable.
Filedata DATA = NULL;
// Read a row at a time until null is the end of the file.
While (tempstring = reader. Readline ())! = NULL ){
// Separate and remove commas (,).
String [] cs = tempstring. Split (",");
Data = new filedata ();
// Assign one element to an object
Data. settest1 (CS [0]);
Data. settest2 (CS [1]);
Data. settest3 (CS [2]);
Data. settest4 (CS [3]);
Data. settest5 (CS [4]);
Data. settest6 (CS [5]);
Data. settest7 (CS [6]);
Data. settest8 (CS [7]);
Filereq. Add (data );
}
Reader. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (reader! = NULL ){
Try {
Reader. Close ();
} Catch (ioexception E1 ){
E1.printstacktrace ();
}
}
}
}
}
}
}
// Test
Public class testreadfile {
Public static void main (string [] ARGs ){
// Read files by row
Readfromfile. readfilebylines ();
}
}