0

I have two files, GettysburgAddress.txt, where the Gettysburg Address is written, and GettysburgAddressCopy.txt, which is an empty text file that I'm supposed to fill in with the Gettysburg Address, each sentence, till its period, on a different line. So I thought of this

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

    }
}

When run, the program rightly creates the GettysburgAddressCopy.txt, if it doesn't exist yet, but it doesn't fill it with the speech. I realize the code naïvety is all in the three lines

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

but then, what's the right code to write? Many thanks for giving me the best tips you can.

1 Answers1

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

    }
}
now30
  • 40
  • 6