
1.png
1.IO流的分類
- 根據(jù)處理數(shù)據(jù)類型的不同分為:字符流和字節(jié)流
-
根據(jù)數(shù)據(jù)流向不同分為:輸入流和輸出流
2.png
2.節(jié)點(diǎn)流和處理流

3.jpg
3.常用例子
3.1基本分為4個(gè)步驟:
1.創(chuàng)建File對(duì)象
2.創(chuàng)建流對(duì)象
3.進(jìn)行讀/寫操作
4.關(guān)閉流對(duì)象
//抽象基類 節(jié)點(diǎn)流 緩沖流(處理流的一種)
//InputStream FileInputStream(字節(jié)流,圖片,視頻等) BufferedInputStream
//OutputStream FileOutputStream BufferedOutputStream
//Reader FileReader(字符流,處理txt等文本文件) BufferedReader
//Writer FileWriter BufferedWriter
public class FileTest {
//FileReader
@Test
public void test1() {
FileReader reader = null;
try {
//實(shí)例化文件
File file = new File("./helloworld.txt");
//創(chuàng)建讀入/寫入對(duì)象
reader = new FileReader(file);
//讀入/寫入
//int read = reader.read();
//方式一:
// int read;
// while ((read = reader.read()) != -1){
// System.out.print((char)read);
// }
//方式二:
int len;
char[] chars = new char[5];
while ((len = reader.read(chars)) != -1) {
//len表示讀取的字符數(shù),保存在chars數(shù)組中
for (int i = 0; i < len; i++) {
System.out.print(chars[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//FileWriter
@Test
public void test2() {
FileWriter fw = null; //如果append為true,則在原文件中追加,否則覆蓋文件
try {
File file = new File("./helloword2.txt");
fw = new FileWriter(file, false);
Writer a = fw.append('a');
fw.write("helloworld2!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//復(fù)制
@Test
public void test3() {
FileReader fr = null;
FileWriter fw = null;
try {
File file = new File("helloworld.txt");
File file1 = new File("copy_helloword.txt");
fr = new FileReader(file);
fw = new FileWriter(file1);
char[] chars = new char[1];
int len;
while ((len = fr.read(chars)) != -1) {
System.out.println(chars[0]);
fw.write(chars, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//圖片復(fù)制 FileOutputWriter,FileInputReader
@Test
public void test4() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File src = new File("./pic.png");
File dest = new File("./pic2.png");
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用緩沖流(內(nèi)部有個(gè)有容器,當(dāng)讀取到一次數(shù)量的數(shù)據(jù)時(shí),一次性寫入)
@Test
public void test5(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis;
FileOutputStream fos;
try {
File src = new File("./pic.png");
File dest = new File("./pic3.png");
//創(chuàng)建節(jié)點(diǎn)流
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
//使用處理流來(lái)包裹節(jié)點(diǎn)流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//只要關(guān)閉外層的流,內(nèi)層的流會(huì)自動(dòng)關(guān)閉
if(bis !=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用對(duì)象流
@Test
public void test6(){
try {
File file = new File("object.txt");
//序列化,將對(duì)象寫入文件中
//1.類必須實(shí)現(xiàn)Serializable接口
//2.需要一個(gè)UID,如果沒(méi)有的話,運(yùn)行時(shí)會(huì)自動(dòng)生成一個(gè),但是當(dāng)類改變時(shí),反序列化時(shí)會(huì)出現(xiàn)錯(cuò)誤
// private static final long serialVersionUID = -6849794470754667710L;
FileOutputStream fos = new FileOutputStream(file,false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("yang",18));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test7(){
try {
File file = new File("./object.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Person person = (Person) ois.readObject();
System.out.println(person);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Person implements Serializable{
String name;
int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4.自我總結(jié)
要進(jìn)行IO的操作,首要要確定是要進(jìn)行讀操作還是寫操作,讀的話就是Input/Reader,寫的話就是Output/Writer,其次確定操作的是字符還是字節(jié),字符流的話就是使用Reader,字節(jié)流的話就是使用Stream,同時(shí)在使用readf()進(jìn)行讀操作時(shí),使用char[] 或 byte[] 數(shù)組來(lái)存儲(chǔ)每一次讀入的數(shù)據(jù),然后使用write()將char[] 或 byte[]中的數(shù)據(jù)寫入指定文件中。
