一:字节流
public void test() throws Exception {
File file = new File("D:\\zsc\\ccc.mp4");
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream(file);
byte by[] = new byte[1024];
long count = file.length()%1024 == 0 ? file.length()/1024 : file.length()/1024 + 1;
for(int i=0;i<count;i++){
fis.read(by);
fos.write(by);
fos.flush();
fos.close();
fis.close();
}
}
二:字符流
public void test() throws Exception {
File file1 = new File("D:\\zsc\\ccc.mp4");
String file2="D:\\zsc"+File.separator+"ccc.mp4";
String str="wogwpgwpgfgowg";
//输入流
FileReader fr=new FileReader(file1);
BufferedReader bfr=new BufferedReader(fr);
//输出流
FileWriter fw=new FileWriter(file2,true); //不覆盖原有数据
BufferedWriter bfw=new BufferedWriter(fw);
bfr.readLine();
bfr.read();
char ch[] = new char[1024];
long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
for(int i=0;i<count;i++){
bfr.read(ch);
}
bfw.write(str);
bfw.flush();
}
三:使用字节流读取二进制文件
public void test() throws Exception {
File file1 = new File("D:\\zsc\\ccc.mp4");
String file2="D:\\zsc"+File.separator+"ccc.mp4";
String str="wogwpgwpgfgowg";
//输入流
FileInputStream fis=new FileInputStream(file1);
DataInputStream dis=new DataInputStream(fis);
//输出流
FileOutputStream fos=new FileOutputStream(file2);
DataOutputStream dos=new DataOutputStream(fos);
dis.read();
//读取出来的是整数
dis.readInt();
byte by[] = new byte[1024];
long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
for(int i=0;i<count;i++){
dis.read(by);
}
//把数据写入二进制文件
dos.write(1);
dos.write(by);
dos.flush();
}
四:设置流的输入输出格式
public void test() throws Exception {
File file1 = new File("D:\\zsc\\ccc.mp4");
String file2="D:\\zsc"+File.separator+"ccc.mp4";
String str="wogwpgwpgfgowg";
//输入流
FileInputStream fis=new FileInputStream(file1);
//设置输入格式
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
//输出流
FileOutputStream fos=new FileOutputStream(file2,true); //不覆盖原有数据
//设置输出格式
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
isr.read();
char ch[] = new char[1024];
long count = file1.length()%1024 == 0 ? file1.length()/1024 : file1.length()/1024 + 1;
for(int i=0;i<count;i++){
isr.read(ch);
}
osw.write(str);
osw.flush();
}
五:键盘输入流
public void test() throws Exception {
File file1 = new File("D:\\zsc\\ccc.mp4");
String file2="D:\\zsc"+File.separator+"ccc.mp4";
String str="wogwpgwpgfgowg";
//键盘输入
InputStreamReader isr=new InputStreamReader(System.in,"UTF-8");
BufferedReader bfr=new BufferedReader(isr);
//输出
FileWriter fw=new FileWriter(file1);
BufferedWriter bw=new BufferedWriter(fw);
bfr.readLine();
bfr.read();
bw.write(str);
bw.flush();
}
六:序列化和反序列化
注意:
1,只有实现了Serializable接口的类的对象才能实序列化
2,如果一个可序列化的类有多个父类,则这些父类要么也是可序列化的,要么有无参的构造器
public void test() throws Exception {
File file1 = new File("D:\\zsc\\ccc.mp4");
String file2="D:\\zsc"+File.separator+"ccc.mp4";
Bbs_User user=new Bbs_User();
//序列化,输出流
FileOutputStream fos=new FileOutputStream(file2);
ObjectOutputStream oos=new ObjectOutputStream(fos);
//反序列化,输入流
FileInputStream fis=new FileInputStream(file1);
ObjectInputStream ois=new ObjectInputStream(fis);
oos.writeObject(user);
oos.flush();
ois.readObject();
}