package FourWaysToGetText;
import java.io.*;
public class FourWays {
/**
* 運用四種方式讀寫文件,按讀取文件大小分為一次讀取一個字節和一次讀取一個字節數組,
* 按讀取方式可以分為基本字節流和字節緩沖流(其實在使用上區別不大,只是字節緩沖流需要用到基本字節流對象)
* 自己緩沖的讀取效率還是比基本字節流高的,大家可以讀取大一些的文件,例如圖片或大一點的文本,
* 可以比較他們在內存中所需要的運行時間。
*/
public static void main(String[] args) throws IOException {
Long start1 = System.currentTimeMillis();
method4();
Long finish1 = System.currentTimeMillis();
System.out.print("基本字節流復制文本所耗時間\t(ms)");
System.out.print(finish1 - start1);//獲得運行所需時間
}
//字節緩沖流一次讀寫一個字節數組
public static void method4() throws IOException {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\itcast\\creat.txt"));
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("Gzy_BasicJava\\out5.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(len);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
//字節緩沖流一次寫入一個字節
public static void method3() throws IOException {
BufferedInputStream b1 = new BufferedInputStream(new FileInputStream("E:\\itcast\\creat.txt"));
BufferedOutputStream BO = new BufferedOutputStream(new FileOutputStream("Gzy_BasicJava\\out5.txt"));
int len;
while ((len = b1.read()) != -1) {
BO.write(len);
}
b1.close();
BO.close();
}
//基本字節流一次讀寫一個字節
public static void method1() throws IOException {
FileInputStream in1 = new FileInputStream("E:\\itcast\\creat.txt");
FileOutputStream out1 = new FileOutputStream("Gzy_BasicJava\\out1.txt");
int len;
while ((len = in1.read()) != -1) {
out1.write(len);
}
in1.close();
out1.close();
}
//基本字節流一次讀寫一個字節數組
public static void method2() throws IOException {
FileInputStream in2 = new FileInputStream("E:\\itcast\\creat.txt");
FileOutputStream out2 = new FileOutputStream("Gzy_BasicJava\\out2.txt");
int len;
byte[] bytes = new byte[1024];
while ((len = in2.read(bytes)) != -1) {
out2.write(len);
}
out2.close();
in2.close();
}
}