您的位置:首页 > 编程语言 > PHP开发

FileOutputStream和FileInputStream使用方法说明

2014-12-26 11:03 477 查看

package fileTest;




import java.io.File;


import java.io.FileInputStream;


import java.io.FileOutputStream;




public class FileDemo {


    public static void main(String[] arg){


        File f = new File("1.txt");

        


        


        


        //向文件里写如"Hello"字符串.


        try    {


            //要写入的数据转换成字节数组


            byte[] buf = "Hello".getBytes();


            


            //如果1.txt存在,则删除1.txt里面的内容,文本所有内容变为Hello


            //如果1.txt不存在,在新建1.txt文本,写入Hello


            FileOutputStream out = new FileOutputStream(f);


            


            out.write(buf);


        


            out.close();


        }catch(Exception e)    {


            System.out.println(e);


        }


        


        


        


        //读取文件中的内容。可在程序中单独使用,不用关心"写"是否存在.


        try    {


            //只要f存在就可以读出f的内容,与写操作代码没有关联性.


            FileInputStream in = new FileInputStream(f);


            


            byte[] buf = new byte[1024];    


            


            int len=in.read(buf);        //从流中读取内容


            String str = new String(buf,0,len);


            


            System.out.println(str);    //打印f文件的内容.        


        }catch(Exception e)    {


            System.out.println(e);


        }


                


        


        


    }


}

转自:http://www.blogjava.net/myfly/archive/2008/09/01/226084.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息