很奇怪文件读取输出Java
Very strange File reading output Java
因此,我从文本文件中读取单词并将它们保存在 ArrayList 的 ArrayList 中。它应该按照文件中的原样打印单词。例如:
test1 test2 test3 test4 test5
test6 test7 test8
test9 test10
但是它打印出这个:Actual output here
为什么它会这样以及如何解决它?
下面是阅读代码:
package com.company;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
public class WordOrder {
public ArrayList<ArrayList<String>> LinesList;
public ArrayList<String> Words_per_line_list;
protected String FileName;
protected File file;
public WordOrder(){
LinesList = new ArrayList<>();
Words_per_line_list = new ArrayList<>();
}
public void wordReading() throws IOException, IndexOutOfBoundsException{
String word_to_be_read;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter the name of the file");
FileName = scan.nextLine ();
file = new File(FileName);
BufferedReader in = new BufferedReader(new FileReader (FileName));
if(in.read () == -1){
throw new IOException ("File does not exist or cannot be accessed");
}
System.out.println ("Test");
int i =0, j = 0;
while(in.readLine() != null) {
LinesList.add(i, Words_per_line_list);
while ((in.read ()) != -1) {
word_to_be_read = in.readLine ();
Words_per_line_list.add(j, word_to_be_read);
System.out.println (LinesList.get (i).get (j));
j++;
}
i++;
}
}
如有任何帮助,我们将不胜感激。
while 语句正在读取数据,但您没有对该数据执行任何操作..
第一个 while(in.readLine() != null) {
将从您的文件中读取第一行
即测试 1 测试 2 测试 3 测试 4 测试 5
但你什么都不做。
第二个while ((in.read ()) != -1) {
将从文件中读取单个字符。因此 t
离开 est6 test7 test8
由 word_to_be_read = in.readLine ();
读取,然后再次从下一行读取 t
,将 est9 test10
留给下一个 readline
您可以在外部 while 中将该行读入变量,然后在 while 循环内处理该行。
String line;
while((line = in.readLine()) != null) {
// process the line however you need to
System.out.println(line);
}
因此,我从文本文件中读取单词并将它们保存在 ArrayList 的 ArrayList 中。它应该按照文件中的原样打印单词。例如:
test1 test2 test3 test4 test5
test6 test7 test8
test9 test10
但是它打印出这个:Actual output here 为什么它会这样以及如何解决它? 下面是阅读代码:
package com.company;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
public class WordOrder {
public ArrayList<ArrayList<String>> LinesList;
public ArrayList<String> Words_per_line_list;
protected String FileName;
protected File file;
public WordOrder(){
LinesList = new ArrayList<>();
Words_per_line_list = new ArrayList<>();
}
public void wordReading() throws IOException, IndexOutOfBoundsException{
String word_to_be_read;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter the name of the file");
FileName = scan.nextLine ();
file = new File(FileName);
BufferedReader in = new BufferedReader(new FileReader (FileName));
if(in.read () == -1){
throw new IOException ("File does not exist or cannot be accessed");
}
System.out.println ("Test");
int i =0, j = 0;
while(in.readLine() != null) {
LinesList.add(i, Words_per_line_list);
while ((in.read ()) != -1) {
word_to_be_read = in.readLine ();
Words_per_line_list.add(j, word_to_be_read);
System.out.println (LinesList.get (i).get (j));
j++;
}
i++;
}
}
如有任何帮助,我们将不胜感激。
while 语句正在读取数据,但您没有对该数据执行任何操作..
第一个 while(in.readLine() != null) {
将从您的文件中读取第一行
即测试 1 测试 2 测试 3 测试 4 测试 5
但你什么都不做。
第二个while ((in.read ()) != -1) {
将从文件中读取单个字符。因此 t
离开 est6 test7 test8
由 word_to_be_read = in.readLine ();
读取,然后再次从下一行读取 t
,将 est9 test10
留给下一个 readline
您可以在外部 while 中将该行读入变量,然后在 while 循环内处理该行。
String line;
while((line = in.readLine()) != null) {
// process the line however you need to
System.out.println(line);
}