java IO流涉及到的class 和 interface.
字节流和字符流的区别:
字节流:字节流是一个字节一个字节读取,和一个字节写入,使用于传输任何类型的数据。
字符流:基于字节流,每次需要查询编码表,返回对应的字符,使用于传输字符数据。
一般情况,如果传输的是纯文本数据,建议使用字符流,其他的用字节流。
这些图是我从网上下的,我想对学习java的IO会很有帮助。有什么指点的,还请高手指点。
java IO流 class structure picture.
InputStream与OutputStream对应图
Reader与Writer对应图
代码部分:(有点儿多,技术不精,还请高手指点)
01 | package com.wangbiao.test; |
04 | import java.io.FileInputStream; |
05 | import java.io.FileNotFoundException; |
06 | import java.io.InputStream; |
14 | public class Test_InputStream { |
17 | public static void test_read_first(File file) throws Exception{ |
18 | InputStream in= new FileInputStream(file); |
19 | byte [] b= new byte [( int ) file.length()]; |
22 | System.out.println( new String(b)); |
24 | public static void test_read_second(File file) throws Exception{ |
25 | InputStream in= new FileInputStream(file); |
26 | byte [] b= new byte [( int ) file.length()]; |
29 | System.out.println( new String(b, 0 ,len)); |
32 | public static void test_read_third(File file) throws Exception{ |
33 | InputStream in= new FileInputStream(file); |
35 | byte [] b= new byte [ 1024 ]; |
38 | while ((temp=in.read())!=- 1 ){ |
43 | System.out.println( new String(b, 0 ,len)); |
47 | public static void main(String[] args) throws Exception { |
48 | File file= new File( "D:" +File.separator+ "test.txt" ); |
51 | test_read_third(file); |
01 | package com.wangbiao.test; |
04 | import java.io.FileNotFoundException; |
05 | import java.io.FileOutputStream; |
06 | import java.io.OutputStream; |
11 | * OutputStream以及其常用子类的用法。 |
15 | public class Test_OutputStream { |
19 | public static void test_write_first(File file) throws Exception{ |
20 | OutputStream out= new FileOutputStream(file, true ); |
21 | String str= "test java io" ; |
22 | byte [] b=str.getBytes(); |
27 | public static void test_write_second(File file) throws Exception{ |
28 | OutputStream out= new FileOutputStream(file, true ); |
30 | String str= "\r\n test java io" ; |
31 | byte [] b=str.getBytes(); |
32 | for ( int i = 0 ; i < b.length; i++) { |
38 | public static void main(String[] args) throws Exception { |
39 | File file= new File( "D:" +File.separator+ "test.txt" ); |
42 | test_write_second(file); |
01 | package com.wangbiao.test; |
04 | import java.io.FileNotFoundException; |
05 | import java.io.FileReader; |
16 | public class Test_Reader { |
19 | public static void test_read_first(File file) throws Exception{ |
20 | Reader in= new FileReader(file); |
22 | char [] c= new char [ 1024 ]; |
25 | System.out.println( new String(c, 0 ,len)); |
28 | public static void test_read_second(File file) throws Exception{ |
29 | Reader in= new FileReader(file); |
31 | char [] c= new char [( int ) file.length()]; |
34 | System.out.println( new String(c)); |
37 | public static void test_read_third(File file) throws Exception{ |
38 | Reader in= new FileReader(file); |
40 | char [] c= new char [ 1024 ]; |
43 | while ((temp=in.read())!=- 1 ){ |
48 | System.out.println( new String(c, 0 ,len)); |
51 | public static void main(String[] args) throws Exception{ |
52 | File file= new File( "D:" +File.separator+ "test.txt" ); |
53 | test_read_first(file); |
54 | test_read_second(file); |
55 | test_read_third(file); |
01 | package com.wangbiao.test; |
04 | import java.io.FileWriter; |
05 | import java.io.IOException; |
15 | public class Test_Writer { |
17 | public static void test_write_first(File file) throws Exception{ |
18 | Writer out= new FileWriter(file); |
19 | String str= "\r\n love java" ; |
25 | public static void test_write_second(File file) throws Exception{ |
26 | Writer out= new FileWriter(file, true ); |
27 | String str= "\r\n love java" ; |
33 | public static void main(String[] args) throws Exception { |
34 | File file= new File( "D:" +File.separator+ "test.txt" ); |
35 | test_write_first(file); |
36 | test_write_second(file); |
01 | package com.wangbiao.test; |
04 | import java.io.FileNotFoundException; |
05 | import java.io.FileOutputStream; |
06 | import java.io.FileWriter; |
07 | import java.io.OutputStream; |
19 | public class Compare_OutputStream_Writer { |
27 | public static void test_outputStream(File file) throws Exception{ |
28 | OutputStream out= new FileOutputStream(file); |
29 | String str= "xxxxxxxxxxxx" ; |
30 | byte [] b=str.getBytes(); |
35 | public static void test_writer(File file) throws Exception{ |
36 | Writer out= new FileWriter(file, true ); |
37 | String str= "\r\nxxxxxxxxxx" ; |
45 | public static void main(String[] args) throws Exception{ |
46 | File file= new File( "D:" +File.separator+ "test.txt" ); |
47 | test_outputStream(file); |
01 | package com.wangbiao.test; |
03 | import java.io.ByteArrayInputStream; |
04 | import java.io.ByteArrayOutputStream; |
06 | import java.io.IOException; |
07 | import java.io.InputStream; |
08 | import java.io.OutputStream; |
13 | * 内存操作流ByteArrayInputStream and ByteArrayOutputStream. |
17 | public class Test_ByteArray { |
20 | public static void test(File file) throws Exception{ |
21 | String str= "work hard" ; |
22 | byte [] b=str.getBytes(); |
27 | InputStream in= new ByteArrayInputStream(b); |
28 | OutputStream out= new ByteArrayOutputStream(); |
30 | while ((temp=in.read())!=- 1 ) { |
35 | out.write(Character.toUpperCase(c)); |
38 | String string=out.toString(); |
41 | System.out.println(string); |
45 | public static void main(String[] args) throws Exception{ |
46 | File file= new File( "D:" +File.separator+ "test.txt" ); |
01 | package com.wangbiao.test; |
04 | import java.io.FileInputStream; |
05 | import java.io.FileOutputStream; |
06 | import java.io.FileWriter; |
07 | import java.io.InputStream; |
08 | import java.io.InputStreamReader; |
09 | import java.io.OutputStreamWriter; |
15 | * 转换流InputStreamReader and OutputStreamWriter,都是将字节流装换成字符流 |
19 | public class Test_InputSteamReader { |
22 | public static void test_inputStreamReader(File file) throws Exception{ |
23 | InputStream in= new FileInputStream(file); |
24 | Reader rd= new InputStreamReader(in); |
25 | char [] c= new char [( int ) file.length()]; |
28 | System.out.println( new String(c)); |
32 | public static void test_outputStreamWriter(File file) throws Exception{ |
35 | Writer out= new OutputStreamWriter( new FileOutputStream(file)); |
40 | public static void main(String[] args) throws Exception { |
41 | File file= new File( "D:" +File.separator+ "test" +File.separator+ "test.txt" ); |
42 | file.setWritable( true ); |
43 | test_inputStreamReader(file); |
44 | test_outputStreamWriter(file); |
01 | package com.wangbiao.test; |
03 | import java.io.IOException; |
04 | import java.io.PipedInputStream; |
05 | import java.io.PipedOutputStream; |
10 | * PipedInputStream and PipedOutputStream,用于不同线程之间流的数据传输 |
14 | public class Test_Piped { |
16 | public static void main(String[] args) throws Exception { |
17 | PipedOutputStream_Class send= new PipedOutputStream_Class(); |
18 | PipedInputStream_Class receive= new PipedInputStream_Class(); |
20 | send.getOutput().connect(receive.getInput()); |
22 | new Thread(send).start(); |
23 | new Thread(receive).start(); |
28 | class PipedInputStream_Class implements Runnable{ |
29 | private PipedInputStream input= null ; |
30 | public PipedInputStream_Class() { |
31 | this .input= new PipedInputStream(); |
37 | byte [] b= new byte [ 1024 ]; |
41 | while ((temp=input.read())!=- 1 ){ |
46 | } catch (IOException e) { |
49 | System.out.println( new String(b, 0 ,len)); |
52 | public PipedInputStream getInput() { |
58 | class PipedOutputStream_Class implements Runnable{ |
59 | private PipedOutputStream output= null ; |
60 | public PipedOutputStream_Class() { |
61 | this .output= new PipedOutputStream(); |
68 | output.write(str.getBytes()); |
70 | } catch (IOException e) { |
76 | public PipedOutputStream getOutput() { |
001 | package com.wangbiao.test; |
004 | import java.io.FileInputStream; |
005 | import java.io.FileNotFoundException; |
006 | import java.io.FileOutputStream; |
007 | import java.io.InputStream; |
008 | import java.io.PrintStream; |
014 | * @author WangBiao 2013-4-27上午10:44:34 |
016 | public class Test_PrintStream { |
032 | public static void test_printStream(File file) throws Exception{ |
033 | PrintStream ps= new PrintStream( new FileOutputStream(file)); |
035 | ps.print( "ddddddddd" ); |
036 | ps.println( "ccccccccccc" ); |
042 | public static void test_format(File file) throws Exception{ |
043 | PrintStream ps= new PrintStream(file); |
044 | String name= "zhangsan" ; |
046 | float score=( float ) 97.5 ; |
048 | ps.printf( "姓名为:%s;年龄为:%d;分数为:%f;性别为:%c " , name,age,score,sex); |
053 | public static void test_sys_in() throws Exception { |
054 | InputStream in=System.in; |
057 | byte [] b= new byte [ 1024 ]; |
058 | System.out.print( "请输入内容:" ); |
060 | StringBuffer sb= new StringBuffer(); |
063 | while ((temp=in.read())!=- 1 ){ |
070 | System.out.println(sb.toString()); |
074 | public static void test_sys_out() { |
078 | public static void test_sys_err() { |
083 | public static void test_sys_in_redirect(File file) throws Exception { |
084 | System.setIn( new FileInputStream(file)); |
085 | InputStream input=System.in; |
086 | byte [] b= new byte [ 1024 ]; |
088 | System.out.println( new String(b)); |
092 | public static void test_sys_out_redirect(File file) throws Exception { |
093 | PrintStream ps= new PrintStream(file); |
095 | System.out.println( "love java" ); |
100 | public static void test_sys_err_redirect(File file) throws Exception { |
101 | PrintStream ps= new PrintStream(file); |
103 | System.err.println( "love java" ); |
107 | public static void main(String[] args) throws Exception { |
108 | File file= new File( "D:" +File.separator+ "test" +File.separator+ "test.txt" ); |
113 | test_sys_out_redirect(file); |
114 | test_sys_err_redirect(file); |
01 | package com.wangbiao.test; |
03 | import java.io.DataInputStream; |
04 | import java.io.DataOutputStream; |
06 | import java.io.FileInputStream; |
07 | import java.io.FileOutputStream; |
08 | import java.io.InputStream; |
09 | import java.io.OutputStream; |
13 | * DataInputStream and DataOutputStream,就我的理解是,这两个类不同之处是可以读取和写入基本数据类型的数据。 |
14 | * 两者搭配起来使用,并且写入和读取的顺序要严格一样 |
18 | public class Test_DataInputStream { |
21 | public static void test_DataInputStream(File file) throws Exception{ |
22 | InputStream in= new FileInputStream(file); |
23 | DataInputStream input= new DataInputStream(in); |
33 | while ((c=input.readChar())!= '\t' ){ |
37 | name= new String(temp, 0 ,len); |
40 | classNum=input.readInt(); |
42 | System.out.printf( "姓名为:%s; 分数为: %5.2f; 班级为:%d\n" ,name,fl,classNum); |
44 | } catch (Exception e) { |
55 | public static void test_DataOutputStream(File file) throws Exception{ |
56 | OutputStream out= new FileOutputStream(file); |
57 | DataOutputStream output= new DataOutputStream(out); |
58 | String[] name= new String[]{ "zhangsan" , "lisi" , "wangwu" }; |
59 | float [] score={ 95 .5f, 86 .5f,100f}; |
60 | int [] classNum={ 1 , 1 , 3 }; |
61 | for ( int i = 0 ; i < name.length; i++) { |
62 | output.writeChars(name[i]); |
63 | output.writeChar( '\t' ); |
64 | output.writeFloat(score[i]); |
65 | output.writeChar( '\t' ); |
66 | output.writeInt(classNum[i]); |
67 | output.writeChar( '\n' ); |
71 | public static void main(String[] args) throws Exception{ |
72 | File file= new File( "D:" +File.separator+ "test" +File.separator+ "test.txt" ); |
73 | test_DataOutputStream(file); |
74 | test_DataInputStream(file); |
01 | package com.wangbiao.test; |
04 | import java.io.FileInputStream; |
05 | import java.io.FileNotFoundException; |
06 | import java.io.InputStream; |
07 | import java.io.SequenceInputStream; |
11 | * SequenceInputStream,合并流的主要功能是将两个文件的内容合并成一个文件 |
15 | public class Test_SequenceInputStream { |
17 | public static void test(File file1,File file2,File all) throws Exception{ |
21 | InputStream input_first= new FileInputStream(file1); |
22 | InputStream input_second= new FileInputStream(file2); |
23 | InputStream input_all= new FileInputStream(all); |
25 | SequenceInputStream sq= new SequenceInputStream(input_first,input_second); |
26 | byte [] temp= new byte [ 1024 ]; |
29 | while ((data=sq.read())!=- 1 ){ |
30 | temp[len]=( byte ) data; |
33 | System.out.println( new String(temp, 0 ,len)); |
37 | public static void main(String[] args) throws Exception{ |
38 | File file1= new File( "D:" +File.separator+ "test" +File.separator+ "test.txt" ); |
39 | File file2= new File( "D:" +File.separator+ "test" +File.separator+ "good.txt" ); |
40 | File all= new File( "D:" +File.separator+ "test" +File.separator+ "all.txt" ); |
41 | test(file1,file2,all); |
001 | package com.wangbiao.test; |
004 | import java.io.FileInputStream; |
005 | import java.io.FileOutputStream; |
006 | import java.io.InputStream; |
007 | import java.io.OutputStream; |
008 | import java.util.zip.ZipEntry; |
009 | import java.util.zip.ZipFile; |
010 | import java.util.zip.ZipInputStream; |
011 | import java.util.zip.ZipOutputStream; |
016 | * ZipOutputStream和ZipInputStream为压缩流,每一个压缩文件都可以称为ZipFile. |
020 | public class Test_ZipOutputStream { |
023 | public static void test_zipOutputStream(File file) throws Exception{ |
024 | InputStream input= null ; |
025 | File zipFile= new File( "D:" +File.separator+ "test" +File.separator+ "test.zip" ); |
026 | ZipOutputStream zipOut= new ZipOutputStream( new FileOutputStream(zipFile));; |
027 | if (file.isDirectory()){ |
028 | File[] files=file.listFiles(); |
029 | for ( int i = 0 ; i < files.length; i++) { |
030 | input= new FileInputStream(files[i]); |
031 | zipOut.putNextEntry( new ZipEntry(file.getName()+File.separator+files[i].getName())); |
032 | zipOut.setComment(files[i].getName()); |
034 | while ((temp=input.read())!=- 1 ){ |
039 | input= new FileInputStream(file); |
040 | zipOut.putNextEntry( new ZipEntry(file.getName())); |
041 | zipOut.setComment( "test zipoutputStream" ); |
043 | while ((temp=input.read())!=- 1 ){ |
055 | public static void test_zipInputStream() throws Exception{ |
056 | File file= new File( "D:" +File.separator+ "test" +File.separator+ "test.zip" ); |
057 | InputStream input= null ; |
058 | OutputStream output= null ; |
061 | ZipFile zipFile= new ZipFile(file); |
062 | ZipInputStream zipInputStream= new ZipInputStream( new FileInputStream(file)); |
064 | while ((entry=zipInputStream.getNextEntry())!= null ){ |
065 | System.out.println( "解压缩" +entry.getName()); |
066 | outFile= new File( "D:" +File.separator+entry.getName()); |
067 | if (!outFile.getParentFile().exists()){ |
068 | outFile.getParentFile().mkdir(); |
070 | if (!outFile.exists()){ |
071 | outFile.createNewFile(); |
073 | input=zipFile.getInputStream(entry); |
074 | output= new FileOutputStream(outFile); |
076 | while ((temp=input.read())!=- 1 ){ |
080 | zipInputStream.close(); |
086 | public static void test_zipFile() throws Exception{ |
087 | File file= new File( "D:" +File.separator+ "test" +File.separator+ "test.zip" ); |
088 | ZipFile zipFile= new ZipFile(file); |
089 | System.out.println(zipFile.getName()); |
092 | public static void main(String[] args) throws Exception{ |
096 | test_zipInputStream(); |
01 | package com.wangbiao.test; |
03 | import java.io.ByteArrayInputStream; |
05 | import java.io.FileInputStream; |
06 | import java.io.IOException; |
07 | import java.io.InputStream; |
08 | import java.io.PushbackInputStream; |
12 | * PushbackInputStream and PushbackReader 为回退流,在数据读取的时候,都是按着顺序读取的,回退流就是讲读取进来的某些数据重新退回到输入流的缓冲区中。 |
16 | public class Test_PushbackInputStream { |
19 | public static void test() throws IOException{ |
20 | InputStream input= new ByteArrayInputStream( "www.oschina.net" .getBytes()); |
21 | PushbackInputStream pushbackInput= new PushbackInputStream(input); |
23 | while ((temp=pushbackInput.read())!=- 1 ){ |
25 | pushbackInput.unread(temp); |
26 | temp=pushbackInput.read(); |
27 | System.out.print( "濾掉" +( char )temp); |
29 | System.out.print(( char )temp); |
33 | pushbackInput.close(); |
37 | public static void main(String[] args) throws IOException{ |