A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孔祥攀 黑马帝   /  2011-8-5 17:09  /  2515 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

RandomAccessFile类和FileOutputStream类,分别是如何使用的呀?由其是这两个的write方法,好像都是用来写文件的呀

1 个回复

倒序浏览
黑马网友  发表于 2011-8-5 18:35:26
沙发
RandomAccessFile类:支持随机访问,但是只限于操作文件。[code]import java.io.*;
public class testRandom {
                public static void main(String [] args) throws Exception
                {
                        RandomAccessFile ra=new RandomAccessFile("t12tt.txt","rw"); //这里不一样
                        ra.write("hello heima".getBytes());
                        ra.close();
                       
                        RandomAccessFile raf=new RandomAccessFile("t12tt.txt","r");
                        int len=0;
                        byte [] buf=new byte [1024];
                        String strname=null;
                        len=raf.read(buf);
                        strname=new String(buf,0,len);
                        System.out.println(strname);
                        raf.close();
                        }
                }[/code]FileOutputStream类:创建一个此类实例对象时,指定的文件已经存在,则会覆盖清除原来的内容。[code]import java.io.*;
public class testRandom {
                public static void main(String [] args) throws Exception
                {
                        FileOutputStream out=new FileOutputStream("t12tt.txt");   //这里不一样
                        out.write("hello heima".getBytes());
                        out.close();               
                        FileInputStream raf=new FileInputStream("t12tt.txt");
                        int len=0;
                        byte [] buf=new byte [1024];
                        String strname=null;
                        len=raf.read(buf);
                        strname=new String(buf,0,len);
                        System.out.println(strname);
                        raf.close();
                        }
                }[/code]

评分

参与人数 1技术分 +2 收起 理由
老罗 + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马