JAVA程序中怎么把信息存到文件中

[复制链接]
查看11 | 回复1 | 2009-6-24 10:34:39 | 显示全部楼层 |阅读模式
import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;/** * 实时写入文件 ** @author Administrator */public class Inquest {
public static void main(String[] args ) { BufferedReader in = null; PrintWriter out = null; try {
in = new BufferedReader(new InputStreamReader(System.in));// 接受控制台输入
File file = new File("d:/abc.txt");// 输出到文件
out = new PrintWriter(file);
String str = null;
// 用户输入quit则退出程序
while (!(str = in.readLine()).equalsIgnoreCase("quit")) {out.println(str);out.flush();
} } catch (Exception e) {
e.printStackTrace(); } finally {
try {if (out != null)
out.close();if (in != null)
in.close();
}
catch (IOException ie) {ie.printStackTrace();
} }
}}
回复

使用道具 举报

千问 | 2009-6-24 10:34:39 | 显示全部楼层
用ObjectOutputStream将一个类的对象写到文件中,这个类要实现Serializable接口。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行