1.IO流是干什么的?
IO流指 的是输入输出流,用来处理设备上的数据。这里的设备指硬盘,内存,键盘录入,网络传输等。
2.IO流的分类?
按处理数据类型来分:字节流和字符流
按流的方向来分:输入流和输入流。
PS:初学流的时候,新手很容易搞不清什么时候用输入流,什么时候用输出流。简单来说,当需要读数据的时候,需要使用输入流,当需要写数据的时候,需要使用输出流。我以前是这么记忆的:“读入写出”,要读的话就用输入流,要写的话,就用输出流。经常想起这4个字,慢慢就记熟下来了。
3.什么时候使用字节流?什么时候使用字符流?
首先需要知道的是,任何数据存在硬盘上时,都是以二进制的形式存储的。而通过使用字节流,可以读取任意文件。字节流一次读取一个字节,而字符流使用了字节流读到一个或者多个字节时,去查找指定的编码表,返回对应的编码。所以字符流只能处理纯文本字符数据,而字节流可以处理更多类型的数据,比如图片,视频,音频文件等。因此,只要是纯文本数据处理,优先考虑使用字符流。其他情况就使用字节流。
4.IO流类的关系(列举了其中一部分)
1.字符流的读写+不加BufferedReader/Writer
- public class CharIOTest {
- /**
- * 流转换读取数据.
- * @throws FileNotFoundException
- * @throws IOException
- * @throws IOException
- */
- @Test
- public void readChangeTest() throws IOException {
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
- FileInputStream fis = new FileInputStream(file);// 获取该文件的输入流
- InputStreamReader isr=new InputStreamReader(fis, "gbk");
- char[] bb = new char[1024];// 用来保存每次读取到的字符
- StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
- int n;// 每次读取到的字符长度
- while ((n = isr.read(bb)) != -1) {
- sb.append(new String(bb, 0, n));
- }
- isr.close();// 关闭输入流,释放连接
- System.out.println(sb);
- }
- /**
- * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
- * 好处:代码少写几行,但不通用。
- * @throws IOException
- */
- @Test
- public void readTest() throws IOException {
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要读取的文件
- FileReader fr=new FileReader(file);
- // System.out.println(fr.getEncoding());
- char[] bb = new char[1024];// 用来保存每次读取到的字符
- StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
- int n;// 每次读取到的字符长度
- while ((n = fr.read(bb)) != -1) {
- sb.append(new String(bb, 0, n));
- }
- fr.close();// 关闭输入流,释放连接
- System.out.println(sb);
- }
- /**
- * 往文件里写入数据.
- * @throws IOException
- */
- @Test
- public void writeTest() throws IOException {
- String writerContent = "hello world,你好世界";// 要写入的文本
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 要写入的文本文件
- if (!file.exists()) {// 如果文件不存在,则创建该文件
- file.createNewFile();
- }
- FileWriter writer = new FileWriter(file);// 获取该文件的输出流
- System.out.println(writer.getEncoding());
- // 写内容 ,默认使用writer.getEncoding()进行写入,不会出现乱码,因为得到的文件的编码便是写入的编码
- writer.write(writerContent);
- writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里
- writer.close();// 关闭输出流,施放资源
- }
- }
public class CharIOTest {
/**
* 流转换读取数据.
* @throws FileNotFoundException
* @throws IOException
* @throws IOException
*/
@Test
public void readChangeTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
FileInputStream fis = new FileInputStream(file);// 获取该文件的输入流
InputStreamReader isr=new InputStreamReader(fis, "gbk");
char[] bb = new char[1024];// 用来保存每次读取到的字符
StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
int n;// 每次读取到的字符长度
while ((n = isr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
isr.close();// 关闭输入流,释放连接
System.out.println(sb);
}
/**
* 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
* 好处:代码少写几行,但不通用。
* @throws IOException
*/
@Test
public void readTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要读取的文件
FileReader fr=new FileReader(file);
// System.out.println(fr.getEncoding());
char[] bb = new char[1024];// 用来保存每次读取到的字符
StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
int n;// 每次读取到的字符长度
while ((n = fr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
fr.close();// 关闭输入流,释放连接
System.out.println(sb);
}
/**
* 往文件里写入数据.
* @throws IOException
*/
@Test
public void writeTest() throws IOException {
String writerContent = "hello world,你好世界";// 要写入的文本
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 要写入的文本文件
if (!file.exists()) {// 如果文件不存在,则创建该文件
file.createNewFile();
}
FileWriter writer = new FileWriter(file);// 获取该文件的输出流
System.out.println(writer.getEncoding());
// 写内容 ,默认使用writer.getEncoding()进行写入,不会出现乱码,因为得到的文件的编码便是写入的编码
writer.write(writerContent);
writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里
writer.close();// 关闭输出流,施放资源
}
}
2.字节流的读写+不加BufferedInputStream/OutputStream
- public class ByteIOTest {
- /**
- *
- * 将D盘下的D:\\xxx.ico文件,读取后,再存到E盘下面.
- *
- * @param args
- * @throws Exception
- */
- @Test
- public void imageIOTest(String[] args) throws Exception {
- FileInputStream in = new FileInputStream(new File("D:\\xxx.ico"));// 指定要读取的图片
- File file = new File("E:\\test.jpg");
- if (!file.exists()) {// 如果文件不存在,则创建该文件
- file.createNewFile();
- }
- FileOutputStream out = new FileOutputStream(new File("E:\\yuanwang.ico"));// 指定要写入的图片
- int n = 0;// 每次读取的字节长度
- byte[] bb = new byte[1024];// 存储每次读取的内容
- while ((n = in.read(bb)) != -1) {
- out.write(bb, 0, n);// 将读取的内容,写入到输出流当中
- }
- out.close();// 关闭输入输出流
- in.close();
- }
- }
public class ByteIOTest {
/**
*
* 将D盘下的D:\\xxx.ico文件,读取后,再存到E盘下面.
*
* @param args
* @throws Exception
*/
@Test
public void imageIOTest(String[] args) throws Exception {
FileInputStream in = new FileInputStream(new File("D:\\xxx.ico"));// 指定要读取的图片
File file = new File("E:\\test.jpg");
if (!file.exists()) {// 如果文件不存在,则创建该文件
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(new File("E:\\yuanwang.ico"));// 指定要写入的图片
int n = 0;// 每次读取的字节长度
byte[] bb = new byte[1024];// 存储每次读取的内容
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);// 将读取的内容,写入到输出流当中
}
out.close();// 关闭输入输出流
in.close();
}
}
3.字符流的读写+加BufferedReader/Writer
- public class BufferCharIOTest {
- /**
- * 考虑到编码问题-转换流读取,即字节流->字符流
- windows系统下默认编码为gbk,即所读取文本为gbk编码。
- 因此如果myeclipse的默认是utf-8编码的话则读取中文时会出现乱码
- 情况,utf8 中文会默认3个字节,而gbk 中文默认2个字节。
- 一个字符在不同编码的情况下,其所对应的字节数是不相同,字符有中文字符,英文字符,数字字符等等
- * @throws FileNotFoundException
- * @throws IOException
- */
- @Test
- public void readChangeTest() throws FileNotFoundException, IOException {
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
- // 获得该文件的缓冲输入流
- //BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
- BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
- InputStreamReader isr=new InputStreamReader(bis, "gbk");
- char[] bb = new char[1024];// 用来保存每次读取到的字符
- StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
- int n;// 每次读取到的字符长度
- while ((n = isr.read(bb)) != -1) {
- sb.append(new String(bb, 0, n));
- }
- isr.close();// 关闭输入流,释放连接
- System.out.println(sb);
- }
- /**
- * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
- * 好处:代码少写几行,但不通用。
- * @throws IOException
- */
- @Test
- public void readTest() throws FileNotFoundException, IOException {
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
- // 获得该文件的缓冲输入流
- BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
- String line = "";// 用来保存每次读取一行的内容
- while ((line = bufferedReader.readLine()) != null) {
- System.out.println(line);
- }
- bufferedReader.close();// 关闭输入流
- }
- /**
- * 在写入过程中,不需要考虑编码问题,
- * 因为生成的文件文本的编码会与写入编码一致。因此不会出现乱码情况
- *
- * @throws IOException
- */
- @Test
- public void writeTest() throws IOException {
- File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要写入的文件
- if (!file.exists()) {// 如果文件不存在则创建
- file.createNewFile();
- }
- // 获取该文件的缓冲输出流
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
- // 写入信息
- bufferedWriter.write("你好世界");
- bufferedWriter.newLine();// 表示换行
- bufferedWriter.write("hello world");
- bufferedWriter.flush();// 清空缓冲区
- bufferedWriter.close();// 关闭输出流
- }
- }
public class BufferCharIOTest {
/**
* 考虑到编码问题-转换流读取,即字节流->字符流
windows系统下默认编码为gbk,即所读取文本为gbk编码。
因此如果myeclipse的默认是utf-8编码的话则读取中文时会出现乱码
情况,utf8 中文会默认3个字节,而gbk 中文默认2个字节。
一个字符在不同编码的情况下,其所对应的字节数是不相同,字符有中文字符,英文字符,数字字符等等
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void readChangeTest() throws FileNotFoundException, IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
// 获得该文件的缓冲输入流
//BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
InputStreamReader isr=new InputStreamReader(bis, "gbk");
char[] bb = new char[1024];// 用来保存每次读取到的字符
StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
int n;// 每次读取到的字符长度
while ((n = isr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
isr.close();// 关闭输入流,释放连接
System.out.println(sb);
}
/**
* 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
* 好处:代码少写几行,但不通用。
* @throws IOException
*/
@Test
public void readTest() throws FileNotFoundException, IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\1.txt");// 指定要读取的文件
// 获得该文件的缓冲输入流
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";// 用来保存每次读取一行的内容
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();// 关闭输入流
}
/**
* 在写入过程中,不需要考虑编码问题,
* 因为生成的文件文本的编码会与写入编码一致。因此不会出现乱码情况
*
* @throws IOException
*/
@Test
public void writeTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\2.txt");// 指定要写入的文件
if (!file.exists()) {// 如果文件不存在则创建
file.createNewFile();
}
// 获取该文件的缓冲输出流
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
// 写入信息
bufferedWriter.write("你好世界");
bufferedWriter.newLine();// 表示换行
bufferedWriter.write("hello world");
bufferedWriter.flush();// 清空缓冲区
bufferedWriter.close();// 关闭输出流
}
}
4.字节流的读写+加BufferedInputStream/OutputStream
- public class BufferByteIOTest {
- public void imageIOTest(String[] args) throws Exception {
- // 指定要读取文件的缓冲输入字节流
- BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\xx.jpg"));
- File file = new File("E:\\xx.jpg");
- if (file != null) {
- file.createNewFile();
- }
- // 指定要写入文件的缓冲输出字节流
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
- byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组
- int n;// 每次读取到的字节数组的长度
- while ((n = in.read(bb)) != -1) {
- out.write(bb, 0, n);// 写入到输出流
- }
- out.close();// 关闭流
- in.close();
- }
- }