Java IO原理
1. I/O是Input/Output的縮寫, I/O技術是非常實用的技術,用於 處理設備之間的數據傳輸。如讀/寫文件,網絡通訊等。
2. Java程序中,對於數據的輸入/輸出操作以“流(stream)” 的 方式進行。
3. java.io包下提供了各種“流”類和接口,用以獲取不同種類的 數據,並通過標準的方法輸入或輸出數據。
以下代碼塊中分別是文件流(也稱節點流)的字符輸入流、字符輸出流、字節輸入流、使用字符流複製文本、使用字節流複製圖片的演示
package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/*
FileReader :字符輸入流
read(): 返回的是讀取到的一個字符的int類型
read(char[] cbuf): 返回的int是數組裏元素的個數
*/
public class FileReaderDemo01 {
//第一種方式
@Test
public void test01(){ //這裏最好不要用拋出異常,因爲如果出現IOException異常,會出現流開啓的狀態下程序異常終結,而導致流沒有關閉!
FileReader fileReader = null;
try {
File file = new File("hello.txt");
fileReader = new FileReader(file); //這裏可能出現FileNotFoundException異常
int read; //記錄每次讀取字符的個數
//read()方法:返回一個讀入的一個字符,如果到達文件末尾,則返回-1
while((read = fileReader.read()) != -1){ //這裏可能出現IOException異常
System.out.print((char)read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) //如果上面出現FileNotFoundException異常而導致fileReader流沒有開啓,
fileReader.close(); //那麼這裏直接關閉就會出現錯誤,所以需要加以判斷
} catch (IOException e) {
e.printStackTrace();
}
}
}
//第二種方式
@Test
public void test02(){
FileReader fileReader = null;
try {
File file = new File("hello.txt");
fileReader = new FileReader(file);
char[] chars = new char[5];
int len;
while((len = fileReader.read(chars))!= -1){
//方式一:
for (int i = 0; i < len; i++) {
System.out.print(chars[i]);
}
//方式二:
// String str = new String(chars,0,len);
// System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileReader != null)
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/*
FileWriter :字符輸出流
兩個構造器
----FileWriter(file)
----FileWriter(file,true)
方法
----write()
*/
public class FileWriterDeom01 {
@Test
public void test01(){
FileWriter fw = null;
try {
//創建File對象 如果writer.txt文件不存在,則會自動爲我們創建,不會報錯
File file = new File("writer.txt");
//創建FileWriter對象 如果想要在原有文本上添加字符而不是覆蓋原有字符,需調用FileWriter(file ,true/false)方法
//調用FileWriter(file) 方法的話默認爲false,爲覆蓋原有文本; 選擇true爲添加操作
fw = new FileWriter(file,true);
//使用write()方法寫入字符
fw.write("I have a dream!\n");
fw.write("You need a dream!\n");
//資源關閉
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/*
字節輸入流(FileInputStream)
操作方式基本與字符流一樣
使用字節流讀取文本數據時,可能出現中文亂碼,因爲一箇中文是三個字節
但是如果使用字節流實現文本的複製,也是可行的!
*/
public class FileInputStreamDemo01 {
@Test
public void test01() throws IOException {
FileInputStream fis = null;
try {
File file = new File("inputStream.txt");
fis = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1){
String str = new String(buffer,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null)
fis.close();
}
}
}
package com.Lindom.IO流;
import org.junit.Test;
import java.io.*;
/*
文本複製:
這裏是結合字符輸入流FileReader和字符輸出流FileWriter,完成了對文本的複製操作
*/
public class FileCopyDemo {
@Test
public void test(){
FileReader fr = null;
FileWriter fw = null;
try {
File file = new File("copy.txt");
File file1 = new File("copyTo.txt");
fr = new FileReader(file);
fw = new FileWriter(file1,true);
char[] cbuf = new char[5];
int len = 0;
while((len = fr.read(cbuf)) != -1){
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.Lindom.IO流;
import org.junit.Test;
import java.io.*;
/*
使用FileInputStream(字節輸入流)和FileOutputStream(字節輸出流)完成圖片的複製
*/
public class FileCopyDemo02 {
@Test
public void test01(){
FileInputStream fis = null;
FileOutputStream fop = null;
try {
File file = new File("Lisa01.jpg");
fis = new FileInputStream(file);
fop = new FileOutputStream("Lisa.jpg");
byte[] bytes = new byte[5];
int len;
while ((len = fis.read(bytes)) != -1){
fop.write(bytes,0,len);
}
System.out.println("圖片複製成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null)
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}