【问题标题】:replace a line to above line for every two lines java每两行java替换一行到上面的行
【发布时间】:2016-06-17 20:11:22
【问题描述】:

我有一个这样的文本文件:

Emma,F,20355
Olivia,F,19553
Sophia,F,17327
Ava,F,16286
Isabella,F,15504
Mia,F,14820
Abigail,F,12311
Emily,F,11727

我正在尝试删除, 之后的单词,并且每两行将两行放在一行中。 例如:

Emma Olivia
Sophia Ava
Isabella Mia
Abigail Emily

程序可以做第一部分,但我不知道程序如何做第二部分。我可以在第一个, 之后拆分单词和数字,但是我卡住了如何排列行。

代码如下:

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
String[] a;
String res;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    res = a[0] + "\n";
    writer.write(res);
}
writer.close();
reader.close();

我想我需要在 while 循环中创建一个 for 循环,但我不确定要写什么来计算偶数行或奇数行。

【问题讨论】:

标签: java file


【解决方案1】:

改成这样的:

int count = 1;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    res = a[0] + count % 2 == 0 ? "\n" : " ";
    count++;
    writer.write(res);
}

【讨论】:

    【解决方案2】:
    try (
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))
    ) {
        while (true) {
            String line1 = reader.readLine();
            if (line1 == null) { 
                break;
            }
            writer.write(line1.split(",", 2)[0]);
    
            String line2 = reader.readLine();
            if (line2 == null) {
                writer.newLine();
                break;
            }
            writer.write(" " + line2.split(",", 2)[0]);
            writer.newLine();
        }
    }
    

    【讨论】:

    • 错了。输入错误会抛出异常。
    • @questionerandanswerer 什么例外?喜欢null
    • 你读的是行里面的,所以如果行数是奇数,当你尝试读新行时会抛出异常,因为可能没有行。
    • @questionerandanswerer 现已修复。它还按照 Elliott 的建议使用 try-with-resources
    • @JeffNeet 我什至不确定是否会首先出现奇数(OP 需要澄清),但我会编辑。
    【解决方案3】:
    int newLine = 1;
    while ((currentLine = reader.readLine()) != null) {
        a = currentLine.split(",");
        if (newLine % 2 == 0)
            res += a[0] + "\n";
        else
            res += a[0] + " ";
        newLine++;
    }
    writer.write(res);
    

    【讨论】:

    • 将整个文件存储在字符串中并不是一个好主意。最好在迭代时写入另一个文件。
    【解决方案4】:

    如果阅读器中还有第二行,请尝试同时读取两行。

    类似这样的:

    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
    String currentLine;
    String[] a;
    String[] b;
    String res;
    while ((currentLine = reader.readLine()) != null) {
        a = currentLine.split(",");
        if (reader.hasNext()) {
            b = reader.readLine().split(",");
            res = a[0] + " " + b[0] + "\n";
        } else {
            res = a[0]+"\n";
        }
        writer.write(res);
    }
    writer.close();
    reader.close();
    

    【讨论】:

    • 虽然这看起来ok,但我更喜欢try-with-resources 而不是两个明确的close 调用。
    • @ElliottFrisch 这是个好主意。我的回答对吗?
    • 这是一个公平的观点。我只是复制了 OP 提供的代码并修改了与他们的问题直接相关的部分。但是,在 try-with-resources 块中实例化 BufferedReader(假设 Java 7+)绝对是一种更安全的方法。
    【解决方案5】:

    如上所述,一次读两行。组合它们,然后根据分隔符(逗号)拆分 - 然后应该很容易将新格式写入文本文件(也许将结果弹出列表,然后遍历列表以将其写出。

    这不是一个完整的解决方案,但应该足以让您了解这个想法。

    // Read two lines at a time
    String currentLine = reader.readLine(); //Emma,F,20355
    String nextLine = reader.readLine();    //Olivia,F,19553
    
    String combinedLine = currentLine + "," + nextLine;
    
    // split into separate elements
    String[] elements = combinedLine.split(",");
    
    List<String> newLines = new ArrayList<>();
    newLines.add(elements[0] + " " + elements[3]);
    
    for (final String line : newLines) {
        // write to file
        writer.write(res);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      相关资源
      最近更新 更多