如何在java中逐行读取文件中的输入? [重复]

时间:2022-08-08 20:44:08

This question already has an answer here:

这个问题在这里已有答案:

Ok, I know this is a really rookie question, but I looked around online a lot and am yet unable to find the answer to my question:

好吧,我知道这是一个非常新的问题,但我在网上看了很多,但我还是找不到问题的答案:

How can I read input from a file line by line in java?

如何在java中逐行读取文件中的输入?

Assume a file input that has integers on each line, like:

假设每行都有整数的文件输入,如:

1
2
3
4
5

Here's the snippet of code I am trying to work with:

这是我尝试使用的代码片段:

public static void main(File fromFile) {

  BufferedReader reader = new BufferedReader(new FileReader(fromFile));

  int x, y;

  //initialize
  x = Integer.parseInt(reader.readLine().trim());
  y = Integer.parseInt(reader.readLine().trim());

}

Presumably, this would read in the first two lines and store them as integers in x and y. So going off the example, x=1, y=2.

据推测,这将读取前两行并将它们存储为x和y中的整数。因此,在示例中,x = 1,y = 2。

It is finding a problem with this, and I don't know why.

它发现了这个问题,我不知道为什么。

3 个解决方案

#1


2  

 public static void main(String[] args) {
        FileInputStream fstream;
        DataInputStream in = null;
        try {
            // Open the file that is the first 
            // command line parameter
            fstream = new FileInputStream("textfile.txt");
            // Get the object of DataInputStream
            in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int x = Integer.parseInt(br.readLine());
            int y = Integer.parseInt(br.readLine());

            //Close the input stream

        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

#2


2  

please check your main method(). It should be like these

请检查您的主要方法()。它应该是这样的

public static void main(String... args) {
}

or

public static void main(String[] args) {
}

then read like that :

那样读:

 BufferedReader reader = new BufferedReader(new FileReader(fromFile));

 String line;
 while( (line = reader.readLine()) != null){
        int i = Integer.parseInt(line);
 }

#3


0  

We usually use a while loop, the readLine method tells whether the end of file is reached or not:

我们通常使用while循环,readLine方法告诉是否到达文件末尾:

 List<String> lines = new ArrayList<String>();
 while ((String line = reader.readLine()) != null)
   lines.add(line);

Now we have a collection (a list) that holds all lines from the file as separate strings.

现在我们有一个集合(列表),它将文件中的所有行保存为单独的字符串。


To read the content as Integers, just define a collection of integers and parse while reading:

要将内容作为整数读取,只需定义一个整数集合并在阅读时解析:

 List<Integer> lines = new ArrayList<Integer>();
 while ((String line = reader.readLine()) != null) {
   try {
     lines.add(Integer.parseInt(line.trim()));
   } catch (NumberFormatException eaten) {
     // this happens if the content is not parseable (empty line, text, ..)
     // we simply ignore those lines
   }
 }

#1


2  

 public static void main(String[] args) {
        FileInputStream fstream;
        DataInputStream in = null;
        try {
            // Open the file that is the first 
            // command line parameter
            fstream = new FileInputStream("textfile.txt");
            // Get the object of DataInputStream
            in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int x = Integer.parseInt(br.readLine());
            int y = Integer.parseInt(br.readLine());

            //Close the input stream

        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

#2


2  

please check your main method(). It should be like these

请检查您的主要方法()。它应该是这样的

public static void main(String... args) {
}

or

public static void main(String[] args) {
}

then read like that :

那样读:

 BufferedReader reader = new BufferedReader(new FileReader(fromFile));

 String line;
 while( (line = reader.readLine()) != null){
        int i = Integer.parseInt(line);
 }

#3


0  

We usually use a while loop, the readLine method tells whether the end of file is reached or not:

我们通常使用while循环,readLine方法告诉是否到达文件末尾:

 List<String> lines = new ArrayList<String>();
 while ((String line = reader.readLine()) != null)
   lines.add(line);

Now we have a collection (a list) that holds all lines from the file as separate strings.

现在我们有一个集合(列表),它将文件中的所有行保存为单独的字符串。


To read the content as Integers, just define a collection of integers and parse while reading:

要将内容作为整数读取,只需定义一个整数集合并在阅读时解析:

 List<Integer> lines = new ArrayList<Integer>();
 while ((String line = reader.readLine()) != null) {
   try {
     lines.add(Integer.parseInt(line.trim()));
   } catch (NumberFormatException eaten) {
     // this happens if the content is not parseable (empty line, text, ..)
     // we simply ignore those lines
   }
 }