JAVA io流筆記05 增強流
package FileText;
//位元組流使用byte 陣列!!!
//字元流使用char 陣列!!!
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
//增強
public class BufferedFile05 {
public static void main(String args[]){
BuffW();
BuffF();
}
public static void BuffW(){
//BufferedReader 緩衝字元輸入流
File file = new File("D:/text/dd/text.txt");
File file2 = new File("D:/text/dd/Buftext.txt");
try {
BufferedReader read = new BufferedReader(new FileReader(file));
char[] cha = new char[1024];
int len = 0;
if(-1!=(len=read.read(cha))){
BufferedWriter write = new BufferedWriter(new FileWriter(file2));
String str = new String(cha,0,len);
write.write(str, 0, len);
System.out.println(str);
write.flush();
write.close();
read.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void BuffF(){
//BufferedInputStream 緩衝位元組輸入流
File file = new File("D:/text/dd/text.txt");
File file2 = new File("D:/text/dd/Buftext2.txt");
try {//Inputstream 轉換流
InputStreamReader input = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)),"utf-8");
char[] byt = new char[1024];
int len = 0;
if(-1!=(len=input.read(byt))){
String strs =new String("秉忠貞之志,守謙退之節");
byte[] newbyt = new byte[1024];
newbyt = strs.getBytes();
String str = new String(byt,0,len);
System.out.println(str);//從檔案中讀取的
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
out.write(newbyt, 0, len); //出現亂碼。等待後續改進//後續轉換流已經解決問題
out.flush();
out.close();
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}