Java I/O第二篇 之 (缓冲流 随机流 数组流 数据流)

时间:2023-11-15 15:20:50

1:缓冲流 BufferedReader  BufferedWriter

具有较强的读写能力,能单独读写一行数据,能够减少对硬盘的访问次数。

 /**
* 缓冲流 能够增强对问价数据的可读性,减少访问读取硬盘的次数,提高效率
* */
public static void bufferedDemo() throws IOException{
File fread=new File("E:\\input.txt");
File fwrite=new File("E:\\output.txt");
System.out.println("文件:"+"E:\\output.txt"+"是否可读"+fwrite.canWrite());
BufferedReader bis=null;
BufferedWriter bos=null;
FileReader fis=new FileReader(fread);
FileWriter fos=new FileWriter(fwrite);
bis=new BufferedReader(fis);
bos=new BufferedWriter(fos);
//读取文件数据,打印展示
int lineLen=0;
String arrayStr[]=new String[20]; //创建数组,存储读取内容
String temp;
while((temp=bis.readLine())!=null){
System.out.println(temp);
arrayStr[lineLen]=temp;
lineLen++;
}
System.out.println("文件读取完毕");
//输出数据
String s;
while((s=bis.readLine())!=null){
bos.write(s);
bos.newLine(); //插入换行符
}
System.out.println("数据输出完毕。");
bos.flush();
bis.close();
bos.close();
}

2:随机流 RandomAccessFile

RandomAccessFile流,既可以读取文件中的数据,也可以写进文件中的数据的一种流

 /**
* 随机流 可读可写的类型RandomAccessFile
* @throws IOException
* */
public static void RandomAccessDemo() throws IOException{
//单独使用Random写进一些数据,在读出来
int data[]={1,2,3,4,5,6,7,8,9};
File fileName=new File("E:\\RandomAccessFile.txt");
if(!fileName.exists())
fileName.createNewFile();
RandomAccessFile raf=new RandomAccessFile(fileName, "rw");
for(int i=0;i<data.length;i++){
raf.writeInt(i);
}
System.out.println("数据写入完毕。");
//读取数据
for(int j=data.length-1;j>=0;j--){
raf.seek(j*4);
System.out.printf("\t%d",raf.readInt());
}
raf.close();
}

3:数组流     字节数组流:ByteArrayInputStream  ByteArrayOutputStream

字符数组流:BytearrayReader  ByteArrayWriter

 /**
* 数组流 字节数组流(ByteArrayInputStream ByteArrayOutputStream)
* 字符数组流(BytearrayReader ByteArrayWriter)
* */
public static void ByteArrayDemo() throws Exception{
//字节流
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte [] str="祝你节日快乐".getBytes();
bos.write(str); //写入数据
//读取数据
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
byte[] b=new byte[bos.toByteArray().length];
bis.read(b);
System.out.println("写入的数据为:"+new String(b));
bis.close();
bos.close();
//字符流
CharArrayWriter caw=new CharArrayWriter();
char [] str2="中秋节快乐!".toCharArray();
caw.write(str2);
CharArrayReader car=new CharArrayReader(caw.toCharArray());
char[] c=new char[caw.toCharArray().length];
car.read(c);
System.out.println("读取的数据为:"+new String(c));
car.close();
caw.close();
}

4:数据流   DataInputStream DataOutputStream

能够方便的读取与机器无关的java原始数据,读取时不必纠结去字节大小

/**
* 数据流 DataInputStream DataOutputStream 能够方便的读取与机器无关的java原始数据
* */
public static void DataDemo() throws IOException{
//创建文件 ,用数据流写入不同类型的数据,分别读出不同类型的数据
File f=new File("E:\\DataDemo.txt");
FileOutputStream fos=new FileOutputStream(f);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(23);
dos.writeDouble(22.343435);
dos.writeFloat(55.2343f);
dos.writeBoolean(true);
dos.writeChars("中秋快乐!");
System.out.println("数据读入完毕!");
//读取数据
FileInputStream fis=new FileInputStream(f);
DataInputStream dis=new DataInputStream(fis);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readFloat());
System.out.println(dis.readFloat());
System.out.println(dis.readBoolean());
char c='\0';
while((c=dis.readChar())!='\0'){
System.out.print(c);
}
dis.close();
dos.close();
}