简体   繁体   中英

Reading input stream from file prints missing characters

I am using BufferedReader in java to read characters from an input file. However, the output is unusual. In my code below, I first use scanner to show the contents of the file itself then I use BufferedReader to print each individual character:

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());
          }






    }
}

Below is the output I get, the first tokens displayed are by the scanner but after the highlighted prentices its the BufferedReaders output. It prints 3 spaces then a closed curly bracket then -1 (end). What is the meaning of this?

在此处输入图片说明

In your code you are reading the line in while loop condition with br.readline() . Then in the loop body you are again reading the char using br.read() . So the first character in the each line is printed.

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

To fix there either use the method metioned by Halko Sajtarevic(But there each char is read as an integer and converted back to string) or simply eliminate reading the character second time and just print the string.

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

Your problem is that you are reading the BufferedReader line by line instead of character by character.

Try this version instead:

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());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM