以下内容来自marschen的java4android视频教程
本集主要内容
1.大文件的读写方法
2.字符流的使用方法
总结,大文件读写,创建一个数组,用一个循环,每次从文件中读取一部分放入数组
这个循环当读写到文件尾部的时候,停.
最后,要记得
file.close();
import java.io.*;
class Test{
public static void main(String argsp[]){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("E:/marschen/fileop/form.txt");
fos = new FileOutputStream("E:/marschen/fileop/to.txt");
byte [] buffer = new byte[1024];
while(true){
int temp = fis.read(buffer,0,buffer.length);
if(-1 == temp){
break;
}
fos.write(buffer,0,temp);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
//字符流:读写文件时,以字符为基础
//字符输入流:Reader <----FileReader int read(char [] c,int off,int len);
//字符输出流:Writer <----FileWriter int write(char [] c,int off,int len);
import java.io.*;
public class TestChar{
public static void main(String args[]){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("E:/marschen/fileop/form.txt");
fw = new FileWriter("E:/marschen/fileop/to.txt");
char [] buffer = new char[100];
int temp = fr.read(buffer,0,buffer.length);
fw.write(buffer,0,temp);
/*for(int i = 0; i < buffer.length; i++)
{
System.out.println(buffer[i]);
}*/
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
fr.close();
fw.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}