有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用inputStream作为outputStream的文本?

我有两个文件,葛底斯堡地址。写好葛底斯堡地址的txt和葛底斯堡地址副本。txt,这是一个空文本文件,我应该用葛底斯堡地址填充,每个句子,直到它的句号,在不同的行上。 所以我想到了这个

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

    }
}

运行时,程序会正确地创建葛底斯堡地址副本。txt,如果它还不存在,但它没有用语音填充它。我意识到这三行代码都很幼稚

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

但是,正确的代码是什么? 非常感谢你给我最好的建议


共 (1) 个答案

  1. # 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);
    
        }
    }