0

我需要在现有文本文件中添加标题行,有没有办法在不影响任何其他文件格式或结构的情况下做到这一点。

4

1 回答 1

0

读取文件,添加标题并保存文件:

try{
    BufferedReader in = new BufferedReader(new FileReader(yourFile));
    String header = "Your Header";
    String content = "";
    while (in.ready()) {
      content += in.readLine();
    }
    in.close();

    String output = header + System.getProperty("line.separator") + content;

    FileWriter fstream = new FileWriter(YourOutputFile);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(output);  
    out.close();

  }catch (IOException e){
    System.err.println("Error: " + e.getMessage());
  }
于 2013-03-22T06:10:29.553 回答