------- <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();
}
}