java - Reading from file with DataInputStream is very slow -


I got a large number of files.

I have tried to use the following to read the code from the file, but can it help to reduce time in a very slow way?

My very slow way to read the following code is:

  Import java.io.buffferedInputStream; Import java.io.DataInputStream; Import java.io.file; Import java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.IOException; Import java.util. *; Public class file input {public static zero main (string [] args) {scanner scan 1 = new scanner (system.); String filename = scan1 NXtine (); File file = new file (file name); FileInputStream fis = null; Buffardinputstream bis = null; DataInputStream dis = null; {Fis = Try New FileInputStream (File); BIS = new buffer inputstream (FIS); Dis = new data inputstream (BIS); While (dis.available ()! = 0) {System.out.println (dis.readLine ()); } Fis.close (); Bis.close (); Dis.close (); } Hold (FileNotFoundException e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); }}}}  

Do not use a DataInputStream Instead, read lines from a file, instead, use a BufferedReader :

  fis = new FileInputStream (file); BufferedReader Reader = new BufferedReader (new InputStreamReader (fis)); While ((string line = reader. Readline ()) = tap) {System.out.println (line); }  

DataInputStream.readLine javadoc asks you not to use that method. (It has been disliked)

Of course, when you actually move around to read the numbers, then I encourage you to forget to read yourself, and simply Scanner Read the numbers for you If you need to know which numbers are in the same row, then scanner can also do this for you:

  scanner file scanner = new scanner (file, "UTF-8") .useDelimiter ("+ | * (? = \\ n) | (? & Lt; = \ \ N) * "); While (fileScanner.hasNext ()) {list & lt; Integer & gt; NumberOnline = new Array's & lt; Integer & gt; (); While (fileScanner.hasNextInt ()) {numbersOnLine.add (fileScanner.nextInt ()); } ProcessLineOfNumbers (numberOnline); If (fileScanner.hasNext ()) {fileScanner.next (); // clear the new line}  

This fancy rezux makes it that the new characters between the lines also appear in the form of a token of scanner .


Comments