【问题标题】:remove all the occurances of "Meredith" from a text file [closed]从文本文件中删除所有出现的“Meredith”[关闭]
【发布时间】:2017-09-09 03:14:43
【问题描述】:

我的程序应该从命令行获取参数。例如,如果我调用 java Test1 Meredith,从 myFile.txt 中删除 Meredith,并将更新的信息发送到名为 targetFile.txt 的新文本文件中。我不确定它是否确实访问了 myFile.txt。它是如何访问的?我的意思是它怎么知道我需要从下面的代码访问 myFile.txt ?是 args[0] 吗?

import java.io.*;
import java.util.*;

public class Test1 {

    public static void main(String[] args) throws Exception {
        if (args.length != 4) {
            System.out.println(
                    "Usage: java Test1 myFile.txt targetFile.txt aaa ccc");
            System.exit(1);
        }

        // Check if source file exists
        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(2);
        }

        // Check if target file exists
        File targetFile = new File(args[1]);
        if (targetFile.exists()) {
            System.out.println("Target file " + args[1] + " already exists");
            System.exit(3);
        }
        StringBuilder sb = new StringBuilder();
        try (
                // Create input and output files
                Scanner input = new Scanner(sourceFile);
                PrintWriter output = new PrintWriter(targetFile);) {
            while (input.hasNext()) {
                String s1 = input.nextLine();
                String s2 = s1.replaceAll(args[2], args[3]);
                sb.append("\r\n" + s2);
            }
        }
        try (
                PrintWriter output = new PrintWriter(sourceFile);) {
            output.printf("%s\r\n", sb.toString());
        }
    }
}

【问题讨论】:

  • 问题出在哪里?
  • 不应该是s1.replaceAll(args[2], ""); 来简单地删除“Meredith”吗?
  • 看起来您正在将结果写回sourceFile,而不是targetFile...
  • 好吧,我不确定它是否可以访问 myFile.txt。它怎么知道那是我首先需要访问的文件?
  • 梅雷迪思对你做了什么??

标签: java command-line io


【解决方案1】:

这...

try (
        // Create input and output files
        Scanner input = new Scanner(sourceFile);
        PrintWriter output = new PrintWriter(targetFile);) {
    while (input.hasNext()) {
        String s1 = input.nextLine();
        String s2 = s1.replaceAll(args[2], args[3]);
        sb.append("\r\n" + s2);
    }
}
try (
        PrintWriter output = new PrintWriter(sourceFile);) {
    output.printf("%s\r\n", sb.toString());
}

似乎从sourceFile 读取,构建一个新的String(通过StringBuilder)然后将其写回sourceFile...

我建议改成类似的东西

    try (
            // Create input and output files
            Scanner input = new Scanner(sourceFile);
            PrintWriter output = new PrintWriter(targetFile);) {
        while (input.hasNext()) {
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2], args[3]);
            output.printf("%s%n", s2);
        }
    }

我也建议更换...

System.out.println(
        "Usage: java Test1 myFile.txt targetFile.txt aaa ccc");

有点像...

System.out.println("Usage: Test1 {source file} {target file} {source text} {replacement text}");
System.out.println("Where:");
System.out.println("\tsource file - is the source file to be read from");
System.out.println("\ttarget file - is the target file that the results are to be written to");
System.out.println("\tsource text - is the text to be replaced");
System.out.println("\treplacement text - is the new text to take its place");

对于不知道程序的用途或应该如何工作的用户来说,这将更有意义

但我的目标文件仍然是空的。不知道。所以基本没有变化

修改后的代码对我来说很好,我建议你查看错误的输出文件。尝试添加...

System.out.println("Reading from " + sourceFile.getAbsolutePath());
System.out.println("  Writing to " + targetFile.getAbsolutePath());

在读/写循环之前(或之后),这将打印出文件的绝对路径,并将其与您尝试使用的文件的位置进行比较

【讨论】:

  • 这可以称为java Test1 myFile.txt targetFile.txt Merdith ""
  • 非常感谢,这看起来很棒。但是我的目标文件仍然是空的。不知道。所以基本没有变化
  • @CrazyGal 除了你正在查看错误的文件这一事实之外,不,因为它对我来说很好用
  • 所以您在目标文件中看到了结果?
  • @CrazyGal 我所做的只是拿走你的代码,用我的建议更新它,然后它就奏效了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2020-06-15
  • 2014-08-10
  • 1970-01-01
  • 2017-04-17
  • 2014-01-04
相关资源
最近更新 更多