【问题标题】:reading content from file, then copying it to another file (updated code)从文件中读取内容,然后将其复制到另一个文件(更新代码)
【发布时间】:2016-01-12 18:17:30
【问题描述】:

代码如下:

    FileReader fr = new FileReader("datos_clientes.txt");
    BufferedReader br = new BufferedReader(fr);

    while ((line = br.readLine()) != null) {
        String nameMark = "#n";
        String addressMark = "#d";

        int nameStart = line.indexOf(nameMark) + nameMark.length();
        int addressStart = line.indexOf(addressMark) + addressMark.length();
        String name = line.substring(nameStart, addressStart - addressMark.length());
        String address = line.substring(addressStart, line.length());
        if (line.startsWith("tipo1.")) {
            FileWriter fw = new FileWriter(name +".txt");
            char[] vector = name.toCharArray();
            char[] vector2 = address.toCharArray();
            int index = 0;
            while (index < vector.length) {
                fw.write(vector[index]+vector2[index]);

                index++;
            }
            fw.close();
        } else if (line.startsWith("tipo2.")) {
            FileWriter fw = new FileWriter(name +".txt");
            char[] vector = name.toCharArray();
            char[] vector2 = address.toCharArray();
            int index = 0;
            while (index < vector.length) {
                fw.write(vector[index]+vector2[index]);

                index++;
            }


        fw.close();
        }

        else if (line.startsWith("tipo3.")) {
            FileWriter fw = new FileWriter(name +".txt");
            char[] vector = name.toCharArray();
            char[] vector2 = address.toCharArray();
            int index = 0;
            while (index < vector.length) {
                fw.write(vector[index]+vector2[index]);

                index++;
            }
            fw.close();

    }


}

我希望从这段代码中创建每个带有收件人姓名及其地址的新文件。 新文件仅显示随机字母字符的组合。

然后我有三个预制文件,我必须将它们包含在每个新文件中,例如,如果其中一个新文件是“Maria Roberts.txt”并且此人将收到一封“类型 1”的信件,我想要文件 (Maria Roberts.txt) 包括姓名、她的地址和文件“type1.txt” 我不知道该怎么做。

我知道我会在每个新问题中添加一些内容...抱歉,我认为这对我来说会更容易理解。 再次感谢!

【问题讨论】:

  • 您的错误是否说明了是哪条线路导致的?如果else if(..tipo2..) 的代码与if 完全相同,那么它的意义何在?
  • 您的输入中是否有可能缺少“#n”或“#a”?
  • 埃里克是对的!在子字符串操作中使用它们之前,您应该始终检查索引是否为 ">=0"。
  • 是的,他是对的!谢谢。现在我如何用接收文件的人的名字来命名文件? @ErickG.Hagstrom
  • 谢谢!我现在有个问题。在新文件中,我也需要写地址,当我这样做时,文件中的文本会混淆。我怎样才能正确地做到这一点? @ErickG.Hagstrom

标签: java string file loops search


【解决方案1】:

您将名称数组中的一个字符与地址数组中的一个字符相加,然后输出结果。

fw.write(vector[index]+vector2[index]);

相反,您想写入整个名称数组,然后(在不同的循环中)写入整个地址数组。

        int index = 0;
        while (index < vector.length) {
            fw.write(vector[index]);
            index++;
        }
        index = 0;
        while (index < vector2.length) {
            fw.write(vector2[index]);
            index++;
        }

这只会将它们粘在一起,但您可以发挥自己的想象力并找出如何以您想要的方式将它们分开。

【讨论】:

  • 非常感谢您的帮助!并将另一个文件的内容写入新文件我该怎么做?
  • 来吧,@Maria。试一试。您已经知道完成这项工作所需的一切。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多