文件操作相关!!

rookie001 2009-07-14 07:16:26
1.文件操作中,能不能直接修改某个数据,否则怎么处理比较好
2.先往某个文件写一些记录,记录内容没有固定长度(字符串和数字)
然后在读这个文件上的记录分别赋值到某个变量,怎么处理比较好
...全文
55 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigbug9002 2009-07-14
  • 打赏
  • 举报
回复
import java.io.*;

public class Test3{
public static void main(String[] args) throws IOException{
writeSomeToFile("result.rst","1 + 2 = 3");
String s=readLineFromFile("result.rst");
String[] strs=s.split("\\s+"); //split whith white charactor
for(int i=0;i<strs.length;i++){
System.out.println(strs[i]);
}
}
public static void writeSomeToFile(String fileName,String some) throws IOException{
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)),"UTF-8"));
bw.write(some,0,some.length());
bw.newLine();
bw.flush();
bw.close();
}
public static String readLineFromFile(String fileName) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
String s=null;
s=br.readLine();
br.close();
return s;
}

}


这个程序是前些回答某贴的时,我写的.你看看,如果有其它需要,你可以自己写程序,多查一下API,?
qingyunzhuimeng 2009-07-14
  • 打赏
  • 举报
回复
你可以用正则表达示试一试,我想就可以解决你的问题
rookie001 2009-07-14
  • 打赏
  • 举报
回复
求大侠给示例[Quote=引用 2 楼 bigbug9002 的回复:]
1.可以,只是你要知道改的是那个数,要用RadomAccessFile类,这是一个字节流类.
2.以转行符分隔记录,以空格或逗号分隔字段。
[/Quote]
gzx287253268 2009-07-14
  • 打赏
  • 举报
回复
路过··
bigbug9002 2009-07-14
  • 打赏
  • 举报
回复
1.可以,只是你要知道改的是那个数,要用RadomAccessFile类,这是一个字节流类.
2.以转行符分隔记录,以空格或逗号分隔字段。
keivin01 2009-07-14
  • 打赏
  • 举报
回复
用XMl或者properties呗

62,620

社区成员

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

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