从文件复制/写入文件

时间:2013-12-02 18:22:53

标签: java file file-io

如何从文件中“删除”某个字符。另外,你如何打印文件中的东西?

编写一个程序,读取文本文件,也许是小说的文本。程序将相同的文本复制到输出文件,除了删除所有无用的单词,如“the”,“a”和“an”。 (确定要移除的其他单词。删除的单词列表称为停止列表。)通过使用hasNext()和next()读取令牌的文本文件令牌来执行此操作,但只写出不在停止列表。

提示用户输入和输出文件的名称。保留输入文件的行结构。通过使用nextLine()读取每一行,然后为该行创建一个新的Scanner来完成此操作。 (查看Scanner的在线文档。)对于每一行的Scanner,使用hasNext()和next()来扫描其标记。

public static void main(String[] args) throws IOException {

    String fileName;
    Scanner user = new Scanner(System.in);
    System.out.print("File name: ");
    fileName = user.nextLine().trim();

    File file = new File(fileName);



    PrintStream printfile = new PrintStream(file);
    System.out.println("Input data into file: ");
    String datainfile = user.nextLine();
    Scanner scan = new Scanner(file);
    printfile.println(datainfile);

    while (scan.hasNextLine()) {
        while (scan.hasNext()) {
            String character = scan.next();
            if (character.equals("a")) {

            }
        }
    }
}

EDIT 感谢peeskillet,我再试一次。但是,在我的程序中某处似乎出现了错误,我得到了:

  AAApotatopotatopotatojava.util.Scanner[delimiters=\p{javaWhitespace}+][position=0]                            [match valid=false][need input=false][source closed=false][skipped=false][group  separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

你能查看我的节目吗?

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Input File name: ");
    String filename1 = keyboard.nextLine().trim();
    System.out.print("Output File name: ");
    String filename2 = keyboard.nextLine().trim();

    File inputFile = new File(filename1);
    File outputFile = new File(filename2);

    PrintStream printfile = new PrintStream(inputFile);
    System.out.println("Input data into file: ");
    String datainfile = keyboard.nextLine();

    printfile.println(datainfile);

    Scanner inFile = new Scanner(inputFile);
    PrintWriter writeFile = new PrintWriter(outputFile);

    Scanner lineScanner;

    while (inFile.hasNextLine()) {

        String line = inFile.nextLine();

        lineScanner = new Scanner(line);

        while (lineScanner.hasNext()) {
            String word = lineScanner.next();
            if(!(word.equals("a"))) {
                writeFile.print(word + " ");
                System.out.print(word);

            }
            if(!(word.equals("an"))) {
                writeFile.print(word + " ");
                System.out.print(word);
            }
            if(!(word.equals("the"))) {
                writeFile.print(word + " ");
                System.out.print(word);
            }
            else {
                writeFile.print(" ");
            }

        }

        writeFile.println();

    }
    writeFile.close();

    Scanner readOutput = new Scanner(outputFile);

    System.out.println(readOutput);

}
}

1 个答案:

答案 0 :(得分:1)

首先,您需要两个File个对象,一个用于输入,另一个用于输出。你只有一个。

你想做这样的事情

Scanner keyboard = new Scanner(System.in);
System.out.print("Input File name: ");
String filename1 = keyboard.nextLine();
System.out.print("Output File name: ");
String filename2 = keyboard.nextLine();

File inputFile = new File(filename1);
File outputFile = new File(filename2);

Scanner infile = new Scanner(inputFile);
PrintWriter outputFile = new PrintWriter(outputFile);

Scanner lineScanner;

while(infile.hasNextLine()){             // here you read each line of a file
    String line = inFile.nextLine();     // here is a line

    lineScanner = new Scanner(line);     // for the above line, create a scanner 
                                         // just to scan that line
    while(lineScanner.hasNext()){        // loop through that line
        // do something
    }
}
outputFile.close();

编辑:我只是将所有条件都放在一个陈述中

    while (lineScanner.hasNext()) {
        String word = lineScanner.next();
        if(!(word.equals("a")) && !(word.equals("an")) && !(word.equals("the"))) {
            writeFile.print(word + " ");
            System.out.print(word);
        }

    }