5.27 outputstream 和 inputstream

I/O类包括节点流类和包装流类

FileOutputStream和FileInputStream创建磁盘文件的输入输出流对象

创建FileInputStream实例对象时,指定的文件应当是存在和可读的,创建FileOutputStream实例对象时,如果指定的文件已经存在,这个文件中的原来内容将被清除

创建FileOutputStream实例对象时,可以指定还不存在的文件名,不能指定一个已被其他程序打开的文件

实例:

[java] www.2cto.com
import java.io.*; 
 
public class FileStreamTest { 
    public static void main(String [] args) { 
        try { 
            FileOutputStream out = new FileOutputStream("hello.txt"); 
             
            out.write("love_snooker".getBytes()); 
             
            out.close(); 
             
            File file = new File("hello.txt"); 
            FileInputStream in = new FileInputStream(file); 
             
            byte[] buf = new byte[12]; 
            int len = in.read(buf); 
             
            System.out.println(buf); 
            System.out.println(new String(buf, 0, len)); 
             
            in.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

其他函数请参照jdk帮助文档

 
posted @ 2014-05-27 21:38  behappylee  阅读(127)  评论(0编辑  收藏  举报