用java中的io流写入文件如何避免覆盖原来的内容?求代码
2013-08-13
展开全部
用这个方法public FileOutputStream(String name,boolean append)throws FileNotFoundException,将逻辑值append设为false时,写入的字节数据覆盖文本原来的内容,为ture是即追加到原来文本的末尾。覆盖原文本内容如:FileOutputStream x=new FileOutputStream("文件全路径",false);throws FileNotFoundException为抛出异常。
2013-08-13
展开全部
FileWriter fileWriter = new FileWriter(fileName, true);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-13
展开全部
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("c:\\1.txt",false));
BufferedWriter bw = new BufferedWriter(osw);
String s = null;
s = br.readLine();
while (s != null) {
bw.write(s);
bw.newLine();
/**
* 添加的代码
*/
bw.flush();
System.out.println(s.toUpperCase());
s = br.readLine();
}
br.close();
bw.close();
} catch (IOException e) { }
}
// TODO Auto-generated method stub
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("c:\\1.txt",false));
BufferedWriter bw = new BufferedWriter(osw);
String s = null;
s = br.readLine();
while (s != null) {
bw.write(s);
bw.newLine();
/**
* 添加的代码
*/
bw.flush();
System.out.println(s.toUpperCase());
s = br.readLine();
}
br.close();
bw.close();
} catch (IOException e) { }
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询