用DataInputStream从文件中读取非常慢 [英] Reading from file with DataInputStream is very slow
问题描述
我试图用下面的代码从文件中读取它,但是它超慢任何人都可以帮助减少时间?
以下是我的代码以非常缓慢的方式读取它:
import java.io.BufferedInputStream;
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 FileInput {
public static void main(String [] args){
Scanner scan1 = new Scanner(System.in);
String filename = scan1.nextLine();
档案档案=新档案(档名);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
尝试{
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis); (dis.available()!= 0)
{
System.out.println(dis.readLine());
}
fis.close();
bis.close();
dis.close();
$ b $ catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
$ div class =h2_lin>解决方案
请勿使用 DataInputStream
读取文件中的行。而是使用 BufferedReader
,如下所示:
fis = new FileInputStream (文件);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); ((String line = reader.readLine())!= null){
System.out.println(line);
$ b $ DataInputStream.readLine
告诉你不要使用该方法。 (它已被弃用)
当然,当你真正开始阅读数字时,我会鼓励你忘记阅读自己的文章,只要让扫描仪
为您读取数字。如果您需要知道同一行上有哪些数字, Scanner
也可以为您做到这一点:
<$ p $扫描仪fileScanner =新扫描仪(文件,UTF-8)。useDelimiter(+ | *(?= \\\\
)|(?<= \\\\
) *);
while(fileScanner.hasNext()){
List< Integer> numbersOnLine = new ArrayList< Integer>(); (fileScanner.hasNextInt()){
numbersOnLine.add(fileScanner.nextInt());
}
processLineOfNumbers(numbersOnLine);
if(fileScanner.hasNext()){
fileScanner.next(); //清除换行符
}
}
换行符之间的换行符也显示为 Scanner
的标记。
I have got a file containing a large amount of numbers.
I have tried to use the following code to read it from the file, but it is super slow anyone can help to reduce the time?
Following is my code to read it in a very slow way:
import java.io.BufferedInputStream;
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 FileInput {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
String filename = scan1.nextLine();
File file = new File(filename);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Don't use a DataInputStream
to read lines from a file. Instead, use a BufferedReader
, as in:
fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
while ((String line = reader.readLine()) != null) {
System.out.println(line);
}
The javadoc on DataInputStream.readLine
tells you to not use that method. (it's been deprecated)
Of course, when you actually get around to reading the numbers, I'd encourage you to forget reading the lines yourself, and just let Scanner
read the numbers for you. If you need to know which numbers were on the same line, Scanner
can do that for you too:
Scanner fileScanner = new Scanner(file, "UTF-8").useDelimiter(" +| *(?=\\n)|(?<=\\n) *");
while (fileScanner.hasNext()) {
List<Integer> numbersOnLine = new ArrayList<Integer>();
while (fileScanner.hasNextInt()) {
numbersOnLine.add(fileScanner.nextInt());
}
processLineOfNumbers(numbersOnLine);
if (fileScanner.hasNext()) {
fileScanner.next(); // clear the newline
}
}
That fancy regex makes it so that the newline characters between lines also show up as tokens to the Scanner
.
这篇关于用DataInputStream从文件中读取非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!