package test;
import java.io.*;
/**
* 字节流和字符流的测试
*/
public class IOtest {
public static void main(String[] args) {
IOtest iOtest=new IOtest();
try {
//注意字节流在读取时 存在中文乱码问题 需谨慎
iOtest.byFileOutputStream();//字节流 输出流 也就是写入文件
iOtest.byFileOutputStreamAppend();//字节流 输出流 向文件追加内容
iOtest.byFileInputStream();//字节流 输入流 也就是从文件读取
iOtest.byFileInputStreamArr();//字节流 输入流 也就是从文件读取 读取指定数组个字节
iOtest.byBufferedOutputStream();//字节流 缓冲输出流 也就是写入文件
iOtest.byBufferedInputStream();//字节流 缓冲输入流 也就是读取文件
//字符流
iOtest.byOutputStreamWriter();//字符输出流 也就是写入文件
iOtest.byInputStrreamReader();//字符输入流 也就是读取文件
iOtest.byPrintWriter();//字符输出流 字符缓存行刷新 也就是写入文件
iOtest.byBufferedReader();//字符输入流 字符缓存读取 也就是读取文件
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件字节输出流
* 这里需要注意,若指定的文件已经包含内容,那么当使用FOS对其写入数据时,会将该文件中原有数据全部清除
*/
public void byFileOutputStream() throws IOException {
FileOutputStream fos=new FileOutputStream("test.txt");
fos.write("helloworld".getBytes());
fos.close();
}
/**
* 测试追加写的方式
*/
public void byFileOutputStreamAppend() throws IOException {
FileOutputStream fos=new FileOutputStream("test.txt",true);
fos.write("林兴".getBytes());
fos.close();
}
/**
* 测试文件输入流的创建以及读取数据
* 由于是字节流所以会导致中文乱码问题
*/
public void byFileInputStream() throws IOException {
// 根据给定的File对象创建文件输入流
FileInputStream fis = new FileInputStream("test.txt");
int d = -1;
while ((d = fis.read()) != -1) {
System.out.print((char) d);
}
fis.close();
}
/**
* 测试使用字节数组形式复制文件
* 因为一个汉字2个字节以32个字节数组来复制不会导致中文乱码
* 缺点:增大写出次数无疑会降低写出效率,为此我们可以使用缓冲输出流来一次性批量写出若干数据减少写出次数来提高写出效率
* BufferedOutputStream缓冲输出流内部维护着一个缓冲区,每当我们向该流写数据时,都会先将数据存入缓冲区,
* 当缓冲区已满时,缓冲流会将数据一次性全部写出。
* FileInputStream也支持批量读取字节数据的方法:
* -int read(byte[] b)
* 从此输入流中将最多b.length个字节的数据读入到字节数组b中
* FileOutputStream也支持批量写出字节数据的方法
* -void write(byte[] d)
* 将b.length个字节从指定byte数组写入此文件输出流中。
* -void write(byte[] d,int offset,int len)
* 将指定byte数组中从偏移量off开始的len个字节写入此文件输入流
*/
public void byFileInputStreamArr() throws IOException {
FileInputStream fis = new FileInputStream("test.txt");//从test.txt复制到test1.txt
FileOutputStream fos = new FileOutputStream("test1.txt");
int len =-1;
byte[] buf = new byte[32];
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
System.out.println("复制完毕");
fis.close();
fos.close();
}
/**
* BufferedOutputStream读入缓冲区的方式
* 如果原有文件吕有文字会被清除重新写入
* flush()方法清空缓冲区强制写入文件中
*/
public void byBufferedOutputStream() throws IOException {
FileOutputStream fos=new FileOutputStream("test.txt");//向test.txt中写入
BufferedOutputStream bos=new BufferedOutputStream(fos);
bos.write("hello林兴".getBytes());//所有字节被存入缓冲区,等待一次性写出
bos.write("helloworld1".getBytes());//结果为:hello林兴helloworld1
bos.close();//关闭流之前,缓冲输出流会将缓冲区内容一次性写出
}
/**
* BufferedInputStream 从文件中每次读取字节数组到缓冲区
* 注意中文占2个字节所以直接打印输出会乱码 而fis.read(buf)在写到文件时不会乱码
* bis读取后bos写入文件是否会乱码呢?请看下面的测试 结果是同样不会乱码
*/
public void byBufferedInputStream() throws IOException {
//创建缓冲字节输入流 从test中读取
FileInputStream fis = new FileInputStream("test.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//写入test1中
FileOutputStream fos=new FileOutputStream("test1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
int d =-1;
//缓冲读入,实际上并非是一个字节一个字节从文件读取的
while((d=bis.read())!=-1){
bos.write(d);
}
bis.close();
bos.close();
}
/**
* **************************************************************************************
* 以下是字符流,字符流是在字节流的基础上的
* Reader是字符输入流的父类
* Writer是字符输出流的父类
* 字符流是字符(char)为单位读写数据的。一次处理一个unicode.
* 字符流的底层仍然是基本的字节流。
* InputStreamReader:字符输入流 OutputStreamWriter:字符输出流
*/
public void byOutputStreamWriter() throws IOException {
FileOutputStream fos=new FileOutputStream("test.txt");//字节流依赖字节流
//FileOutputStream fos=new FileOutputStream("test.txt",true);//追加模式
OutputStreamWriter writer=new OutputStreamWriter(fos,"UTF-8");
String str ="大家好!";
writer.write(str);
writer.close();
}
/**
* 字符流从文件中读取
* 和字节流fis的区别是字符流从文件中读取的中文直接输出到控制台不会乱码
* 对于文件的操作使用字符流更适合
*/
public void byInputStrreamReader() throws IOException {
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");//GBK
int c=-1;
while((c=reader.read())!=-1){
System.out.print((char)c);
}
reader.close();
}
/**
* 字节有字节缓冲输出流BufferedOutputStream那么字符呢?字符是PrintWriter 写入文件
* PrintWriter是具有自动行刷新的缓冲字符输出流,其提供了比较丰富的构造方法。
* 字节输入流BufferedInputStream 与之对应的字符输入流 BufferedReader
*/
public void byPrintWriter() throws UnsupportedEncodingException, FileNotFoundException {
FileOutputStream fos = new FileOutputStream("test.txt");//字节流
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");//字符流
//创建带有自动行刷新的PW
PrintWriter pw = new PrintWriter(osw,true);
pw.println("大家好!aaaa林兴");//立即写出该字符串
pw.close();//关闭时,pw也会先进行flush
}
public void byBufferedReader() throws IOException {
FileInputStream fis=new FileInputStream("test.txt");
InputStreamReader reader=new InputStreamReader(fis,"UTF-8");
BufferedReader br=new BufferedReader(reader);
String valueString = null;
while ((valueString = br.readLine()) != null) {
System.out.println(valueString);
}
br.close();// 关闭时,pw也会先进行flush
}
}
IO字节流字符流
最新推荐文章于 2024-07-14 07:30:00 发布