A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 科篮 中级黑马   /  2014-7-17 22:30  /  1327 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 科篮 于 2014-9-21 22:44 编辑

没法删除没法删除没法删除没法删除没法删除

4 个回复

倒序浏览
流包括 字节流,字符流。
根据你的问题,你是说要操作文件(例如txt文件),那么就分为纯文本文件,和非纯文本文件(例如mp4文件)
根据目标不同,选择的流类型不同,字节流可以操作所有的文件。字符流用来操作文本文件,那么,你可能就有问题,有了字节流,为什么还要使用字符流呢。两个字。方便!字符流是专门用于处理文本文本文件的具体可以拆看帮助文档

InputStream OutputStream 字节流的输入输出,是所有字节流的根类
InputStream
        |-- FileInputStream 用来读取文件的字节输入流 FileInputStream fis = new FileInputStream(new File("文件路径"));
        |-- BufferedInputStream 当你需要用到缓冲时,可以使用这个字节流,参数为一个
            InputStream示例 BufferedInputStream bis = BufferedInputSteam(fis);

标准输入: 键盘,对应的是System.in


OutputStream
        |--FileOutputStream 用来将数据输出到一个文件中 FileOutputStream fos = new FileOutputStream(new File("文件路径"));
        |--BufferedOutputStream 需要用到缓冲时,用到该流
           BufferedOutputStream bos = new BufferedOutputStream(fos);

标准输出:控制台,对应的是System.out

Writer Reader 字符流的输出输入,是所有字符流的超类

Reader
        |--FileReader 用来读取纯文本文件的便捷类 FileReader fr = new FileReader(new File("文件路径"));
        |--BufferedReader 如果需要使用缓冲区,使用该类 BufferedReader br = new BufferedReader(fr);
       
        比较特殊的有一个转换流InputStreamReader:将输入字节流转换为输入字符流
        InputStreamReader isr = new InputStreamReader(InputStream实现);例如用字节流读取文本文件后,使用转换流转换为字符流进行操作

Writer
        |--FileWriter 用来将文本数据输出在文本文件中 FileWriter fw = new FileWriter(new File("文件路径"));
        |--BufferedWriter 如果需要使用缓冲区,使用该类 BufferedWriter br = new BufferedWriter(fw);

        比较特殊的有一个转换流OutputStreamWriter:将输出字节流转换为输出字符流
        OutputStraemWriter osw = new OutputStreamWriter(OutputStream实例); 例如 将System.out转换为向文件输出

File类:将文件系统中的文件和文件夹封装成了对象。提供了更多的属性和行为可以对这些文件和文件夹进行操作。这些是流对象办不到的,因为流只操作数据。


流的操作规律:
1,明确源和目的。
需要读取,可以使用两个体系:InputStream、Reader;
需要写入,可以使用两个体系:OutputStream、Writer;
2,操作的数据是否是纯文本数据?
如果是:读取:Reader
    输出:Writer
如果不是:读取:InputStream
      输出:OutputStream
3,虽然确定了一个体系,但是该体系中有太多的对象,到底用哪个呢?
明确操作的数据设备。
数据源对应的设备:硬盘(File),内存(数组),键盘(System.in)
数据汇对应的设备:硬盘(File),内存(数组),控制台(System.out)。
4,需要在基本操作上附加其他功能吗?比如缓冲。
如果需要就进行装饰。
转换流特有功能:转换流可以将字节转成字符,原因在于,将获取到的字节通过查编码表获取到指定对应字符。
转换流的最强功能就是基于 字节流 + 编码表 。没有转换,没有字符流。
发现转换流有一个子类就是操作文件的字符流对象:
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
想要操作文本文件,必须要进行编码转换,而编码转换动作转换流都完成了。所以操作文件的流对象只要继承自转换流就可以读取一个字符了。
但是子类有一个局限性,就是子类中使用的编码是固定的,是本机默认的编码表,对于简体中文版的系统默认码表是GBK。
FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
以上两句代码功能一致,
如果仅仅使用平台默认码表,就使用FileReader fr = new FileReader("a.txt"); //因为简化。
如果需要制定码表,必须用转换流。
转换流 = 字节流+编码表。
转换流的子类File = 字节流 + 默认编码表。
凡是操作设备上的文本数据,涉及编码转换,必须使用转换流。
在io中的其他类和上面所提到类的具体方法,可以看下帮助文档
回复 使用道具 举报
字符流读:
  1. FileReader fr=null;
  2.                         try
  3.                         {
  4.                                 fr=new FileReader("demo.txt");
  5.                                 char[] n=new char[1024];
  6.                                 int num=0;
  7.                                 while((num=fr.read(n))!=-1)
  8.                                         System.out.println(num+"<>>"+new String(n,0,num));
  9.                         }
  10.                         catch (IOException e)
  11.                         {
  12.                                 e.printStackTrace();
  13.                         }
  14.                         finally
  15.                         {
  16.                                 try
  17.                                 {
  18.                                         if(fr!=null)
  19.                                         fr.close();
  20.                                 }
  21.                                 catch (IOException e)
  22.                                 {
  23.                                         e.printStackTrace();
  24.                                 }
  25.                                
  26.                         }
