简体   繁体   English

如何在 java 中打印整行文本(来自输入文件)

[英]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.

相关问题 从java中的文本文件打印随机行 - Print random line from text file in java 如果在文本文件中找到输入的int,如何从Java中的导入文本文件打印一行? - How do i print a line from an imported text file in java if the inputted int is found in that text file? 如何从文本文件复制行并打印行和行号 - How to copy a line from a text file and print the line and line number 如何使用Java中的math.random从文本文件中打印一行特定的文本? - How do you print a specific line of text from a text file using math.random in Java? Java; 从输入文本文件到输出文本文件的换行符 - Java; Line breaks from input text file to output text file 从 java 中的文本文件打印包含特定模式的行 - print a line that contains a specific pattern from a text-file in java 让 Java 能够从文本文件中读取一行并打印出来 - Having Java be able to read one line from a text file and print it 如何在java中的输出文本文件中的每一行中打印stringbuilder的内容 - How to print the contents of stringbuilder in each line in a output text file in java 如何在Java中读取文本文件的第一行并打印出来? - How to read the first line of a text file in java and print it out? 如何从文本文件中逐行将变量分配到Java文件中 - How to assign variables into a java file from a text file line by line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM