【问题标题】:How can I write a new line of text to a file once a specific line of text has been found?找到特定的文本行后,如何将新的文本行写入文件?
【发布时间】:2015-03-26 09:20:00
【问题描述】:

我正在创建一个程序,它可以简单地创建足球队的对象,然后将球队的名称和记录存储在一个文本文件中。

我正在尝试在我的文件中创建部分,以便我可以打印出同一大陆上的团队。

假设下一行文本是我的文本文件的开头

北美

结束北美

大家好

如果我想让我在北美的团队的对象只写在这两行文本之间,我该怎么做?

目前我得到的结果不能正常工作,团队名称和记录正在“结束北美”之后打印。

以下是我目前的代码。

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

public class Teams {

   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
  Scanner nameInput = new Scanner(System.in);

  System.out.println("Team Creator!");
  System.out.println("-----------------------------");
  System.out.println("Select an option");
  System.out.println("");
  System.out.println("1. Create-A-Team");
  System.out.println("");

  int optionSelection = input.nextInt();
  if(optionSelection==1) {

     System.out.println("Please enter the name of the team you'd like to create");

     String teamName = nameInput.nextLine();
     Team team = new Team();
     team.createTeam(teamName);
          }
       }
    }

class Team {
   String name;
   int wins,loses;
   boolean northAmericanTeam = true;

   public void createTeam(String name) {
      Team newTeam = new Team();
      newTeam.name = name;
      this.wins = 0;
      this.loses = 0;
      System.out.println("You  created " + name + "!");
      System.out.println("Wins: " + wins);
      System.out.println("Loses: " + loses);

  if(northAmericanTeam == true) {
     BufferedReader reader = null;

     try(PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter("teaminfo.txt", true)))) {
        File file = new File("teaminfo.txt");
        reader = new BufferedReader(new FileReader(file));

        String line;
        String nAmerica ="North America";
        while ((line = reader.readLine()) != null) {
           System.out.println(line);

           if(line.equals(nAmerica)) {
              write.println(name+"" + ": Wins[" +wins+"] Loses[" +loses+"]");
              write.close();
           }
        }

     } 
     catch (IOException e) {
        e.printStackTrace();
     } 
     finally {
        try {
           reader.close();
        } 
        catch (IOException e) {
           e.printStackTrace();
            }
         }
      }
   }
}

【问题讨论】:

  • 看来您正在写入您正在读取的同一个文件 - 这不起作用,您需要写入另一个文件。 (之后可能将该文件移至您原来的 teaminfo.txt)

标签: java string file text line


【解决方案1】:

我真的不知道这是否是最好的解决方案,但您可以使用模式来做到这一点。

private static final Pattern pAmerica = Pattern.compile("End\\sNorth\\sAmerica");

然后,在你的打印机中,你可以匹配它并写在后面:

while ((line = reader.readLine()) != null) {
           System.out.println(line);
           Matcher mAmerica = pAmerica.matcher(line);
           if(mAmerica.find()) {//Maybe using mAmerica.matches() here can be better
              write.println(name+"" + ": Wins[" +wins+"] Loses[" +loses+"]");
              write.close();
           }
        }

永远不要忘记以静态方式编译您的模式,以确保使用最低性能,它们很难编译^^

【讨论】:

    【解决方案2】:

    您假设作者将被放置在读者到达的地方,这是不正确的。

    例如,当读取器到达第 5 行时,写入器仍将尝试在文件末尾写入。

    您可以将整个文件(如果不是那么大)读入 StringBuilder,然后对其进行操作,然后将 i 写回文件:

            StringBuilder filecontent = new StringBuilder("Yor File Contents");
            if (filecontent.indexOf("North America") != -1){
                filecontent.insert(filecontent.indexOf("North America\n" + "North America\n".length(), "\n" + name+"" + ": Wins[" +wins+"] Loses[" +loses+"]")
            }
    
    // write filecontent.toString() to the File again
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多