Java 程序只读取一行而不是整个文件 publicstaticvoidmain(字符串ar[])抛出FileNotFoundException{ 扫描=新扫描仪(新文件阅读器(新文件(“files.txt”)); 字符串nextLine=scan.nextLine(); 字符串[]文件名=nextLine.split(\\s+); 对于(int i=0;i
嗯,这是因为您只读取文件中的一行:Java 程序只读取一行而不是整个文件 publicstaticvoidmain(字符串ar[])抛出FileNotFoundException{ 扫描=新扫描仪(新文件阅读器(新文件(“files.txt”)); 字符串nextLine=scan.nextLine(); 字符串[]文件名=nextLine.split(\\s+); 对于(int i=0;i,java,token,filereader,Java,Token,Filereader,嗯,这是因为您只读取文件中的一行: public static void main(String ar[]) throws FileNotFoundException{ scan = new Scanner(new FileReader(new File("files.txt"))); String nextLine = scan.nextLine(); String[] fileNames = nextLine.split("\\s+"); for(i
public static void main(String ar[]) throws FileNotFoundException{
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
String[] fileNames = nextLine.split("\\s+");
for(int i=0;i<fileNames.length;i++){
System.out.println(fileNames[i]);
}
for(int i=0;i<nextLine.length();i++){
char c = nextLine.charAt(i);
boolean isWhiteSpace = isWhiteSpace(c);
if(isWhiteSpace){
if(c == ' ')
System.out.println("white space (blank) at index "+i);
if(c == '\n')
System.out.println("white space (line feed) at index "+i);
if(c == '\r')
System.out.println("white space (\\r - carriage return) at index "+i);
if(c == '\t')
System.out.println("white space (\\t - tab) at index "+i);
}
}
}
要读取整个文件,必须调用scan.nextLine(),直到到达文件结尾。要检查文件结尾,可以执行while循环,从而遍历整个文件:
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
这是因为您只读取文件中的一行:
public static void main(String ar[]) throws FileNotFoundException{
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
String[] fileNames = nextLine.split("\\s+");
for(int i=0;i<fileNames.length;i++){
System.out.println(fileNames[i]);
}
for(int i=0;i<nextLine.length();i++){
char c = nextLine.charAt(i);
boolean isWhiteSpace = isWhiteSpace(c);
if(isWhiteSpace){
if(c == ' ')
System.out.println("white space (blank) at index "+i);
if(c == '\n')
System.out.println("white space (line feed) at index "+i);
if(c == '\r')
System.out.println("white space (\\r - carriage return) at index "+i);
if(c == '\t')
System.out.println("white space (\\t - tab) at index "+i);
}
}
}
要读取整个文件,必须调用scan.nextLine(),直到到达文件结尾。要检查文件结尾,可以执行while循环,从而遍历整个文件:
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
这是因为您只读取文件中的一行:
public static void main(String ar[]) throws FileNotFoundException{
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
String[] fileNames = nextLine.split("\\s+");
for(int i=0;i<fileNames.length;i++){
System.out.println(fileNames[i]);
}
for(int i=0;i<nextLine.length();i++){
char c = nextLine.charAt(i);
boolean isWhiteSpace = isWhiteSpace(c);
if(isWhiteSpace){
if(c == ' ')
System.out.println("white space (blank) at index "+i);
if(c == '\n')
System.out.println("white space (line feed) at index "+i);
if(c == '\r')
System.out.println("white space (\\r - carriage return) at index "+i);
if(c == '\t')
System.out.println("white space (\\t - tab) at index "+i);
}
}
}
要读取整个文件,必须调用scan.nextLine(),直到到达文件结尾。要检查文件结尾,可以执行while循环,从而遍历整个文件:
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
这是因为您只读取文件中的一行:
public static void main(String ar[]) throws FileNotFoundException{
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
String[] fileNames = nextLine.split("\\s+");
for(int i=0;i<fileNames.length;i++){
System.out.println(fileNames[i]);
}
for(int i=0;i<nextLine.length();i++){
char c = nextLine.charAt(i);
boolean isWhiteSpace = isWhiteSpace(c);
if(isWhiteSpace){
if(c == ' ')
System.out.println("white space (blank) at index "+i);
if(c == '\n')
System.out.println("white space (line feed) at index "+i);
if(c == '\r')
System.out.println("white space (\\r - carriage return) at index "+i);
if(c == '\t')
System.out.println("white space (\\t - tab) at index "+i);
}
}
}
要读取整个文件,必须调用scan.nextLine(),直到到达文件结尾。要检查文件结尾,可以执行while循环,从而遍历整个文件:
scan = new Scanner(new FileReader(new File("files.txt")));
String nextLine = scan.nextLine();
您似乎只使用了
String nextLine=scan.nextLine();
一次!您似乎只使用了String nextLine=scan.nextLine();
一次!您似乎只使用了String nextLine=scan.nextLine();
一次!您似乎只使用了String nextLine=scan.nextLine();
一次!字符串nextLine=scan.nextLine()
在我看来,使用扫描器“扫描”只能从您的文件中获取一行。因此,您只能从相关文件中获取一行。String nextLine=scan.nextLine()
在我看来,使用扫描器“扫描”只能从您的文件中获取一行。因此,您只能从相关文件中获取一行。String nextLine=scan.nextLine()
在我看来,使用扫描器“扫描”只能从您的文件中获取一行。因此,您只能从相关文件中获取一行。String nextLine=scan.nextLine()
在我看来,使用扫描器“scan”只接受文件中的一行。因此,您只从相关文件中获得一行。您正在调用方法
nextLine()
只有一次,因此您只能读取程序中的一行。您必须添加for循环以继续读取所有可用的行。您正在调用方法nextLine()
只有一次,因此您只能读取程序中的一行。您必须添加for循环以继续读取所有可用的行。您正在调用方法nextLine()
只有一次,因此您只能读取程序中的一行。您必须添加for循环以继续读取所有可用的行。您正在调用方法nextLine()
只有一次,因此您只能读取程序中的一行。您必须添加for循环以继续读取所有可用的行。为什么您认为它应该读取整个内容?为什么您认为它应该读取整个内容?为什么您认为它应该读取整个内容?为什么您认为它应该读取整个内容?为什么您认为它应该读取整个内容?我同意“我没有试过,但它只是进入了一个永无止境的While循环。@user1769881您确定键入的是正确的吗?这不应该导致无限循环……我只是试过,但它只是进入了永无止境的While循环。@user1769881您确定键入的是正确的吗?这不应该导致无限循环……我刚刚尝试过,b。”但它只是进入了一个永无止境的While循环。@user1769881您确定键入的是正确的吗?这不应该导致无限循环…我只是尝试了一下,但它只是进入了一个永无止境的While循环。@user1769881您确定键入的是正确的吗?这不应该导致无限循环。。。