Java输入输出流

转自http://blog.csdn.net/hguisu/article/details/7418161

下面是提取一些精华部分:

  1. Java的IO模型设计非常优秀,它使用Decorator模式,按功能划分Stream,您可以动态装配这些Stream,以便获得您需要的功能。例如,您需要一个具有缓冲的文件输入流,则应当组合使用FileInputStream和BufferedInputStream。 
  2. 在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:

             标准输入输出,文件的操作,网络上的数据流,字符串流,对象流,zip文件流等等,java中将输入输出抽象称为流,就好像水管,将两个容器连接起来。将数据冲外存中读取到内存中的称为输入流,将数据从内存写入外存中的称为输出流。


  3. java有流式部分(字节流,字符流),非流式部分(文件),其他(文件读取部分的与安全相关的类,如:SerializablePermission类)

  4. java的IO流类库包括四个基本类,inputstream,outputstream,reader,writer及各种派生类。InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit) ; 而Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题。Reader类能够将输入流中采用其他编码类型的字符转换为Unicode字符,然后在内存中为其分配内存 ,Writer类能够将内存中的Unicode字符转换为其他编码类型的字符,再写到输出流中。

  5. IOException异常类的子类:

public class  EOFException :
   非正常到达文件尾或输入流尾时,抛出这种类型的异常。
public class FileNotFoundException:
   当文件找不到时,抛出的异常。
public class InterruptedIOException:

   当I/O操作被中断时,抛出这种类型的异常


  1. 字节流inputstream,outputstream范例:

文件输入流:
import java.io.IOException;  
import java.io.FileInputStream;  
;  
public class TestFile {  
    public static void main(String args[]) throws IOException {  
        try{      
               FileInputStream rf=new   FileInputStream("InputFromFile.java");  
               int n=512;   byte  buffer[]=new  byte[n];     
               while((rf.read(buffer,0,n)!=-1)&&(n>0)){  
                   System.out.println(new String(buffer) );  
                }  
                System.out.println();  
                rf.close();  
        } catch(IOException  IOe){        
              System.out.println(IOe.toString());  
        }  
  
    }  
  
}
文件输出流:
<pre name="code" class="java">import java.io.IOException;  
import java.io.FileOutputStream;  
public class TestFile {  
    public static void main(String args[]) throws IOException {  
        try {  
            System.out.println("please Input from      Keyboard");  
            int count, n = 512;  
            byte buffer[] = new byte[n];  
            count = System.in.read(buffer);  
            FileOutputStream wf = new FileOutputStream("d:/myjava/write.txt");  
            wf.write(buffer, 0, count);  
            wf.close(); // 当流写操作结束时,调用close方法关闭流。  
            System.out.println("Save to the write.txt");  
        } catch (IOException IOe) {  
            System.out.println("File Write Error!");  
        }  
    }  
  
} 

 
文件输入流和输出流的应用:
import java.io.File;  
import java.io.IOException;  
import java.io.FileOutputStream;  
import java.io.FileInputStream;  
  
public class TestFile {  
    public static void main(String args[]) throws IOException {  
        try {  
            File inFile = new File("copy.java");  
            File outFile = new File("copy2.java");  
            FileInputStream finS = new FileInputStream(inFile);  
            FileOutputStream foutS = new FileOutputStream(outFile);  
            int c;  
            while ((c = finS.read()) != -1) {  
                foutS.write(c);  
            }  <pre name="code" class="java">import java.io.*;  
  
public class ReadWriteToFile {  
    public static void main(String args[]) throws IOException {  
        InputStreamReader sin = new InputStreamReader(System.in);  
        BufferedReader bin = new BufferedReader(sin);  
        FileWriter out = new FileWriter("myfile.txt");  
        BufferedWriter bout = new BufferedWriter(out);  
        String s;  
        while ((s = bin.readLine()).length() > 0) {  
            bout.write(s, 0, s.length());  
        }  
  
    }  
}
finS.close(); foutS.close(); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } } }
 
缓冲输入输出流(从键盘输入流到内存示例):<pre name="code" class="java">import java.io.*;  
  
public class ReadWriteToFile {  
    public static void main(String args[]) throws IOException {  
        InputStreamReader sin = new InputStreamReader(System.in);  
        BufferedReader bin = new BufferedReader(sin);  
        FileWriter out = new FileWriter("myfile.txt");  
        BufferedWriter bout = new BufferedWriter(out);  
        String s;  
        while ((s = bin.readLine()).length() > 0) {  
            bout.write(s, 0, s.length());  
        }  
  
    }  
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值