2017-02-08 6 views
0

私は入力ファイルから文字を読み込むために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(終わり)を表示します。これの意味は何ですか?

enter image description here

+0

この質問はクールだと思います。 –

+1

あなたのコードは意味をなさない。あなたは行を読み込んでいますが、ストリームの終わりを示す-1であっても、* next *文字を印刷します。ファイルを印刷する場合は、読み込んだ行を出力します。 – EJP

答えて

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

これは私に無限ループを与えます。これはwhileループに(char)-1を加えることで修正できます。それは働くタイ! –

+0

提案された修正を追加してください:) –

+0

c =(char)br.read())!=(char)-1、ここで2番目の条件は何を確認しますか? – mc20

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

はるこSajtarevicによってmetioned方法を使用するかが解決するには(そこ各文字を整数として読み取られ、文字列に変換される)、または単に文字二度目に読み取る排除し、単に文字列を印刷します。

while ((strLine = br.readLine()) != null) { 
      // Print the content on the console 
      System.out.println (strLine); 
} 
関連する問題