2017-07-10 3 views
0

Gettysburg Addressが書かれているGettysburgAddress.txtとGettysburgAddressCopy.txtという2つのファイルがあります。これはGettysburg Address、各文、その期間まで記入する必要がある空のテキストファイルです別の行。実行すると は、だから私はこのinputStreamをoutputStreamのテキストとして使用するにはどうすればよいですか?

import java.io.PrintWriter;` 
import java.io.FileNotFoundException;v` 
import java.util.Scanner;` 
import java.io.File; 

public class UltimateTextFileOutputDemo 

{ 

    public static void main(String[] args) 
    { 
     String writtenFileName = "C:\\Documents and Settings\\GettysburgAddressCopy.txt"; 
     PrintWriter outputStream = null; 
     try 
     { 
      outputStream = new PrintWriter(writtenFileName); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Error opening the file" + writtenFileName); 
      System.exit(0); 
     } 


     String readFileName = "C:\\Documents and Settings\\GettysburgAddress.txt"; 
     Scanner inputStream = null; 
     try 
     { 
      inputStream = new Scanner(new File(readFileName)); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Error opening the file " + 
          readFileName); 
      System.exit(0); 
     } 
     while (inputStream.hasNextLine()) 
     { 
      inputStream.useDelimiter(".");   // setting the period as the delimiter of the read piece of text, I'm sure it gets a single, complete sentence 
      String line = inputStream.nextLine(); 
      outputStream.println(line); 
     } 
     outputStream.close(); 
     inputStream.close(); 

     System.out.println("The Gettysburg Address was written to " + writtenFileName); 

    } 
} 

を考え、それがまだ存在しない場合、プログラムが正しく、GettysburgAddressCopy.txtを作成しますが、それはスピーチでそれを満たしていません。私はコードネイヴェがすべて3行に入っていることを理解しています。

inputStream.useDelimiter("."); 
String line = inputStream.nextLine(); 
outputStream.println(line); 

しかし、正しいコードは何ですか? できるだけ多くのヒントをいただき、ありがとうございます。

答えて

-1
import java.io.PrintWriter; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.io.File; 

public class UltimateTextFileOutputDemo 

{ 

    public static void main(String[] args) 
    { 
     String writtenFileName = "D:\\testdump\\GettysburgAddressCopy.txt"; 
     PrintWriter outputStream = null; 
     try 
     { 
      outputStream = new PrintWriter(writtenFileName); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Error opening the file" + writtenFileName); 
      System.exit(0); 
     } 


     String readFileName = "D:\\testdump\\GettysburgAddress.txt"; 
     Scanner inputStream = null; 
     try 
     { 
      inputStream = new Scanner(new File(readFileName)); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Error opening the file " + 
          readFileName); 
      System.exit(0); 
     } 
     inputStream.useDelimiter("\\."); 
     while (inputStream.hasNextLine()) 
     { 
         // setting the period as the delimiter of the read piece of text, I'm sure it gets a single, complete sentence 
      String line = inputStream.next(); 
      outputStream.println(line); 
     } 
     outputStream.close(); 
     inputStream.close(); 

     System.out.println("The Gettysburg Address was written to " + writtenFileName); 

    } 
} 
関連する問題