reading input till EOF in java

2019-01-23 09:52发布

问题:

In C++ if I wish to read input till the EOF I can do it in the following manner

while(scanf("%d",&n))
{
    A[i]=n;
    i++;
}

I can then run this code as ./a.out < input.txt. What is the java equivalent of this code?

回答1:

You can do this:

Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
    A[i] = s.nextInt();
    i++;
}


回答2:

// assuming that reader is an instance of java.io.BufferedReader
String line = null;
while ((line = reader.readLine()) != null) {
    // do something with every line, one at a time
}

Let me know if you run into difficulties.



回答3:

 import java.io.BufferedReader;
 import java.io.FileReader;

BufferedReader br = null;  
     br = new BufferedReader(new FileReader(file));
       while ((line = br.readLine()) != null) {              

     }

    //using Scanner class

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      System.out.println(line);
   }


回答4:

Here is Java equivalent code using BufferedReader and FileReader classes.

  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;

  public class SmallFileReader {
        public static void main(String[] args) throws IOException {
             BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
             String line=nul;
             while( (line=br.readLine()) != null) {
                    System.out.println(line);  
             }
  }
}


回答5:

Here is Java equivalent code using BufferedReader and FileReader classes.

  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;

  public class SmallFileReader {
        public static void main(String[] args) throws IOException {  

Option 1:
String fileName = args[0];
BufferedReader br = new BufferedReader(new FileReader(fileName));
Option 2:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a file name: ");
String fileName = br.readLine();

             //BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
             String line=null;
             while( (line=br.readLine()) != null) {
                    System.out.println(line);  
             }
  }
}  

I made little modification to @Vallabh Code. @tom You can use the first option, if you want to input the file name through command line.
java SmallFileReader Hello.txt
Option 2 will ask you the file name when you run the file.



回答6:

The only thing that really works for me (you don't even have to create a file)

Scanner read = new Scanner(System.in);
String cadena;
boolean cond = true;
int i =0;
while (cond){
    cadena = read.nextLine();
    if(cadena.isEmpty())
        cond = false;
}


回答7:

A simple solution would be to use Scanner class.

See snipet below:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        while(s.hasNextLine())
        {
            String line = s.nextLine();
            System.out.println(line);
        }
    }
}


标签: java file-io eof