Java IO总结

来源:互联网 发布:psv天才麻将少女淘宝 编辑:程序博客网 时间:2024/05/02 02:25

文字参考:http://blog.csdn.net/ilibaba/article/details/3955799

代码参考:Java程序设计 / 雍俊海编著









输入输出理解:输入流是用来读取的,输出流是用来写入的。输入输出流是相对于你的程序说的

Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。

 InputStreamReader

是从字节流到字符流的桥梁:它读入字节,并根据指定的编码方式,将之转换为字符流。使用的编码方式可能由名称指定,或平台可接受的缺省编码方式。

InputStreamReader(InputStream)用缺省的字符编码方式,创建。

InputStreamReader(InputStream,String)用已命名的字符编码方式,创建。

 

Java IO 的一般使用原则  

一、按数据来源(去向)分类:

1 、是文件: FileInputStream, FileOutputStream,(字节流 )FileReader, FileWriter(字符 )

2 、是 byte[] ByteArrayInputStream,ByteArrayOutputStream(字节流 )

3 、是 Char[]: CharArrayReader,CharArrayWriter(字符流 )

4 、是 String:StringBufferInputStream, StringBufferOuputStream (字节流 )StringReader, StringWriter(字符流 )

5 、网络数据流: InputStream, OutputStream,(字节流 ) Reader, Writer(字符流 )

二、按是否格式化输出分:

1 、要格式化输出: PrintStream, PrintWriter

 

三、按是否要缓冲分:

1 、要缓冲: BufferedInputStream,BufferedOutputStream,(字节流 ) BufferedReader, BufferedWriter(字符流 )

 

四、按数据格式分:

1 、二进制格式(只要不能确定是纯文本的) : InputStream, OutputStream及其所有带 Stream结束的子类

2 、纯文本格式(含纯英文与汉字或其他编码方式); Reader, Writer及其所有带 Reader, Writer的子类

 

五、按输入输出分:

1 、输入: Reader, InputStream类型的子类

2 、输出: Writer, OutputStream类型的子类

 

六、特殊需要:

1 、从 Stream Reader,Writer的转换类: InputStreamReader,OutputStreamWriter

2 、对象输入输出: ObjectInputStream,ObjectOutputStream

3 、进程间通信: PipeInputStream,PipeOutputStream, PipeReader, PipeWriter

4 、合并输入: SequenceInputStream

5 、更特殊的需要: PushbackInputStream,PushbackReader, LineNumberInputStream, LineNumberReader

 

决定使用哪个类以及它的构造进程的一般准则如下(不考虑特殊需要):

首先,考虑最原始的数据格式是什么:原则四

 

第二,是输入还是输出:原则五

 

第三,是否需要转换流:原则六第 1

 

第四,数据来源(去向)是什么:原则一

 

第五,是否要缓冲:原则三(特别注明:一定要注意的是 readLine()是否有定义,有什么比 read, write更特殊的输入或输出方法)

 

第六,是否要格式化输出:原则二

实例

