java的写文件操作

muroi 2006-09-18 03:00:34
向一个txt文件写进一个字符窜,然后再从源文件读一个,在写到目的文件里,要求加到前面那个字符窜的后面,不能覆盖前面的。请问该怎么写啊。。。。
...全文
315 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jim801716 2006-09-20
  • 打赏
  • 举报
回复
谢谢楼上的,比看书快多了,楼主该结贴了
ddviplinux 2006-09-18
  • 打赏
  • 举报
回复
java IO流类库非常重要,里面用到了许多设计模式IO在J2EE里也是比较重要的,在实际的项目里也用
的比较多,楼主要多修炼一下内功呀!
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;

;
public class FileWriterDemo {
//读文件的方法,File参数是读入的源文件对象
public void readTxt(File file) {
//声明一个缓冲输入字符流对象
BufferedReader br = null;
try {
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String input_message = br.readLine();
while (input_message != null) {
System.out.println(input_message);
input_message = br.readLine();
}
}
catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
finally {
try {
if (br != null) {
br.close();
}
}
catch (IOException ex1) {
System.out.println(ex1.getMessage());

}
}

}
//写入文件的方法,File参数是写入源文件的对象
public void writeTxt(File file) {
//声明一个缓冲输出流
BufferedWriter bw = null;
//声明一个控制台缓冲输入流,动态写入信息。
BufferedReader br = null;
try {
//构造文件输出字符流,第二参数为true是向文件追加信息的意思
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
//从控制台输入信息
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
String output_message = br.readLine();
while (!output_message.equals("bye")) {
//把控制台的信息,写入到输出字符流
bw.write(output_message);
bw.newLine();
bw.flush();
output_message = br.readLine();
}
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
try {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

}

public static void main(String args[]) {
//测试方法
FileWriterDemo fwd = new FileWriterDemo();
File file = new File("f:/test.txt");
System.out.println("===请输入写入文件的信息===");
fwd.writeTxt(file);
System.out.println("===输出写入文件的信息===");
fwd.readTxt(file);
}

}
wddodo 2006-09-18
  • 打赏
  • 举报
回复
就是追加呗,api上有。
FileOutputStream

public FileOutputStream(String name,
boolean append)
throws FileNotFoundException

Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.

First, if there is a security manager, its checkWrite method is called with name as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
Since:
JDK1.1
See Also:
SecurityManager.checkWrite(java.lang.String)

imA 2006-09-18
  • 打赏
  • 举报
回复
利用FileWriter的append方法
具体的使用看sun的jdk文档

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