java 中的流_Java中的流(IO

Java中的流(IO流.

java.io包中定义了多个流类型(类或抽象类)来实现 输入 / 输出功能,可以从不同的角度对其进行分类:

按单位可分为: 字节流         (一个字节一个字节的读取) 字符流         (一个字符一个字符的读取『一个字符是两个字节』)

按功能不同可以分为: 节点流      (数据的传输) 处理流      (数据的处理)

按方向可以分为: 输入流         (相对于程序来说的) 输出流         (相对于程序来说的)

以下 输出输出流 图中深色的为节点流,浅色为处理流。 InputStream 继承自该类的流都是用于向程序中输入数据,且数据的单位为字节(8bit);

590e79aa1203dd57084be8c823c9bcca.gif

基本方法:

d8443fe7862babd9beb71fc6df77eae4.gif

OutputStream 继承自该类的流是用于程序中输入数据,且数据的单位为字节(8bit);

175b1aff7f4b84929b9cf61018d123a2.gif

基本方法:

24dcebe7165c15287451dc7ba862ae8b.gif

Reader 继承自该类的流都是用于向程序中输入数据,且数据的单位为字符(16bit);

49c5d1e0af728f74906dc4ac3be13f59.gif

常用方法:

1dc58c2cf47f39df3164ac58b146f1a6.gif

Writer 继承自该类的流都是用于程序中输入数据,且数据的单位为字符(16bit);

1dc5388cb38daa1f608192f87c638ca5.gif

常用方法:

b7a2a8fec9135517fdbdf192a68d2ab7.gif

7a1e679f9771c353cf7c471826f1198a.gif

7eb50446d694908630ad5a0523634dcf.gif

缓冲流: 缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,可以降低硬盘的读写次数,同时增加了一些新的方法。 SDK提供了四种缓存流,其常用的构造方法为:

d9d4f0ce64a6ccf3ec5b27f5d66b51bd.gif

转换流(转换编码):

0139edd17c76a0f87f53d32ac66200a6.gif

Print流 用于输出

40e3160db7e2ffa4e41d877dd965ba0a.gif

在使用中如下: new InputStreamReader(System.in);//监听读取键盘的输入 PrintWriter log = new PrintWriter(输入流); 另外实例:     PrintStream ps = null;     try {       FileOutputStream fos =               new FileOutputStream(“d:\\bak\\log.dat”);       ps = new PrintStream(fos);     } catch (IOException e) {       e.printStackTrace();     }     if(ps != null){       System.setOut(ps);     }

Object流 该类的Object流是保存对象用的。 注意代码:             FileOutputStream fos = new FileOutputStream(file);             BufferedOutputStream bos = new BufferedOutputStream(fos); 才可进行写的操作。 要保存对象,必须要求该对象的实体类是可序列化的『Serializable』。 transient关键字 在序列化的实体类中,使用transient关键字修饰的属性,被修饰的属性被视作透明不存在的。 实现Externalizable接口是自己写序列化,保存对象的输出流方法。

具体如下图所示:

85d2b6e679be5001ed55bf18cc886260.gif

示例代码:     public void InputStreamTest() {         /**          * 创建文件夹以及文件          */         String sep = File.separator;         String path = “E:” + sep + “MyTest”;         String fileName = “document.txt”;         File file = new File(path, fileName);         if (file.exists()) {             System.out.println(“文件路径:”+file.getAbsolutePath());             System.out.println(“文件大小:”+file.length());         } else {             file.getParentFile().mkdirs();             try {                 file.createNewFile();             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }

/**         * 字节读取文件          */         try {             FileInputStream fis = new FileInputStream(file);             BufferedInputStream bis = new BufferedInputStream(fis);    //这些Buffered**的都是处理流(缓冲)             int b ;             while ((b=bis.read())!=-1) {                 System.out.print((char)b);             }             bis.close();             System.out.println(“\n\n”);         } catch (FileNotFoundException e) {             // TODO Auto-generated catch block             System.out.println(“未找到文件”);             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }

/**          * 字符读取文件          */         try {             FileReader fileReader = new FileReader(file);             BufferedReader br = new BufferedReader(fileReader);             int b ;             while ((b=br.read())!=-1) {     //使用br.readLine();可读一行数据                 System.out.print((char)b);             }             br.close();         } catch (FileNotFoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }

/**          * 字节输入文件          */         try {             FileOutputStream fos = new FileOutputStream(file);             BufferedOutputStream bos = new BufferedOutputStream(fos);             String str = new String(“所有的中文测试”);             byte[] c=str.getBytes();             bos.write(c);             bos.flush();             bos.close();         } catch (FileNotFoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }                 /**          * 字符输入文件          */         try {             FileWriter fw = new FileWriter(file,true);//写上true代表在原有的字符串后面继续写入             BufferedWriter bw = new BufferedWriter(fw);             String str = “受了点伤”;             bw.write(str.toCharArray());             bw.flush();      //将缓存中的数据完全打印             bw.close();         } catch (IOException e) {             // TODO Auto-generated catch block             System.out.println(“文件读写错误”);             e.printStackTrace();         }                 /**          * Data数据流测试,这个子类是将对象保存到了“*.txt”文件中,在文件中显示乱码          * 因为是将对象保存到了  txt  文件中,所以无法正常显示。          */         try {             FileOutputStream fos = new FileOutputStream(file);             BufferedOutputStream bos = new BufferedOutputStream(fos);             DataOutputStream dos = new DataOutputStream(bos);             double dou = Math.random();             System.out.println(dou);             dos.writeDouble(dou);             dos.flush();             dos.close();         } catch (FileNotFoundException e1) {             // TODO Auto-generated catch block             e1.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }                 /**         * Data数据流在程序之间的传递         */         try {             ByteArrayOutputStream baos = new ByteArrayOutputStream();             DataOutputStream dos = new DataOutputStream(baos);             dos.writeDouble(Math.random());             dos.writeBoolean(true);             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());             System.out.println(bais.available());             DataInputStream dis = new DataInputStream(bais);             System.out.println(dis.readDouble());             System.out.println(dis.readBoolean());             dos.close();             dis.close();         } catch (FileNotFoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }                 /保存对象///stu对象必须实现了Serializable接口,也就是必须可序列化         public void forSaveObject(Student stu){         try {             FileOutputStream fos = new FileOutputStream(“E:/dd.dox”);             ObjectOutputStream oos = new ObjectOutputStream(fos);             oos.writeObject(stu);             oos.flush();             oos.close();         } catch (FileNotFoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }     }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值