一、字节流
二、字符流
try {
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("java_new.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
char input[] = new char[1000];
int length = 0;
while((length = isr.read(input)) != -1){
osw.write(input,0 ,length);
}
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("copy finished !"+length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
三、字符缓冲流
try {
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("java_buffer.txt");
InputStreamReader isr = new InputStreamReader(fis, "gbk");
OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(osw);
String input;
while((input = br.readLine()) != null){
pw.println(input);
}
br.close();
isr.close();
fis.close();
pw.close();
osw.close();
fos.close();
System.out.println("缓冲字符流方式读取完毕!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
四、字节缓冲流
try {
FileInputStream fis = new FileInputStream("Sleep.mp3");
FileOutputStream fos = new FileOutputStream("sleep_new.mp3");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte input[] = new byte[1000];
int count = 0 ;
while(bis.read(input) != -1){
count++;
bos.write(input);
}
bos.close();
fos.close();
bis.close();
fis.close();
System.out.println("总共读取了 "+count+"次");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}