【问题标题】:edit a line in text file编辑文本文件中的一行
【发布时间】:2016-06-29 19:42:05
【问题描述】:

所以我有一个包含以下内容的文本文件:

not voting/1/harold/18
not voting/2/isabel/24

这类似于not voting/number for vote/name/age。我的目标是将not voting 编辑为voted,但仍保留其他信息(number for vote/name/age)。用户输入要投票的数字,如果存在,not voting会自动变为voted

这是我的代码:

            File original = new File("C:\\voters.txt");     
            File temporary = new File("C:\\tempvoters.txt");

            BufferedReader infile = new BufferedReader(new FileReader(original));
            PrintWriter outfile = new PrintWriter(new PrintWriter(temporary));

            numberforvote=JOptionPane.showInputDialog("Enter voters number: ");
            String line=null;

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

                String [] info=line.split("/");
                if(info[1].matches(numberforvote)){
                    all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
                    outfile.println(all);
                    outfile.flush();
                }
            }
        infile.close();
        outfile.close();

        original.delete();
        temporary.renameTo(original);

这可行,但我的代码的问题是第二行 (not voting/2/isabel/24) 将消失/删除。我希望一切都相同,除了给定/输入的投票号码中的not voting。

【问题讨论】:

    标签: java text-files file-handling


    【解决方案1】:
    if(info[1].matches(numberforvote)){
         all="VOTED"+"/"+info[1]+"/"...;
         outfile.println(all);
         outfile.flush();
    } else {
         outfile.println( line );
    }
    

    如果没有匹配则复制到输出。

    我应该补充一点,使用正则表达式进行单个字符串比较应该简化为更简单的info[1].equals(numberforvote)。但是调用numberforvote = numberforvote.trim(); 可能会有用。

    【讨论】:

      【解决方案2】:

      您的输出文件被完全覆盖,因此您必须写入所有行,即使是那些您不打算修改的行:

            if(info[1].matches(numberforvote)){
                  all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
                  outfile.println(all);
             }
            else{
                  outfile.println(line); // this will write the "unchanged" lines
             }
      
              outfile.flush();
      

      【讨论】:

      • 非常感谢它的工作! :)
      【解决方案3】:

      将其移到if 之外,只更改您需要更改的部分。这样你就可以改变任何你想要的然后重建线路。

      if(info[1].matches(numberforvote)){
        into[0] = VOTED;
      }
      
      all=info[0]+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
      
      outfile.println(all);
      outfile.flush();
      

      或清理那条丑陋的线

       StringBuilder sb = new StringBuilder();
       for (String element : info){
          sb.append(element);
       }
       outfile.println(sb.toString());
      

      其他答案

      您可以像其他人建议的那样输出未更改的行

      outfile.println(line);
      

      但如果您想稍后进行其他更改,它就没有那么灵活了。

      【讨论】:

        【解决方案4】:

        您应该简化拆分并写入:

        String [] info=line.split("/",2);
        if ( info.length == 2 ) {
           ...
           outfile.println("VOTED/"+info[1]);
        } else {
          // input error
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-27
          • 1970-01-01
          • 2020-12-17
          • 1970-01-01
          • 2016-06-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多