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

时间:2017-02-08 09:03:58

标签: 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)
    {
        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());
          }






    }
}

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

enter image description here

2 个答案:

答案 0 :(得分:2)

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

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)

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