[英]How to print a full line of text in java (from input file)
I'm trying to print each individual line of an external file, but I can only manage to print each individual word .我正在尝试打印外部文件的每一行,但我只能设法打印每个单词。 Here's what my code currently looks like:
这是我的代码当前的样子:
Scanner scnr = new Scanner(System.in);
System.out.println("Enter input filename: ");
String inputFile = scnr.next();
FileInputStream fileByteStream = null;
Scanner inFS = null;
fileByteStream = new FileInputStream(inputFile);
inFS = new Scanner(fileByteStream);
while (inFS.hasNext()) {
String resultToPrint = inFS.next();
System.out.println(resultToPrint);
}
So, for example, if the external.txt file is something like this: THIS IS THE FIRST LINE.因此,例如,如果 external.txt 文件是这样的: THIS IS THE FIRST LINE。 (new line) THIS IS THE SECOND LINE.
(换行)这是第二行。 (new line) THIS IS THE THIRD LINE.
(换行)这是第三行。 ...
...
Then right now it prints like this: THIS (new line) IS (new line) THE (new line) FIRST (new line) LINE (new line) THIS (new line) IS...然后现在它打印如下: THIS (new line) IS (new line) THE (new line) FIRST (new line) LINE (new line) THIS (new line) IS...
and I want it to print like how it appears in the original file.我希望它像在原始文件中那样打印。
Any suggestions on how to make each iteration of resultToPrint be a full line of text, as opposed to a single word?关于如何使 resultToPrint 的每次迭代成为整行文本而不是单个单词的任何建议? (I'm new to java, so sorry if the answer seems obvious!)
(我是 java 的新手,如果答案看起来很明显,我很抱歉!)
In order to read a file you need a BufferedReader :为了读取文件,您需要一个BufferedReader :
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
从字符输入 stream 读取文本,缓冲字符以便有效读取字符 arrays 和行。
Then use it's method readLine :然后使用它的方法readLine :
Reads a line of text.
读取一行文本。 A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
一行被认为是由换行符 ('\n')、回车符 ('\r') 或回车符后紧接着换行符中的任何一个终止的。
This code reads every line of a file then prints it:此代码读取文件的每一行然后打印它:
try (BufferedReader br = new BufferedReader(new FileReader(new File(filePath)))) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
Replace the line更换线路
inFS = new Scanner(fileByteStream);
by经过
inFS = new Scanner(fileByteStream).useDelimiter( "\\n" );
This will set the "word" separator to the line feed character, making the full line a single "word".这会将“单词”分隔符设置为换行符,使整行成为一个“单词”。
Or use java.nio.files.Files.lines()
…或者使用
java.nio.files.Files.lines()
…
Also java.io.BufferedReader.lines()
would be a nice alternative … java.io.BufferedReader.lines()
也是一个不错的选择……
Simpler and cleaner approach would be:更简单和更清洁的方法是:
FileInputStream fis = new FileInputStream("filename");
Scanner sc = new Scanner(fis);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
or或者
BufferedReader br = new BufferedReader(new FileReader("filename"));
br.lines().forEach(System.out::println);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.