java读写文件

weinideai 2011-05-30 01:30:08
我现在想把
Bufferedreader br=new Bufferedreader();
string line="";
while((line=br.readline())!=null)
{
//添加代码处
}
想把每次line的内容添加到一个文本文件里。
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒙面客 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jerry_bj 的回复:]
刚刚写了个测试、
public class TestData {
public static void ReadData() throws SQLException {
try {
FileReader read = new FileReader("E:/test1.txt");
BufferedReader br = new BufferedReader(read);
String ……
[/Quote]
++
JerryZhou_ 2011-05-30
  • 打赏
  • 举报
回复
刚刚写了个测试、
public class TestData {
public static void ReadData() throws SQLException {
try {
FileReader read = new FileReader("E:/test1.txt");
BufferedReader br = new BufferedReader(read);
String row;

File f = new File("E://test.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
PrintWriter pw = new PrintWriter(fos);
while ((row = br.readLine()) != null) {
pw.write(row);
pw.println();
pw.flush();
}
fos.close();
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
ReadData();
}
}
JAVAJKjiankeJK 2011-05-30
  • 打赏
  • 举报
回复
if (newFile.canWrite()) {
try {
FileOutputStream out = new FileOutputStream(newFile, true);
out.write(s.toString().getBytes());
out.close();
} catch (FileNotFoundException e) {
log.error("文件找不到" + e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
log.error("写文件失败");
e.printStackTrace();
return false;
}

s为变量既要输入的内容

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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