复制代码

字符流写:
  1. FileWriter fw=null;
  2.                 try
  3.                 {
  4.                         fw=new FileWriter("demo.txt");
  5.                         fw.write("djdklj");
  6.                 }
  7.                 catch (IOException e)
  8.                 {
  9.                         e.printStackTrace();
  10.                 }
  11.                 finally
  12.                 {
  13.                         try
  14.                         {
  15.                                 if(fw!=null)
  16.                                         fw.close();
  17.                         }
  18.                         catch (IOException e)
  19.                         {
  20.                                 e.printStackTrace();
  21.                         }       
  22.                 }
复制代码

字节流读:
  1. import java.io.*;
  2. class InputStreamDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 read_3();
  7.         }
  8.         public static void read_1()
  9.         {
  10.                 FileInputStream fis=null;
  11.                 try
  12.                 {
  13.                         fis=new FileInputStream("Demo.java");
  14.                         int len=0;
  15.                         byte[] buf=new byte[1024];
  16.                         while((len=fis.read(buf))!=-1)
  17.                         {
  18.                                 System.out.println(new String(buf,0,len));
  19.                         }
  20.                 }
  21.                 catch (IOException e)
  22.                 {
  23.                         e.printStackTrace();
  24.                 }
  25.                 finally
  26.                 {
  27.                         try
  28.                         {
  29.                                 if(fis!=null)
  30.                                         fis.close();
  31.                         }
  32.                         catch (IOException e)
  33.                         {
  34.                                 e.printStackTrace();
  35.                         }
  36.                 }
  37.         }
  38.         public static void read_2()
  39.         {
  40.                 FileInputStream fis=null;
  41.                 try
  42.                 {
  43.                         fis=new FileInputStream("Demo.java");
  44.                         int ch=0;
  45.                         while((ch=fis.read())!=-1)
  46.                         {
  47.                                 System.out.println((char)ch);
  48.                         }
  49.                 }
  50.                 catch (IOException e)
  51.                 {
  52.                         e.printStackTrace();
  53.                 }
  54.                 finally
  55.                 {
  56.                         try
  57.                         {
  58.                                 if(fis!=null)
  59.                                         fis.close();
  60.                         }
  61.                         catch (IOException e)
  62.                         {
  63.                                 e.printStackTrace();
  64.                         }
  65.                 }
  66.         }
  67.         public static void read_3()
  68.         {
  69.                 FileInputStream fis=null;
  70.                 try
  71.                 {
  72.                         fis=new FileInputStream("Demo.java");
  73.                         byte [] buf=new byte[fis.available()];
  74.                         fis.read(buf);
  75.                         System.out.println(new String(buf));
  76.                 }
  77.                 catch (IOException e)
  78.                 {
  79.                         e.printStackTrace();
  80.                 }
  81.                 finally
  82.                 {
  83.                         try
  84.                         {
  85.                                 if(fis!=null)
  86.                                         fis.close();
  87.                         }
  88.                         catch (IOException e)
  89.                         {
  90.                                 e.printStackTrace();
  91.                         }
  92.                 }
  93.         }
  94. }
复制代码

字节流写:
  1. import java.io.*;
  2. class CopyPicture
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 FileInputStream fis=null;
  7.                 FileOutputStream fos=null;
  8.                 try
  9.                 {
  10.                         fis=new FileInputStream("Desert.jpg");
  11.                         fos=new FileOutputStream("Demo.jpg");
  12.                         int len=0;
  13.                         byte[] buf=new byte[1024];
  14.                         while((len=fis.read(buf))!=-1)
  15.                         {
  16.                                 fos.write(buf,0,len);
  17.                         }
  18.                 }
  19.                 catch (IOException e)
  20.                 {
  21.                         throw new RuntimeException("copy failed!");
  22.                 }
  23.                 finally
  24.                 {
  25.                         try
  26.                         {
  27.                                 if(fis!=null)
  28.                                         fis.close();
  29.                                 if(fos!=null)
  30.                                         fos.close();
  31.                         }
  32.                         catch (IOException e)
  33.                         {
  34.                                  throw new RuntimeException("读取关闭失败");
  35.                         }
  36.                 }
  37.         }
  38. }
复制代码
回复 使用道具 举报
sugar 发表于 2014-7-18 07:53
字符流读:

字符流写:

可以,学习的可以
回复 使用道具 举报
苗润 发表于 2014-7-18 02:44
流包括 字节流,字符流。
根据你的问题,你是说要操作文件(例如txt文件),那么就分为纯文本文件,和非纯 ...

好的,谢了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马