//1)Echo.javapackage cn.edu.sprying.io;/** * 输入流类 * System.in BufferedInputStream 标准输入 * int read() */import java.io.IOException;import java.io.InputStream;public class Echo {public static void b_echo(InputStream in){try {while(true){int i=in.read();if(i==-1)break;char c=(char)i;System.out.print(c);}} catch (IOException e) {// TODO: handle exceptionSystem.err.println("发生异常:"+e);e.printStackTrace();}}public static void main(String[] args){b_echo(System.in);}}//2)EchoFile.javapackage cn.edu.sprying.io;/** * 子类FileInputStream 输入流类 * public int read() throws IOException * public void close() throws IOException */import java.io.FileInputStream;import java.io.IOException;public class EchoFile {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {FileInputStream f=new FileInputStream("test.txt");int i;int b=f.read();for(i=0;b!=-1;i++){System.out.print((char)b);b=f.read();}System.out.println();System.out.println("文件\"test.txt\"字节数为"+i);f.close();} catch (IOException e) {// TODO: handle exception}}}//3)Write.javapackage cn.edu.sprying.io;/** * 输出流类 * 标准输出System.out * flush() String  getBytes */import java.io.IOException;import java.io.OutputStream;public class Write {public static void b_write(OutputStream out){String s="dfadsfasdf我是";byte[] c=s.getBytes();try {out.write(c);out.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubb_write(System.out);}}//4)WriteFile.javapackage cn.edu.sprying.io;/** * FileOutputStream的使用 * public void write(byte[] b) throws IOException * public void flush() throws IOException * String的使用 * public byte[] getBytes()  */import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class WriteFile {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileOutputStream f=null;try {f=new FileOutputStream("out.txt");String s="fdafa我是";byte[] b=s.getBytes();f.write(b);f.flush();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {f.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//5)J_PrintStream.javapackage cn.edu.sprying.io;/** * 一种重要的输出流类 * public PrintStream(OutputStream out) * public PrintStream(OutputStream out,boolean autoFlush) * public PrintStream(String fileName) throws FileNotFoundException * flush() * printf(...); */import java.io.FileNotFoundException;import java.io.PrintStream;public class J_PrintStream {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {PrintStream p=new PrintStream("printstream.txt");//若不存在printstream.txt,自动创建p.printf("%1$d+%2$d=%3$d",1,2,(1+2));p.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//6) J_BufferedInputStream.javapackage cn.edu.sprying.io;/** * 输入流类,比较有无Buffered时间, * 加Buffered后前后。相同:方法一样;不同:构造函数,Buffered参数需InputStream or OutputStream类型。 * 熟悉类Date使用 */import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Date;public class J_BufferedInputStream {private static String fileName = "hs_err_pid2036.log";/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {FileInputStream f=new FileInputStream(fileName);Date date1=new Date();while(f.read()!=-1);Date date2=new Date();//byte[] b1=null;//f.read(b1);//出现错误//System.out.write(b1);BufferedInputStream b=new BufferedInputStream(f);while(b.read()!=-1);Date date3=new Date();f.close();b.close();System.out.printf("FileInputStream:%1$d%n",date2.getTime()-date1.getTime());System.out.printf("BufferedInputStream:%1$d", date3.getTime()-date2.getTime());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//7)Setin.javapackage cn.edu.sprying.io;/** * 标准输入输出流的重定向 * java.io.BufferedInputStreamSystem.in标准输入流 * java.io.PrintStreamSystem.out标准输出流 * java.io.PrintStreamSystem.err标准错误输出流 * public static void setIn(InputStream in) * public static void setOut(PrintStream out) * public static void setErr(PrintStream err) */import java.io.FileInputStream;import java.io.FileNotFoundException;public class Setin {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileInputStream p;try {p = new FileInputStream("Echoout.txt");System.setIn(p);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Echo.b_echo(System.in);}}//8)FileReaderWriter.javapackage cn.edu.sprying.io;/** * 字符输出流类Writer抽象类 * public void write(String str) throws IOException; * public void write(char[] cbuf) throws IOException; * public void write(int c) throws IOException; * public abstract void flush() throws IOException; * public abstract void close() throws IOEXception; * 字符输入流类Reader抽象类 * public void read() throws IOException;,从文件中读入一个字符,当返回值是-1时,表示到达文件末尾 * public void read(char[] cbuf) throws IOException; *  * FileReader * public FileReader(String fileName) throws FileNotFoundExcepiton * ... */import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileReaderWriter {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileWriter fw;try {fw = new FileWriter("filewriter.txt");//若无文件,自动创建fw.write("有志者,事竟成");fw.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}FileReader fr;try {fr=new FileReader("filewriter.txt");for(int i=fr.read();i!=-1;i=fr.read()){System.out.print((char)i);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//9)BufferedReaderWriter.javapackage cn.edu.sprying.io;/** * 对文件读入写出,以熟练带缓存的读写器操作 * LineNumberReader * public String readLine() throws IOException 文件结束时,返回null * public int getLineNumber() * java.io.BufferedWriter * public void newLine() throws IOException */import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.LineNumberReader;public class BufferedReaderWriter {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {BufferedWriter bw=new BufferedWriter(new FileWriter("bufferedreaderwriter.txt"));bw.write("有志者,事竟成");bw.newLine();bw.write("苦心人,天不负");bw.newLine();bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {LineNumberReader br=new LineNumberReader(new FileReader("bufferedreaderwriter.txt"));for(String s=br.readLine();s!=null;s=br.readLine())System.out.println(br.getLineNumber()+":"+s);br.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//10)ReadDate.javapackage cn.edu.sprying.io;/** * 从控制台窗口读入数据 * public InputStreamReader(InputStream in) * public OutputStreamWriter(OutputStream out) */import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;public class ReadDate {public static void init(){try {PrintStream ps=new PrintStream("ReadData.txt");System.setOut(ps);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void printInfo(){System.out.println("输入整数还是浮点数?");System.out.println("\t0:退出; 1:整数; 2:浮点数");}public static int getInt(BufferedReader b){String s;try {s = b.readLine();int i=Integer.parseInt(s);return i;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return -1;} catch(NumberFormatException e){return -1;}}public static double getDouble(BufferedReader b){String s;try {s = b.readLine();double i=Double.parseDouble(s);return i;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return 0d;} catch(NumberFormatException e){return -1;}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//init();//玩玩重定向BufferedReader br=new BufferedReader(new InputStreamReader(System.in));int i;double d;while(true){printInfo();i=getInt(br);switch(i){case 0: return;case 1:System.out.println("\t请输入整数:");i=getInt(br);System.out.println("\t输入整数:"+i);break;case 2:System.out.println("\t请输入浮点数:");d=getDouble(br);System.out.println("\t输入浮点数:"+d);break;default:System.out.println("输入有误,重新输入");}}}}


原创粉丝点击