Java method for reading large files

Source: Internet
Author: User

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:

 
 
  1. Package com. test. common. util;
  2.  
  3. Import java. io. BufferedReader;
  4. Import java. io. File;
  5. Import java. io. FileInputStream;
  6. Import java. io. FileNotFoundException;
  7. Import java. io. FileReader;
  8. Import java. io. IOException;
  9. Import java. util. collections;
  10.  
  11. Import org. apache. commons. io. FileUtils;
  12. Import org. apache. commons. io. LineIterator;
  13.  
  14. Public class HandleTextFile {
  15. // Read using the FileUtils class of the commons-io.jar package
  16. Public static void readTxtFileByFileUtils (String fileName ){
  17. File file = new File (fileName );
  18. Try {
  19. LineIterator lineIterator = FileUtils. lineIterator (file, "UTF-8 ");
  20. While (lineIterator. hasNext ()){
  21. String line = lineIterator. nextLine ();
  22. System. out. println (line );
  23. }
  24. } Catch (IOException e ){
  25. E. printStackTrace ();
  26. }
  27. }
  28. // Use the iterator to read data
  29. Public static void readtxtby.pdf (String fileName ){
  30. FileInputStream fileInputStream = null;
  31. Required bytes = null;
  32. Try {
  33. FileInputStream = new FileInputStream (fileName );
  34. Criteria = new criteria (fileInputStream, "UTF-8 ");
  35. While (iterator. hasNext ()){
  36. String line = response. nextLine ();
  37. System. out. println (line );
  38. }
  39. } Catch (FileNotFoundException e ){
  40. E. printStackTrace ();
  41. } Finally {
  42. If (fileInputStream! = Null ){
  43. Try {
  44. FileInputStream. close ();
  45. } Catch (IOException e ){
  46. E. printStackTrace ();
  47. }
  48. }
  49. If (response! = Null ){
  50. Response. close ();
  51. }
  52. }
  53. }
  54.  
  55. // Read Data Using cache
  56. Public static void readTxtByStringBuffer (String fileName) throws IOException {
  57. File file = new File (fileName );
  58. BufferedReader reader = null;
  59. Try {
  60. Reader = new BufferedReader (new FileReader (file), 10*1024*1024 );
  61. String stringMsg = null;
  62. While (stringMsg = reader. readLine ())! = Null ){
  63. System. out. println (stringMsg );
  64. }
  65. Reader. close ();
  66. } Catch (FileNotFoundException e ){
  67. E. printStackTrace ();
  68. }
  69. }
  70. Public static void main (String [] args ){
  71. Try {
  72. HandleTextFile. readTxtByStringBuffer ("D: \ test \ customer_info.txt ");
  73. } Catch (IOException e ){
  74. E. printStackTrace ();
  75. }
  76. }
  77. }

Reference file: Performance Test for reading large files

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.