java IO流複製文字,視訊檔案
阿新 • • 發佈:2019-02-14
- java IO流的分類:按照流向可分為輸入流和輸出流,按照資料型別可分為位元組流和字元流。
- IO流的適用場景和效率:在處理音訊,視訊,圖片這些檔案時一般使用位元組流,而使用緩衝位元組流(BufferedInputStream和BufferedOutputStream)會比檔案位元組流(FileInputStream和FileOutputStream)的效率高;在處理文字檔案時一般使用字元流,而使用緩衝字元流(BufferedReader和BufferedWriter)會比檔案字元流(FileReader和FileWriter)的效率高。
下面用一張圖來概括IO流體系:
用位元組流複製視訊並比較效率
package com.yixun.cn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class CopyPicture {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv"));
fos = new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv" ));
byte[] b = new byte[64];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("檔案位元組流複製視訊花費時間為:" + (endTime - startTime));
}
@Test
public void test() {
long startTime = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv")));
bos = new BufferedOutputStream(
new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv")));
byte[] b = new byte[64];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("緩衝位元組流複製視訊花費時間為:" + (endTime - startTime));
}
}
5. 用字元流複製文字檔案並比較效率
package com.yixun.cn;
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;
import java.io.OutputStreamWriter;
import org.junit.Test;
public class CopyText {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File(
"C:\\Users\\mayu\\Desktop\\xxx.txt"));
fw = new FileWriter(new File("C:\\Users\\mayu\\Desktop\\test\\xxx.txt"));
char[] c = new char[64];
int len = 0;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("檔案字元流複製文字花費時間為:" + (endTime - startTime));
}
@Test
public void test() {
long startTime = System.currentTimeMillis();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"C:\\\\Users\\\\mayu\\\\Desktop\\\\xxx.txt"))));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
new File("C:\\Users\\mayu\\Desktop\\test\\x"))));
char[] c = new char[64];
int len = 0;
while ((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("緩衝字元流複製文字花費時間為:" + (endTime - startTime));
}
}