【问题标题】:Copying from file/Writing to file从文件复制/写入文件
【发布时间】:2013-12-18 13:33:57
【问题描述】:

如何从文件中“删除”一个字符。还有,文件里的东西怎么打印出来?

编写一个程序来读取文本文件,也许是小说的文本。程序将相同的文本复制到输出文件,除了所有无用的单词,如“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")) {

            }
        }
    }
}

编辑 感谢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);

}
}

【问题讨论】:

  • 您面临的具体问题是什么?
  • 所有无用的词如“the”、“a”和“an”都被删除你的教授必须是俄语。
  • 你检查过类似的问题吗?例如。 stackoverflow.com/questions/1664489/…

标签: java file file-io


【解决方案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);
        }

    }

【讨论】:

  • 谢谢,但我想知道我应该在 while(lineScannner.hasNext()) { } 我应该做 lineScanner.replace("a", " ") 等吗?另外,如何打印文件中的单词?谢谢。
  • 由于您是逐字扫描,因此您不想使用替换。而是使用 if 语句。 If word does not equal "a" or if word does not eqaul "the", ouputFile.print(word + " ");。每行打印到下一行outputFile.println()
  • 这对 peeskillet 有很大帮助,但你能检查一下我改进的程序,看看我做错了什么吗?谢谢!
  • 我会把所有的 ifs 放在一行中。请参阅我的编辑。您所做的是检查每个 if 的单词,因此如果不满足该单词,程序将打印 3 次。
  • 谢谢!抱歉再次打扰您,但是 java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped =false][组分隔符=\,][小数分隔符=\.][正前缀=][负前缀=\Q-\E][正后缀=][负后缀=][NaN字符串=\Q�\ E][infinity string=\Q∞\E] 是什么意思?
猜你喜欢
  • 2013-03-03
  • 2012-04-19
  • 2021-04-18
  • 2016-08-25
  • 2021-02-23
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多