Java 从带有DataInputStream的文件读取非常慢

Java 从带有DataInputStream的文件读取非常慢,java,file-io,Java,File Io,我有一个包含大量数字的文件 我曾尝试使用以下代码从文件中读取它,但它非常慢,任何人都可以帮助减少时间吗 以下是我以非常缓慢的方式读取它的代码: import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOEx

我有一个包含大量数字的文件

我曾尝试使用以下代码从文件中读取它,但它非常慢,任何人都可以帮助减少时间吗

以下是我以非常缓慢的方式读取它的代码:

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

不要使用
DataInputStream
从文件中读取行。相反,请使用
BufferedReader
,如中所示:

fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
while ((String line = reader.readLine()) != null) {
  System.out.println(line);
}
DataInputStream.readLine上的javadoc告诉您不要使用该方法。(它已被弃用)

当然,当你真正开始阅读数字时,我建议你忘记自己读这些行,让
Scanner
为你阅读数字。如果您需要知道哪些号码在同一行上,
Scanner
也可以为您这样做:

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
  }
}

Scanner fileScanner=new Scanner(文件,“UTF-8”)。使用delimiter(“+|*(?=\\n)|”(?它在我的机器上运行得更快,println被注释掉了。写入屏幕会大大降低速度。这不仅仅是java的事情……在C/C++和我使用过的所有其他语言中都会发生。

导入java.io.FileNotFoundException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class file {
public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    String fname = "";
    System.out.print("File Name: ");
    fname = keyboard.next();

    try{
        Scanner file1 = new Scanner(new FileReader(fname));
        System.out.println("File Open Successful");
        int length = file1.nextInt();
        String[] content = new String[length];
        for (int i=0;i<length;i++){
            content[i] = file1.next();
        }
        for (int i=0;i<length;i++){
            System.out.println("["+i+"] "+content[i]);
        }
        System.out.println("End of file.");

    } catch (FileNotFoundException e){
        System.out.println("File Not Found!");
    }   


}
导入java.io.FileReader; 导入java.util.Scanner; 公共类文件{ 公共静态void main(字符串[]args){ 扫描仪键盘=新扫描仪(System.in); 字符串fname=“”; System.out.print(“文件名:”); fname=keyboard.next(); 试一试{ Scanner file1=新扫描仪(新文件读取器(fname)); System.out.println(“文件打开成功”); int length=file1.nextInt(); 字符串[]内容=新字符串[长度];
对于(int i=0;i可能是因为数字正在打印到屏幕上?请注释该行以供测试。虽然我尝试不打印它们,但时间仍然太长。还有,您打算如何处理这些数字?这会改变编码策略。这是真的,但我不明白为什么这会使应用程序失败低。该方法被弃用的原因是它不能正确地进行字节到字符的转换。谢谢你的回答,但是我对扫描仪代码有点困惑,你能给我看一下使用扫描仪读取文件中数字的全部代码吗?我很困惑-我不知道你还想让我给你看什么;那代码是读取文件中的数字,将其解释为
整数
s,然后将一行数字一次传递给您编写的某个函数。除此之外,只需创建您已经创建的
文件
对象,然后使用
整数
s执行任何您想要的操作。如果您不关心什么数字字符在同一行,不要调用
useDelimiter
,只要继续调用
nextInt
,而
hasNextInt
是真的。很抱歉,我的心思在别处,谢谢你的帮助,完成整个文件不像32ms((String line=reader.readLine())!=null)为我抛出了一个错误,似乎你不能像这样分配行的值,感谢你的任何想法虽然我试图不打印出来,但时间仍然太长2MB文件需要多长时间?我将读卡器更改为bufferedreader,现在可以在32msI中完成,我认为可以去掉可用的()测试做到了这一点。BufferedReader很可能会使它变慢。避免只发布代码,始终尝试解释和详细说明您的anwserOk。我将在以后的帖子中尝试这样做。谢谢。