IO流学习笔记3——字节流和字符流的输入/输出

来源:互联网 发布:莱恩打碟软件下载 编辑:程序博客网 时间:2024/05/17 21:53

字节流和字符流的操作方式基本一样,区别只是操作的数据单元有区别:字节流操作的数据单元是字节,字符流操作的数据单元字符。

InputStream和Reader是所有输入流的基类,它们两个都是抽象类,本身不能创建实例,但它们分别有一个用于读取文件的输入流:FileInputStream和FileReader,它们都是节点流。

OutputStream和Writer是所有输出流的基类,和输入流相似,也有对应的输出流:FileOutputStream和FileWriter,它们也是节点流。


1、使用FileInputStream读取文件例子如下:

package com.songguoliang.io;import java.io.FileInputStream;import java.io.IOException;/** * FileInputStream(节点流) * 以字节的方式读取test.txt文件内容,并输出到控制台(可能会出现中文乱码) * @date 2015-08-14 11:21:11 * @author sgl */public class FileInputStreamTest {public static void main(String[]args){FileInputStream fileInputStream=null;try {//创建字节输入流,构造参数不是已存在的流,说明FileInputStream是节点流fileInputStream=new FileInputStream("test.txt");//创建一个长度为1024的缓存区byte[]buffer=new byte[32];//用户保存实际读取的字节数int hasRead=0;//使用循环来重复取字节内容的过程//由于每个中文字占2个字节,如果read方法读取时只读到了半个中文字符,这将导致中文字乱码while((hasRead=fileInputStream.read(buffer))>0){//取出缓存区中字节,将字节数组转换成字符串输出到控制台System.out.println(new String(buffer,0,hasRead));}} catch (IOException e) {e.printStackTrace();} finally{try {//关闭文件输入流//程序里打开的文件IO资源,不属于内存里的资源,垃圾回收无法回收该资源,所以应该显示关闭文件IO资源fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

2、使用FileReader读取文件的例子:

package com.songguoliang.io;import java.io.FileReader;import java.io.IOException;/** * FileReader(节点流) * 以字符的方式读取test.txt文件内容,并输出到控制台 * @date 2015-08-14 11:35:25 * @author sgl */public class FileReaderTest {public static void main(String[] args) throws IOException {FileReader fileReader=null;try {//创建字符输入流,构造参数不是已存在的流,说明FileReader是节点流fileReader=new FileReader("test.txt");//创建一个长度为32的缓冲区char[] buffer=new char[32];//用于保存实际读取的字符数int hasRead=0;//使用循环来重复读取字符过程while((hasRead=fileReader.read(buffer))>0){//取出缓冲区中字节,将字符数组转换成字符串输出到控制台System.out.println(new String(buffer,0,hasRead));}} catch (IOException e) {e.printStackTrace();} finally{//使用finally块来关闭文件输入流if(fileReader!=null){fileReader.close();}}}}



3、使用FileOutputStream输出内容列子如下:

package com.songguoliang.io;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/** * FileOutputStream(节点流) * 读取test.txt文件内容并写入newTest.txt文件,即复制test.txt文件 * @date 2015-08-14 11:44:34 * @author sgl */public class FileOutputStreamTest {public static void main(String[] args){FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;try {//创建字节输入流,构造参数不是已存在的流,说明FileInputStream是节点流fileInputStream=new FileInputStream("test.txt");//创建字节输出流,构造参数不是已存在的流,说明FileOutputStream是节点流fileOutputStream=new FileOutputStream("newTest.txt");byte[] bbuf=new byte[32];int hasRead=0;//循环从输入流中取出数据while((hasRead=fileInputStream.read(bbuf))>0){//每读取一次,即写入文件输出流,读了多少,就写多少。fileOutputStream.write(bbuf, 0, hasRead);}} catch (IOException e) {e.printStackTrace();} finally{//使用finally块来关闭文件输入流if(fileInputStream!=null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}//使用finally块来关闭文件输出流if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}}


4、使用FileWriter输出内容例子如下:

package com.songguoliang.io;import java.io.FileWriter;import java.io.IOException;/** * FileWriter(节点流) * 将字符串写入text.txt文件 * @date 2015-08-14 11:50:42 * @author sgl */public class FileWriterTest {public static void main(String[] args){FileWriter fileWriter=null;try {//创建输出流,构造参数不是已存在的流,说明FileWriter是节点流fileWriter=new FileWriter("text.txt");//因为字符流直接以字符作为操作单位,所以Writer可以用字符串代替字符数组,即使用String对象作为参数。//最后面\r\n是Windows平台的换行符,如果是linux等平台,只要使用\n就可以作为换行符fileWriter.write("锦瑟 - 李商隐\r\n");fileWriter.write("锦瑟无端五十弦,一弦一柱思华年。\r\n");fileWriter.write("庄生晓梦迷蝴蝶,望帝春心托杜鹃。\r\n");fileWriter.write("沧海月明珠有泪,蓝田日暖玉生烟。\r\n");fileWriter.write("此情可待成追忆,只是当时已惘然。\r\n");} catch (IOException e) {e.printStackTrace();} finally{//关闭输出流if(fileWriter!=null){try {fileWriter.close();} catch (IOException e) {e.printStackTrace();}}}}}








0 0
原创粉丝点击