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();
- }
- }
相关推荐
2、常用21个IO流:FileWriter、FileReader、CharArrayReader、CharArrayWriter、CharSequence、OutputStreamWriter、FileOutputStream、InputStreamReader、PrintWriter、BufferedReader、InputStream、...
JAVAIO流总结宝典,分享给大家,总结较为全面
IO流 javaio java 流
本人是初学者,在老师的指导下,将自身对Java中IO流的理解做成以下思维导图
java基础之IO流,基础简介知识,帮助你了解java的io技术
思维导图io流思维导图io流思维导图io流思维导图io流思维导图io流思维导图io流
map集合的特性和它的遍历方式 还有一个根据Set特性的一个排序 以及IO流 字节流和字符流实现代码
IO流文件的复制
关于java的IO流基础的练习,适合刚接触IO流,没有基础的同学
IO流
IO流的主要流
一,IO流的实现方式 1:用的是装饰模式,装饰模式有四个角色,分别是通用接口、本原、抽象装饰、 具体装饰,因为是将本原聚合到了装饰中,所以可以将本原或装饰通过构造子注入到 另一个装饰,多个装饰可以随意...
适用于刚学习过Java,尚未学习数据库的人,因为本程序要存储学生以及老师账户,需要用到IO流写的操作
该文档主要整理了Java IO流的相关信息,主要包括IO流的按照不同维度的分类、节点流、处理流、输入输出流的处理过程、抽象基类的使用等细节内容
Java中文件IO流.pdf 学习资料 复习资料 教学资源
IO流..
彻底明白Java的IO流
IO流读取和创建文件IO流读取和创建文件IO流读取和创建文件IO流读取和创建文件IO流读取和创建文件
JAVA教程 io流操作 JAVA教程 io流操作 JAVA教程 io流操作
java之io流实例应用。