java IO流的基礎操作
一、流的分類
- 按操作資料單位不同分為:位元組流(8 bit),字元流(16 bit)
- 按資料流的流向不同分為:輸入流,輸出流
- 按流的角色的不同分為:節點流,處理流
(抽象基類) |
位元組流 |
字元流 |
輸入流 |
InputStream |
Reader |
輸出流 |
OutputStream |
Writer |
1.Java的IO流共涉及40多個類,實際上非常規則,都是從如上4個抽象基類派生的。
2.由這四個類派生出來的子類名稱都是以其父類名作為子類名字尾。
操作文字檔案用字元流,操作非文字檔案用位元組流。
二、基礎掌握
FileInputStream、FileOutputStream 、FileReader、FileWriter
BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter(基於FileInputStream、FileOutputStream 、FileReader、FileWriter的操作,效率要高一些)
FileInputStream:位元組流,節點流,輸入流
@Test
public void testFileInputStream() {
File file = new File("helloworld.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int i;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream:位元組流,節點流,輸出流
@Test
public void testFileOutputStream() {
File file = new File("hello.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte b[] = "i love china i love the world!".getBytes();
fos.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
FileReader、FileWriter
/**
* 使用字元流複製檔案
* 文字使用
* @param src原始檔路徑
* @param des目標檔案路徑
*/
public void copyFileByReaderWriter(String src, String des) {
File inFile = new File(src);
File outFile = new File(des);
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(inFile);
fw = new FileWriter(outFile);
char[] b = new char[1024];
int len;
while ((len = fr.read(b)) != -1) {
fw.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
FileInputStream、FileOutputStream
/**
* 使用節點流實現複製檔案
* 非文字使用
* @param src原始檔路徑
* @param des目標檔案路徑
*/
public void copyFileByInputOutput(String src, String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.out.println("FileInoputOutputStream spend time=="+(end-start));
}
-----------------------------------------------------------------------------------------------------------------------------------------------
BufferedInputStream、BufferedOutputStream
/***
* 使用bufferedInputOutputStream實現複製
* 適用非文字操作,效率比FileInputStream、FileOutputStream要快
* @param src
* @param des
*/
public void copyFileByBufferedInputStreamOutputStream(String src,String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bis!=null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long end=System.currentTimeMillis();
System.out.println("BufferedInputOutputStream spend time=="+(end-start));
}
BufferedReader、BufferedWriter
/**
* 使用BufferedReader、BufferedWriter實現檔案的讀寫
* @param src
* @param des
*/
public void copyFileByBufferedReaderWriter(String src,String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(inFile);
fw = new FileWriter(outFile);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char[] b = new char[1024];
int len;
while ((len = br.read(b)) != -1) {
bw.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
if(br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end=System.currentTimeMillis();
System.out.println("BufferedReaderWriter spend time=="+(end-start));
}
通過測試BufferedInputStream、BufferedOutputStream操作非文字效率最高
/**
* 測試視訊複製
*/
@Test
public void testCopyFileToVoide() {
String src = "E:\\123.wmv";
String f_des = "E:\\f_v456.wmv";
String b_des="E:\\b_v456.wmv";
String brw_des="E:\\brw_v456.wmv";
copyFileByInputOutput(src, f_des);//FileInoputOutputStream spend time==1221
copyFileByBufferedInputStreamOutputStream(src, b_des);//BufferedInputOutputStream spend time==267
copyFileByBufferedReaderWriter(src, brw_des);//copyFileByBufferedReaderWriter spend time==3750
}