Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中读取输入直到EOF 在C++中,如果我想读取输入,直到EOF,我可以用下面的方式< /P> while(scanf("%d",&n)) { A[i]=n; i++; }_Java_File Io_Eof - Fatal编程技术网

在java中读取输入直到EOF 在C++中,如果我想读取输入,直到EOF,我可以用下面的方式< /P> while(scanf("%d",&n)) { A[i]=n; i++; }

在java中读取输入直到EOF 在C++中,如果我想读取输入,直到EOF,我可以用下面的方式< /P> while(scanf("%d",&n)) { A[i]=n; i++; },java,file-io,eof,Java,File Io,Eof,然后,我可以将此代码作为./a.out

然后,我可以将此代码作为./a.out您可以这样做:

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

如果遇到困难,请告诉我。

以下是使用BufferedReader和FileReader类的Java等效代码

  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);  
             }
  }
}
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;

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

下面是使用BufferedReader和FileReader类的Java等效代码

  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);  
             }
  }
}
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;

  public class SmallFileReader {
        public static void main(String[] args) throws IOException {  
选项1:
String fileName=args[0]
BufferedReader br=new BufferedReader(new FileReader(fileName))
选项2:
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in))
System.out.println(“输入文件名:”)
String fileName=br.readLine()

             //BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
             String line=null;
             while( (line=br.readLine()) != null) {
                    System.out.println(line);  
             }
  }
}  
我对@Vallabh代码做了一些修改。 @如果要通过命令行输入文件名,可以使用第一个选项。
java SmallFileReader Hello.txt


选项2将在运行文件时询问文件名

唯一适合我的东西(你甚至不必创建文件)


一个简单的解决方案是使用
Scanner
class

见下文:

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等效代码,用于在文件结束前读取输入:

import java.util.Scanner;

public class EndOfFileSolutions {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for(int i = 1; sc.hasNext()== true; i++){
            System.out.println(i + " " + sc.nextLine());
        }
    }
}
此代码将生成如下所示的输出--

样本输入: 样本输出:
这个答案同样适用于Java中的

,一种直到EOF的阅读方法是:-

     Scanner scan = new Scanner(System.in);    
     while(scan.hasNextLine())   //read until condition is true
     {
        String line = scan.nextLine();
        System.out.println(line);
        i++;
     } 

EOF或线的末端。。!可以在单while循环条件下实现。。!!,在条件有效之前,循环将运行并从用户处获取输入。“.hasNextLine”正在检查是否有其他输入行。例如,我正在使用Hackerrank实践。 希望这对你有帮助

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

public class Solution {

    public static void main(String[] args) {
        int i=1;
        Scanner sc = new Scanner(System.in); // Making Scanner object
        while(sc.hasNextLine()){            // This is the main condition for EOF  
            String line = sc.nextLine();
            System.out.format("%d %s\n",i,line);
            i++;
        } 
    
      
    }
}

试图用你的标题搜索?搜索
Scanner
BufferedReader
类。@GrijeshChauhan-再次尝试阅读该问题。@Perception抱歉,我搞错了问题是我不知道中的文件名advance@tom你也可以在java中输入命令行,因为C或C++中发生了。如果我的文件包含了<代码> null <代码>会发生什么?一串我无法读取完整文件,这可能是一个原因吗?@I-droid-a
Scanner
根据分隔符字符(默认为空白)的出现情况将输入划分为标记来处理输入。调用时,它希望下一个标记与整数正则表达式匹配;如果没有,该方法将抛出异常。因此,根据您使用
扫描仪的方式,文件中的“null”很可能是一个问题。
1 Hello world
2 I am a file
3 Read me until end-of-file.
     Scanner scan = new Scanner(System.in);    
     while(scan.hasNextLine())   //read until condition is true
     {
        String line = scan.nextLine();
        System.out.println(line);
        i++;
     } 
Scanner scanner = new Scanner(System.in);
int c = 0;
while(scanner.hasNext()){
  System.out.println(++c + " " + scanner.nextLine());
}
scanner.close();
// use while instead of normal for loop. 
// If you need to read a file than BufferReader is the best way to do it, as explained above.
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        int i=1;
        Scanner sc = new Scanner(System.in); // Making Scanner object
        while(sc.hasNextLine()){            // This is the main condition for EOF  
            String line = sc.nextLine();
            System.out.format("%d %s\n",i,line);
            i++;
        } 
    
      
    }
}