当前页面: 开发资料首页 → J2SE 专题 → 请教一个文件IO的问题
请教一个文件IO的问题
摘要: 请教一个文件IO的问题
我有一个文件,其中的内容是
1 12
3 3 4 5
7 8 9 0
也就是文件中有若干行,每行都有若干个数,我想通过编写java代码,将这些数字读到数组中,经过处理后再输出到另一个文件中,我的问题是该声明什么样的input和output呢,有那么多的stream,还有各种的reader,writer,弄的我都晕了,只好上来请教各位了
读写文本文件,强烈推荐reader,writer!!!!!!!
FileStream.
有二种文件
一种是字符编码的,如文本文件、xml文件等,一般使用reader,writer。
剩下的是二进制编码的,使用inputStream和outputStream,其他的类只是提供了更多的功能或更好的性能,可以选择使用。
谁能举个例子呢,要读写我所说的那文件应该声明什么reader呢?
xxx reader = new xxx(new yyy());
int i = reader.xxmethod();
int[][] a= new int[3][4];
try{
File f = new File(-#34;D://aa.txt-#34;);
BufferedReader br = new BufferedReader(new FileReader(f));
String s;
String[] s1;
int i=0;
while((s = br.readLine())!=null) {
s1 = s.split(-#34;//s-#34;);
for(int j=0; j-#60;s1.length; j++) {
a[i][j] = Integer.parseInt(s1[j]);
}
i++;
}
上面的没粘贴完整,缺少异常处理,这个是完整的读入代码
try{
File f = new File(-#34;D://aa.txt-#34;);
BufferedReader br = new BufferedReader(new FileReader(f));
String s;
String[] s1;
int i=0;
while((s = br.readLine())!=null) {
s1 = s.split(-#34;//s-#34;);
for(int j=0; j-#60;s1.length; j++) {
a[i][j] = Integer.parseInt(s1[j]);
}
i++;
}
}catch(Exception e){
e.printStackTrace();
}
晕,上面的又少了数组的定义。。。。。
这个保证完整,华丽的三连贴,HOHO~~~~
int[][] a= new int[3][4];
try{
File f = new File(-#34;D://aa.txt-#34;);
BufferedReader br = new BufferedReader(new FileReader(f));
String s;
String[] s1;
int i=0;
while((s = br.readLine())!=null) {
s1 = s.split(-#34;//s-#34;);
for(int j=0; j-#60;s1.length; j++) {
a[i][j] = Integer.parseInt(s1[j]);
}
i++;
}
}catch(Exception e){
e.printStackTrace();
}