Parse Files in relative paths in Java [read files by row]

Source: Internet
Author: User

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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.