【问题标题】:Looking for a word or character in a file在文件中查找单词或字符
【发布时间】:2016-01-12 15:41:18
【问题描述】:

我正在尝试编写一个程序,该程序给定一个包含五个或更多人的姓名和地址的文件,为每个人创建一个不同的文件(字母)(新文件将被命名为将接收的人它)。 主文件的结构是这样的:

type1.0001 #n John Harrison #a Whatever Street, 490 - Liverpool
.... and so on

所以“type1”是此人必须发送的信件类型,“#n”之后的单词是姓名,“#a”之后的单词是地址。

我一直在尝试的是:

String datos = "main_file.txt";   
String tipo1 = "type1.txt";
String tipo2 = "type2.txt";
String tipo3 = "type3.txt";


char[] type1 = {'t', 'i', 'p', 'o', '1'};
//all other types should be here
String line;
FileReader fr = new FileReader("mainfile.txt");
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
    while ((line = br.readLine()) != ".") {
        char[] lineArray = line.toCharArray();
        if (lineArray == type1) {
            //code that creates file type1

        }
    }
}

fr.close();

到目前为止,这只是决定发送哪封信的代码,但它不起作用。 我认为这与“while”循环有关。

拜托,我 1 个月前开始使用 Java,如果有人能帮助我,我将不胜感激!

谢谢

现在,我有这个:

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

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

        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("file1.txt");
            char[] vector = name.toCharArray();
            int index = 0;
            while (index < vector.length) {
                fw.write(vector[index]);
                index++;
            }
            fw.close();
        } else if (line.startsWith("tipo2.")) {
            FileWriter fw = new FileWriter("file1.txt");
            char[] vector = name.toCharArray();
            int index = 0;
            while (index < vector.length) {
                fw.write(vector[index]);
                index++;
            }
            fw.close();

        }
    }

    fr.close();

但它不起作用。

谁能帮帮我?

【问题讨论】:

  • 您有两个 readLine() 调用,因此您在外部循环中获取一行,然后在第二个循环中获取另一行,丢弃第一行。所以你的外循环只会获取一行,它会被丢弃,然后内循环将处理文件的其余部分,外循环什么都没有。
  • 感谢您的帮助!!现在我如何读取每个人的姓名和地址?我需要将它们都包含在每个新文件中
  • 我编辑了我的问题,有人可以检查一下吗?

标签: java file loops search


【解决方案1】:

正如 Marc B 告诉你的,不要把这些行读两遍。

此外,仅比较行首,将比您使用 char 数组的东西少得多。

要检索姓名和地址,您可以使用StringindexOfsubstring 方法。

这是一个完整的例子:

   while ((line = br.readLine()) != null) {

            // get the name and the address of this line
            String nameMark = "#n";
            String addressMark = "#a";

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

            // get the line type
            if (line.startsWith("tipo1")) {
                    //code that creates file type1 with name and address
            }
            else if(line.startsWith("tipo2")) {
                    //code that creates file type2 with name and address
            }
    ...
    ...
        }

【讨论】:

  • 感谢您的帮助!!现在我如何读取每个人的姓名和地址?我需要将它们都包含在每个新文件中。
  • 我刚刚添加了一些示例来检索您的字段。
  • 我必须为每个人写这个,还是只写一次?
  • 您在循环中只写一次,它将应用于每个读取行(参见编辑后的代码)。
  • 我正在尝试运行它,但它给了我这个:线程“main”java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围:-15
【解决方案2】:

您不能将== 与数组一起使用。 (嗯,你可以,但它并没有达到你的预期。)也就是说,这行是错误的:

  if (lineArray == type1)

改用Arrays.equals

  if (Arrays.equals(lineArray, type1))

【讨论】:

  • 你真的是this book的作者吗?
  • 感谢您的帮助!!现在我如何读取每个人的姓名和地址?我需要将它们都包含在每个新文件中
【解决方案3】:

初学者试试这个。

    String line;
    FileReader fr = new FileReader("mainfile.txt");
    BufferedReader br = new BufferedReader(fr);
    while ((line = br.readLine()) != null) { // finish when line is null not "."

        String [] parts = line.split("#");

        String name, address;
        if (parts.length > 2) {
            name = parts[1].substring(2);
            address = parts[2].substring(2);
        }

        if (line.startsWith("tipo1")) {
            // save to tipo1 file
        } else if (line.startsWith("tipo2")) {
            // save to tipo2 file
        } else if (line.startsWith("tipo2")) {
            // save to tipo2 file
        } else if (line.startsWith("tipo3")) {
            // save to tipo2 file
        } else if (line.startsWith("tipo4")) {
            // save to tipo2 file
        } else if (line.startsWith("tipo2")) {
            // save to tipo2 file
        }
    }

    fr.close();

【讨论】:

    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多