【问题标题】:Appending a string on the specific place in txt file - Java在 txt 文件中的特定位置附加一个字符串 - Java
【发布时间】:2021-11-17 07:11:31
【问题描述】:

我正在尝试将字符串(链接)附加到特定位置的 txt 文件(在“链接:”所在的行中),以获取文件中的行,如“链接:www.link.something”。我正在使用下一个代码,但我的逻辑不起作用。

if(file.getName().equals(filename+".txt")) {
   link = line;
   BufferedReader br;
   BufferedWriter bw;
   boolean no=false;
   String lineE;
   String data="Link:";
   String lessonPath=link;
                                                
                                                
   br = new BufferedReader(new FileReader(file));
    
    
   while((lineE =br.readLine()) !=null){
      if(!no){
         data=line;
         no=true;
      }else{
         data = data+"\n"+lineE;
      }   
   }
   bw = new BufferedWriter(new FileWriter(file));
   bw.write(data+"\n"+lessonPath);
   System.out.println(data+lessonPath);
   bw.flush();
   bw.close();  
   br.close(); 
}

【问题讨论】:

    标签: java string loops file txt


    【解决方案1】:

    如果您可以修改文本文件,则可以使用StringSubstitutor 替换模板。

    【讨论】:

    • 我试图通过将文本文件的现有行与链接字符串连接起来来做到这一点
    • 其他解决方案?
    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2023-03-26
    • 2021-09-04
    相关资源
    最近更新 更多