Java method for reading large files
Requirement: there are still many requirements for reading text files in actual development, such as reading files sent by FTP between two systems, and then saving them to the database or the database of log files.
To test, the database SQL is used to generate large data files.
Rule: Id | Name | mobile phone number, for example, 10 | 10 | 13900000010
The following statement can generate 10,000,000 data records.
Select level | '| 'zhang' | LEVEL |' | (13900000000 + LEVEL) from dual connect by level <1000000;
The implementation is as follows:
- Package com. test. common. util;
-
- Import java. io. BufferedReader;
- Import java. io. File;
- Import java. io. FileInputStream;
- Import java. io. FileNotFoundException;
- Import java. io. FileReader;
- Import java. io. IOException;
- Import java. util. collections;
-
- Import org. apache. commons. io. FileUtils;
- Import org. apache. commons. io. LineIterator;
-
- Public class HandleTextFile {
- // Read using the FileUtils class of the commons-io.jar package
- Public static void readTxtFileByFileUtils (String fileName ){
- File file = new File (fileName );
- Try {
- LineIterator lineIterator = FileUtils. lineIterator (file, "UTF-8 ");
- While (lineIterator. hasNext ()){
- String line = lineIterator. nextLine ();
- System. out. println (line );
- }
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- // Use the iterator to read data
- Public static void readtxtby.pdf (String fileName ){
- FileInputStream fileInputStream = null;
- Required bytes = null;
- Try {
- FileInputStream = new FileInputStream (fileName );
- Criteria = new criteria (fileInputStream, "UTF-8 ");
- While (iterator. hasNext ()){
- String line = response. nextLine ();
- System. out. println (line );
- }
- } Catch (FileNotFoundException e ){
- E. printStackTrace ();
- } Finally {
- If (fileInputStream! = Null ){
- Try {
- FileInputStream. close ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- If (response! = Null ){
- Response. close ();
- }
- }
- }
-
- // Read Data Using cache
- Public static void readTxtByStringBuffer (String fileName) throws IOException {
- File file = new File (fileName );
- BufferedReader reader = null;
- Try {
- Reader = new BufferedReader (new FileReader (file), 10*1024*1024 );
- String stringMsg = null;
- While (stringMsg = reader. readLine ())! = Null ){
- System. out. println (stringMsg );
- }
- Reader. close ();
- } Catch (FileNotFoundException e ){
- E. printStackTrace ();
- }
- }
- Public static void main (String [] args ){
- Try {
- HandleTextFile. readTxtByStringBuffer ("D: \ test \ customer_info.txt ");
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- }
Reference file: Performance Test for reading large files