Java 从文件读取输入流会打印缺少的字符

Java 从文件读取输入流会打印缺少的字符,java,java.util.scanner,bufferedreader,Java,Java.util.scanner,Bufferedreader,我使用java中的BufferedReader从输入文件中读取字符。然而,输出是不寻常的。在下面的代码中,我首先使用scanner显示文件本身的内容,然后使用BufferedReader打印每个字符: import java.util.Scanner; import java.io.*; //import java.io.File; public class Inputs { public static void main(String[] args) {

我使用java中的BufferedReader从输入文件中读取字符。然而,输出是不寻常的。在下面的代码中,我首先使用scanner显示文件本身的内容,然后使用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());
    }

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

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

这个问题看起来很酷。你的代码没有意义。您正在读取行,但随后打印下一个字符,不管它是什么,即使它是-1表示流结束。如果你想打印文件,打印你读到的行。这个问题看起来很酷。你的代码没有意义。您正在读取行,但随后打印下一个字符,不管它是什么,即使它是-1表示流结束。如果要打印文件,请打印您读取的行。这会给我一个无限循环,我们可以通过在while循环中添加(char)-1来修复。真管用!添加建议的修复程序:)c=(char)br.read())!=(char)-1,第二个条件在这里检查什么?第二个条件是什么意思?(C=(char)bR.Read())将值从BR.Read()分配给变量C -在C值与(char)- 1(文件结束)比较之后,得到它。考虑删除(char)和-1之间的空间。它可以使它更具可读性,并且理解您正在将-1转换为char。这给了我一个无限循环,我们可以通过在while循环中添加(char)-1来修复它。真管用!添加建议的修复程序:)c=(char)br.read())!=(char)-1,第二个条件在这里检查什么?第二个条件是什么意思?(C=(char)bR.Read())将值从BR.Read()分配给变量C -在C值与(char)- 1(文件结束)比较之后,得到它。考虑删除(char)和-1之间的空间。它可以使它更具可读性,并理解您正在将-1转换为char。