package io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOStreamDemo {
public static void main(String[] args) {
//
/*FileInputStream fi = null;
FileOutputStream fo =null;
try {
fi= new FileInputStream("f:\\s.txt");
fo= new FileOutputStream("f:\\t.txt");
} catch (IOException e) {
e.printStackTrace();
}
try {
int in=0;
//从字节byte读出来返回int类型;
//读出来后直接写入到文本中,不需要用字节缓冲区接收
//而且写入时字节自己会自动转换成字符
while((in=fi.read())!=-1){
fo.write(in);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fo.close();
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("-----------------------------------------");
//利用缓冲区读写字节流
*/ FileInputStream fs = null;
BufferedInputStream bi= null;
FileOutputStream ft = null;
BufferedOutputStream bo = null;
try {
fs = new FileInputStream("f:\\t.txt");
bi = new BufferedInputStream(fs);
ft = new FileOutputStream("f:\\g.txt");
bo = new BufferedOutputStream(ft);
} catch (Exception e) {
e.printStackTrace();
}
byte[] byt = new byte[1024];
int len = 0;
try {
while((len=bi.read(byt))!=-1){
bo.write(byt,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(fs!=null&&ft!=null){
try {
bo.close();
bi.close();
}
catch (Exception e) {
e.printStackTrace();
}}
//缓冲区读写字符流
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("f:\\g.txt");
fw = new FileWriter("f:\\c.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
} catch (Exception e) {
e.printStackTrace();
}
try {
String line = null;
while((line=br.readLine())!=null){
bw.write(line);
}
} catch (Exception e2) {
e2.printStackTrace();
}finally{
if(fr!=null&&fw!=null){
try {
br.close();
bw.close();
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
}
}
}
|
|