【问题标题】:Reading input stream from file prints missing characters从文件中读取输入流打印丢失的字符
【发布时间】:2017-02-08 09:03:57
【问题描述】:

我在 java 中使用 BufferedReader 从输入文件中读取字符。但是,输出异常。在下面的代码中,我首先使用扫描仪显示文件本身的内容,然后使用 BufferedReader 打印每个单独的字符:

import java.util.Scanner;
import java.io.*;
//import java.io.File;

public class Inputs
{


    public static void main(String[] args)
    {
        System.out.println("Inputs");
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        System.out.println(input);
        // Open the file
        File file = new File("Simple.java");
        try {

            Scanner fileIn = new Scanner(file);

            while(fileIn.hasNext()){
                 System.out.println(fileIn.next());

                }
            fileIn.close();
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("Simple.java");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          //Read File Line By Line
          while ((strLine = br.readLine()) != null)   {
          // Print the content on the console
          String character =  Character.toString ((char) br.read());
          System.out.println (character);
          }
          //Close the input stream
          in.close();
            }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
          }






    }
}

下面是我得到的输出,显示的第一个标记是由扫描仪显示的,但在突出显示的学徒之后是 BufferedReaders 输出。它打印 3 个空格,然后打印一个闭合的大括号,然后打印 -1(结束)。这是什么意思?

【问题讨论】:

  • 这个问题看起来很酷。
  • 您的代码没有意义。您正在读取行,然后打印 next 字符,无论它是什么,即使它是 -1 表示流结束。如果要打印文件,请打印您阅读的行。

标签: java java.util.scanner bufferedreader


【解决方案1】:

在您的代码中,您正在使用br.readline() 读取while 循环条件中的行。然后在循环体中,您再次使用br.read() 读取字符。所以每行的第一个字符都会被打印出来。

while ((strLine = br.readLine()) != null)   {
          // Print the content on the console
          String character =  Character.toString ((char) br.read());
          System.out.println (character);
}

要解决这个问题,要么使用 Halko Sajtarevic 提出的方法(但每个字符都被读取为整数并转换回字符串),或者简单地消除第二次读取字符并打印字符串。

while ((strLine = br.readLine()) != null)   {
          // Print the content on the console
          System.out.println (strLine);
}

【讨论】:

    【解决方案2】:

    您的问题是您正在逐行而不是逐字符读取 BufferedReader。

    试试这个版本:

    try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("Simple.java");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //Read File Line By Line
            char c;
            while ((c = (char) br.read()) != (char) -1) {
                // Print the content on the console
                String character = Character.toString(c);
                System.out.print(character);
            }
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    

    【讨论】:

    • 这给了我一个无限循环,我们可以通过在 while 循环中添加 (char) -1 来修复它。它有效!
    • 添加您建议的修复方法:)
    • c = (char) br.read()) != (char) -1 ,第二个条件在这里检查什么?
    • 第二个条件是什么意思? (c=(char) br.read()) 将 br.read() 中的值赋给变量 c - 然后将 c 的值与 (char) -1 (End of File) 进行比较
    • 谢谢,知道了。考虑删除 (char) 和 -1 之间的空格。它可以使其更具可读性并理解您将 -1 转换为 char。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多