java中如果用outputstream多次写,用inputStream进行读,只有第一次写的全部读到后面的就没了,怎么解决

主要是关于多次写,只能得到第一次写的内容

第1个回答  2018-02-02
package textcut; import java.io.*; //修改文件中指定行的内容 public class TxtCut { private File textFile; private BufferedWriter out; private int num = 3; //要修改的行 public TxtCut() { textFile = new File("e:/text.txt");// 指定源文件路径 try { //存储路径 如果不存回原文件 路径名可改为"e:/***.txt" out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File( "e:/text.txt")))); cut(textFile,num); out.close(); System.out.print("操作完成!"); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 读取文件内容并作适当修改 public void cut(File file,int n) { try { BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(file))); String str = in.readLine(); while(str!=null){ if(--n==0) out.write("Thank you!"+"\r\n"); else out.write(str + "\r\n"); str = in.readLine(); } in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new TxtCut(); } }
相似回答