从文件中读取输入流打印丢失的字符

Reading input stream from file prints missing characters

我在 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(结束)。这是什么意思?

您的问题是您正在逐行而不是逐字符读取 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());
    }

在您的代码中,您正在使用 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);
}