类型 | 字符流 | 字节流 | ||
文件 | FileReader | FileInputStream | ||
FileWriter | FileOutputStream |
|||
内存:数组 | CharArrayReader | ByteArrayInputStream | ||
CharArrayWriter | ByteArrayOutputStream |
|||
内存:字符串 | StringReader | |||
StringWriter | ||||
管道 | PipedReader | PipedInputStream | ||
PipedWriter | PipedOutputStream | |||
--关于节点流的几个实用例子:
FileInputStream和FileOutStream
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class TestStream { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FileOutputStream out = null; try { out = new FileOutputStream("hello.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out.write("hello baby!".getBytes()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } FileInputStream in = null; try { in = new FileInputStream("hello.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ byte[] buf = new byte[1024]; int len = in.read(buf); System.out.print(new String(buf,0,len)); in.close(); }catch(IOException e){ e.printStackTrace(); } } }
FileReader和FileWriter
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class TestStream { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { FileWriter out = new FileWriter("hello.txt"); out.write("hello baby!"); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileReader in = new FileReader("hello.txt"); char[] buf = new char[1024];//char类型,不是byte int len = in.read(buf); System.out.println(new String(buf,0,len)); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
-----------------内存数组 字节流--
构造函数:ByteArrayOutputStream(),创建一个32个字节的缓冲区,数据过多时会自动增长缓冲区的大小。
ByteArrayOutputStream(int size),创建size个字节的缓冲区,数据过多时会自动增长缓冲区的大小。
·编程实例:编写一个函数将一个输入流中的所有英文字母转变成大写字母,然后将结果写入到一个输出流对象,并用这个函数将一个字符串中的所有字符转换成大写字母。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class TestStream { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub byte[] buf = new String("abcdefghijklmnopqrstuvwxyz").getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(buf); ByteArrayOutputStream out = new ByteArrayOutputStream(); charChangeToUp(in,out);//输出流中现存有内容 System.out.println(out.toString()); try { in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static void charChangeToUp (InputStream in,OutputStream out){ int buf = 0; try { while((buf = in.read())!=-1){//未读取到文件的末尾则循环 out.write(Character.toUpperCase(buf));//将内容写入输出流 } in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }