标签:
1.基础流:
2.修饰流:
下面逐个测试:
FileInputStream, FileOutputStream
1 import java.io.ByteArrayInputStream; 2 import java.io.ByteArrayOutputStream; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.io.RandomAccessFile; 10 import java.io.StringBufferInputStream; 11 12 13 public class FileStream { 14 15 public static void main(String[] args) throws IOException 16 { 17 //测试FileInputStream,FileOutputStream 18 FileInputStream fis = new FileInputStream("file.txt"); 19 FileOutputStream fos = new FileOutputStream("file1.txt"); 20 21 byte[] bytes = new byte[fis.available()]; 22 23 fis.read(bytes); 24 fos.write(bytes); 25 26 String s; 27 s = new String(bytes); 28 System.out.println(s); 29 30 } 31 32 }
这里bis从文件file.txt按字节读取文件内容到字节数组bytes,bos将字节数组写入file1.txt文件,并在控制台输出。
ByteArrayInputStream, ByteArrayOutputStream:
1 import java.io.ByteArrayInputStream; 2 import java.io.ByteArrayOutputStream; 3 4 5 public class ByteStreamTest { 6 7 public static void main(String args[]) 8 { 9 //测试ByteArrayOutputStream 10 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 11 //289截断到一个字节后二进制为0000 0033,对应的ascii字符为‘!‘ 12 //原型为write(int),将289写入到字节数组 13 baos.write(289); 14 15 byte[] b = baos.toByteArray(); 16 System.out.println(b.length); 17 //这里字节又被强转为两个字节的char类型,二进制为0000 0000 0000 0033 18 System.out.println((char)b[0]); 19 20 ByteArrayInputStream bais = new ByteArrayInputStream(b); 21 //这里read()将1个字节0000 0033转为整型返回,之后又强转为char 22 char c = (char) bais.read(); 23 System.out.println(c); 24 } 25 }
ByteArrayOutputStream中有字节缓冲,这里将289写入一个字节,发生截断,使用baos.toByteArray()取出字节数组,打印其长度为1,用该字节数组初始化ByteArrayInputStream,从bais取出该字节数组中的一个字节,强转为char类型,33对应的assci码为!,所以输出!。
1 ! !
FileReader, FileWriter
1 import java.io.FileReader; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 5 6 public class FileReaderWriterTest { 7 public static void main(String args[]) throws IOException 8 { 9 FileReader fr = new FileReader("file.txt"); 10 char[] s1 = new char[100]; 11 //按字符也就是两个字节读入到字节数组 12 fr.read(s1); 13 for (int i = 0; i < s1.length; i++) 14 System.out.print(s1[i]); 15 16 17 char[] s2 = {‘你‘, ‘好‘}; 18 FileWriter fw = new FileWriter("fileTo.txt"); 19 //将字符数组写入到文件 20 fw.write(s2); 21 22 //关闭文件,刷新缓冲区 23 fw.close(); 24 fr.close(); 25 26 27 } 28 29 }
fr从file.txt读取内容到字符数组,并将其打印,fw将字符数组写入文件fileTo.txt。
CharArrayReader, CharArrayWriter
1 import java.io.CharArrayReader; 2 import java.io.CharArrayWriter; 3 import java.io.IOException; 4 5 6 public class CharArrayReaderWriterTest { 7 8 public static void main(String[] args) throws IOException 9 { 10 CharArrayWriter caw = new CharArrayWriter(); 11 12 char[] c = {‘字‘, ‘符‘, ‘数‘, ‘组‘}; 13 //将字符数组写入缓冲区 14 caw.write(c); 15 //获取字符数组 16 char[] cc = caw.toCharArray(); 17 String s = new String(cc); 18 System.out.println(s); 19 20 //使用字符数组c构造CharArrayReader 21 CharArrayReader car = new CharArrayReader(c); 22 int a; 23 //读取每个字符 24 while ( (a = car.read()) != -1) 25 { 26 System.out.println((char)a); 27 } 28 29 } 30 }
输出:
字符数组
字
符
数
组
BufferedInputStream, BufferedOutputStream:
1 public class BufferedStream { 2 3 public static void main(String args[]) throws IOException 4 { 5 //修饰流,需要由InputStream,OutputStream构造,本质上还是按字节读入写出,但是增加了缓冲 6 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt")); 7 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("fileTo.txt")); 8 9 int bytesCount = 0; 10 int a; 11 while ( (a = bis.read()) != -1) 12 { 13 bytesCount++; 14 bos.write(a); 15 } 16 System.out.println("读取的字节数:"+bytesCount); 17 18 19 //记得关闭流,刷新缓冲区 20 bis.close(); 21 bos.close(); 22 } 23 }
BufferedInputStream, BufferedOutputStream是修饰流,还是按字节读取,增加了缓冲机制,这是实现了按字节将file.txt的内容复制到fileTo.txt中。
BufferedReader, BufferedWriter:
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 8 public class BufferedReadWriterTest { 9 10 public static void main(String args[]) throws IOException 11 { 12 BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt")); 13 BufferedReader br = new BufferedReader(new FileReader("file.txt")); 14 15 bw.write("第一行\n"); 16 bw.write("第二行\n"); 17 bw.write("第三行\n"); 18 bw.flush(); 19 20 21 String s; 22 while ( (s = br.readLine()) != null) 23 { 24 System.out.println(s); 25 } 26 27 br.close(); 28 bw.close(); 29 30 31 32 } 33 }
BufferedReader, BufferedWriter是修饰流,提供按行读取的功能,这里向文件写入三行数据,然后按行读取。
第一行
第二行
第三行
标签:
原文地址:http://www.cnblogs.com/gatsbydhn/p/5109834.html