String htmlName="index.html"; String relaPath="" Writer out = null; String path = ServletActionContext.getServletContext().getRealPath("/"); File fileName = new File(path + relaPath+"index.html"); try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), "utf-8")); out.write("追加的东西"); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); }
Java代码
1.
可以先把文件里面的东西读出来,放在StringBuffer里面,然后在追加
如:
File f=new File; try { InputStream a = new FileInputStream(f); BufferedReader ins = new BufferedReader(new InputStreamReader(a)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = ins.readLine()) != null) { buffer.append(line); System.out.println(buffer.toString()); } buffer.append("后面的"); FileOutputStream out = new FileOutputStream(f); out.write(buffer.toString().getBytes()); } catch(Exception e) { e.printStackTrace(); }
2.
Java代码 File f=new File("D:/a.txt"); try { FileOutputStream out = new FileOutputStream(f,true); out.write("something".getBytes()); } catch(Exception e) { e.printStackTrace(); }
3.
FileOutputStream接收两个参数,后面个boolean决定是追加还是覆盖
Java代码
PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(rcFile, true)), true); out.println(sb.toString());
第一个true代表append,第二个代表autoflush