Topic: [求助]几个读写存档的问题,应该不是很难,有兴趣的进来看看,谢谢!!!

  Print this page

1.[求助]几个读写存档的问题,应该不是很难,有兴趣的进来看看,谢谢!!! Copy to clipboard
Posted by: 庄敏慎逊让
Posted on: 2004-05-03 21:30

1.我有一个文件words.txt,这个文件里本来有一些数据.运行下面的代码后,words.txt里面以前的信息都没有了.请问如何可以不影响words.txt里以前的内容?
File save = new File("words.txt");
FileWriter in = new FileWriter(save);
in.writer("Ha Ha Ha");

2.有name , id , number这3个变量,把这3个变量写入文件,怎么做可以让这3个变量每个变量占用一行,也就是说存入文件时输入完name后自动换行,第二行输入id,再自动换行,第三行输入number

3.把文档中原有信息全部清空怎么做?

谢谢!!!

2.Re:[求助]几个读写存档的问题,应该不是很难,有兴趣的进来看看,谢谢!!! [Re: 庄敏慎逊让] Copy to clipboard
Posted by: safe
Posted on: 2004-05-04 01:32

1.File save = new File("words.txt", true);
2.use BufferedWriter.newLine()

3.Re:[求助]几个读写存档的问题,应该不是很难,有兴趣的进来看看,谢谢!!! [Re: 庄敏慎逊让] Copy to clipboard
Posted by: haoqingshi
Posted on: 2004-05-04 05:15

假设原来words.txt文件中写有数据:
对于第一个问题:
import java.io.*;
public class ApendFile{
  public static void main(String args[]) throws Exception{
    FileWriter fw=new FileWriter("c:\\words.txt",true);
    fw.write("dajihao");
    fw.flush();
}
}

第二个问题:
String name="haoqingshi";
FileWriter fw=new FileWriter("c:\\words.txt",true);
BufferedWriter out = new BufferedWriter(fw);
out.newLine();
out.write(name);
out.close();

4.Re:[求助]几个读写存档的问题,应该不是很难,有兴趣的进来看看,谢谢!!! [Re: 庄敏慎逊让] Copy to clipboard
Posted by: huangjiacai
Posted on: 2004-05-04 12:19

第一个问题楼上的朋友已经解决了

第二个问题我觉的定义一个类(其属性中有id,name,number)来做更好 呀,既然是oop就应该更用用对象,不然就不要用oop了
public class customer{
private String id;
private String name;
private Sting number;

public getid(String id){
return id;
}
public setid(Sting id){
this.id=id;
}
public getname(String name){
return name;
}
public setname(String name){
this.name=name;
}
public getnumber(String number){
return number;
}
public setnumber(String number){
this.number=number;
}
public toString(){ //是重载toString方法
BuffString buf=new BuffSting();
buf.append(id);
buf.append("\n");
buf.append(name);
buf.append("\n");
buf.append(number);
}
}
在main中可以用ObjectOutputStream写入words.txt了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923