------- <a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">android教育訓練</a>、<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">java教育訓練</a>、期待與您交流! ----------
IO流知識點總結
一、IO流
不同的輸入輸出裝置之間的資料傳輸抽象表述為“流”,IO流用來處理裝置之間的資料傳輸。Java對資料的操作是通過流的方式。Java用于操作流的對象都在IO包中。
流按操作資料分為兩種:位元組流與字元流。
位元組流又分為輸入位元組流InputStream、輸出位元組流OutputStream。
字元流又分為輸入字元流Reader、輸出字元流Writer。
二、位元組流
位元組流的概念:IO流中針對位元組輸入輸出提供的一系列的流,統稱為位元組流。
位元組流:
輸入流:
InputStream(抽象類)
int read()
int read(byte[] bys)
FileInputStream(常用基本流)
BufferedInputStream
輸出流:
OutputStream(抽象類)
write(int by)
write(byte[] bys,int index,int len)
FileOutputStream(常用基本流)
BufferedOutputStream
案例一:
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
// 建立位元組輸入流對象
FileInputStream fis = new FileInputStream("b.txt");
// 調用讀取資料的方式,并顯示
// 方式1
// int by = 0;
// while ((by = fis.read()) != -1) {
// System.out.println(by);
// }
// 方式2
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
// 釋放資源
fis.close();
}
}
案例二:
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// 建立位元組輸出流對象
// FileOutputStream fos = new FileOutputStream("a.txt",true);
FileOutputStream fos = new FileOutputStream("a.txt");
// 調用寫資料的方法
// 寫入一個位元組
// fos.write(97);
// fos.write(98);
// fos.write(99);
// fos.flush();
// 寫一個位元組數組
// byte[] bys = { 97, 98, 99, 100, 101 };
byte[] bys = "abcde".getBytes();
// fos.write(bys);
// 寫一個位元組數組的一部分
fos.write(bys, 0, 2);
// 釋放資源
fos.close();
}
}
案例三:
public class CopyMP3 {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("d:\\Pink.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("copy.mp3"));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
案例四:
public class BufferedStreamDemo {
public static void main(String[] args) throws IOException {
// 寫資料
// BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
// for (int x = 0; x < 5; x++) {
// bw.write("hello" + x);
// // bw.write("\r\n");
// bw.newLine();
// bw.flush();
// }
//
// bw.close();
// 讀取資料
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
三、字元流
字元流的概念:IO流中針對字元輸入輸出提供的一系列的流,統稱為字元流。
字元流:
輸入流:
Reader(抽象類)
int read()
int read(char[] chs)
FileReader(常用基本流)
BufferedReader
String readLine()
輸出流:
Writer(抽象類)
write(int ch)
write(char[] chs,int index,int len)
FileWriter(常用基本流)
BufferedWriter
write(String line)
void newLine()
案例一:
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
// 建立字元輸出流對象
// Writer w = new FileWriter(); // 多态
// File file = new File("a.txt");
// if(!file.exists()){
// file.createNewFile();
// }
// FileWriter fw = new FileWriter(file);
FileWriter fw = new FileWriter("a.txt");
// 調用寫資料的功能
// public void write(String str)
fw.write("hello,io,我來了。");
// 重新整理緩沖區
// public void flush()
fw.flush();
// 釋放資源
// public void close()
fw.close();
//code
}
}
案例二:
public class FileWriterDemo3 {
public static void main(String[] args) throws IOException {
// 建立字元輸出流對象
FileWriter fw = new FileWriter("c.txt",true);
// 寫資料
// 一次寫一個字元
// fw.write('a');
// fw.write(98);
// 一次寫一個字元數組
// char[] chs = { 'a', 'b', 'c', 'd', 'e' };
// fw.write(chs);
// 一次寫一個字元數組的一部分
// fw.write(chs, 0, 2);
// 一次寫一個字元串
// fw.write("abcde");
// // 一次寫一個字元串的一部分
// fw.write("abcde", 0, 3);
// fw.flush();
// 寫資料
// fw.write("hello\r\n");
// fw.write("world\r\n");
// fw.write("java\r\n");
// fw.flush();
// 追加資料
fw.write("hello\r\n");
fw.write("world\r\n");
fw.write("java\r\n");
fw.flush();
// 釋放資源
fw.close();
}
}
案例三:
public class FileWriterDemo4 {
public static void main(String[] args) {
// FileWriter fw = null;
// try {
// // System.out.println(10/0);
// fw = new FileWriter("d.txt");
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// fw.write("d.txt");
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// fw.flush();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
FileWriter fw = null;
try {
fw = new FileWriter("d.txt");
fw.write("hello");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
案例四:
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
//封裝資料源
FileReader fr = new FileReader("d.txt");
//封裝目的地
//要複制到具體的檔案
FileWriter fw = new FileWriter("e.txt");
//讀取資料
// int i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));
char[] chs = new char[3];
int len = 0;
while ((len = fr.read(chs)) != -1) {
//寫入資料
System.out.println(len);
System.out.println(new String(chs,0,len));
// fw.write(chs, 0, len);
}
//釋放資源
fw.close();
fr.close();
}
}