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] |