【问题标题】:append text at the end of the specific lines在特定行的末尾附加文本
【发布时间】:2015-05-13 12:03:40
【问题描述】:

我正在尝试在 java 中执行此功能,但它不工作 我正在尝试在现有 .txt 文件中指定行的末尾添加一个新字符串(详细说明变量 corso 具有相同的 dati[0] 值)。

txt文件格式如下:

课程标识符1_课程名称1_课程描述1_教师标识符1 课程标识符2_课程名称2_课程描述2_教师标识符2

我的问题是如何在行尾附加新的字符串用户? 我的意思是如何在teacher_identifier 之后添加用户?

这是我的功能:

    public void addStudente(String user,String corso) throws IOException{

    FileReader file = new FileReader("C:\\Users\\Marco\\workspace\\ChatRoom\\corsi.txt");
    BufferedReader lettore = new BufferedReader(file);

    FileWriter fileout = new FileWriter("C:\\Users\\Marco\\workspace\\ChatRoom\\corsi.txt",true);
    BufferedWriter filebuf = new BufferedWriter(fileout);
    PrintWriter printout = new PrintWriter(filebuf);

    String temp;
    String[] dati = null;

    while((temp = lettore.readLine()) != null){

        dati = temp.split("_");
        System.out.println("line length: "+lettore.readLine().length());
        if(dati[0].equals(corso)){
            printout.write("_"+user, lettore.readLine().length(),user.length()+1);

        }

    }

    printout.close();
    file.close();


}

【问题讨论】:

    标签: java file append


    【解决方案1】:

    您需要将读取的行写入输出,然后写入您的用户,最后写入新行。

    while(...) {
    ....
    printout.write(temp);
    if (...) {
     printout.write("_"+user);
    }
    printout.println();
    }
    

    【讨论】:

      【解决方案2】:

      第一件事是,你在循环体内多次使用lettore.readLine()。但是您应该知道,这会通过在每次调用时读取下一个可用行来消耗您的输入流。但是您只想每轮读取一行。这是在 while 谓词中完成的。您将该行存储在 temp 变量中。在循环体内你应该只使用这个变量并且不要再调用readLine()

      然后您解析该行以确定这是否是添加用户的行。但是else 分支没有任何动作。如果这不是要修改的行,则应按原样打印整行:

      if(dati[0].equals(corso)){
        // ...
      } else {
        printout.println(temp); // Write the unmodified line
      }
      

      那如果你找到要修改的那行,为什么不使用PrintWriterprint()println()方法呢? 这样的事情就足够了:

      if(dati[0].equals(corso)){
        // Add the user to the line
        printout.print(temp);
        printout.print('_');
        printout.println(user);
      } else {
        printout.println(temp); // Write the unmodified line
      }
      

      【讨论】:

        【解决方案3】:

        感谢您的帮助,我已根据您的建议修复了我的代码,现在代码正在运行 我使用以下代码:

        public void addStudente(String user,String corso) throws IOException{
        
            FileReader file = new FileReader("C:\\Users\\Marco\\workspace\\ChatRoom\\corsi.txt");
            BufferedReader lettore = new BufferedReader(file);
        
            FileWriter fileout = new FileWriter("C:\\Users\\Marco\\workspace\\ChatRoom\\corsi.txt",true);
            BufferedWriter filebuf = new BufferedWriter(fileout);
            PrintWriter printout = new PrintWriter(filebuf);
        
            String temp;
            String[] dati = null;
        
            while((temp = lettore.readLine()) != null){
        
                dati = temp.split("_");
                printout.write(temp);
        
                    if(dati[0].equals(corso)){
        
                        printout.write("_"+user);
                    }
        
                printout.println();
        
            }
        
            printout.close();
            file.close();
        }
        

        代码正确添加了用户,但它们不会覆盖文件中的文本。 例如在运行里面的代码之前有:

        345_title_course_description
        456_title1_course1_description1

        运行后的文件是:

        345_title_course_description
        456_title1_course1_description1
        345_title_course_description_user
        456_title1_course1_description1

        如何覆盖文件中的现有文本?

        【讨论】:

          猜你喜欢
          • 2022-06-11
          • 2021-09-01
          • 2011-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多