Java_RandomAccessFile和IO流

Java RandomAccessFile类

Test2.java 文件代码:

//读取文件内容
public class Test1RandomAccessFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//表示对该文件的访问权限是只读模式
RandomAccessFile raf=new RandomAccessFile("b.txt","r");
//读取一个字节
int d=raf.read();
System.out.println((char)d);//97-->a
int d1=raf.read();
System.out.println((char)d1);//98-->b
int d2=raf.read();
System.out.println(d2);//-1表示读到文件末尾
raf.close();
}


}


RandomAccessFile写文件

Test3.java 文件代码:

//RandomAccessFile只有写权限才可以创建文件,读权限是不行的
public class Test2RandomAccessFile {
public static void main(String[] args) throws IOException {
// //FileNotFoundException 系统找不到指定的文件
// RandomAccessFile raf=new RandomAccessFile("c.txt", "r");
RandomAccessFile raf=new RandomAccessFile("c.dat","rw");//dat表示音频视频格式
byte[] data="HelloWorld".getBytes();//String.getBytes()将字符串变成字节数组
raf.write(data);//写字节数组
raf.close();
}


}


RandomAccessFile读文件

下面是一个例子:

Test4.java 文件代码:

public class Test3RandomAccessFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
RandomAccessFile raf=new RandomAccessFile("raf.txt","rw");
byte[] buf=new byte[10];//buf={0,0,0,0,0,0,0,0,0,0}  HelloWorld
int len=raf.read(buf);//将读取到的字节数组放入buf中   buf={H,e,l,l,o,W,o,r,l,d}
System.out.println("实际读取到的字节量:"+len);
System.out.println(new String(buf));//将buf数组变成String
raf.close();
}


}


 

RandomAccessFile是基于指针的

//RandomAccessFile的读写操作是基于指针的
public class Test4RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "rw");
//指针默认从0开始(文件的第一个字符的前面)
System.out.println(raf.getFilePointer());//表示返回当前的指针位置 0
raf.read();//读一个字节指针移动一位
System.out.println(raf.getFilePointer());//1
//读取world,需要跳过ello这四个字节
raf.skipBytes(4);//跳过的字节量
System.out.println(raf.getFilePointer());//5
//读出World
byte[] buf=new byte[5];
int len=raf.read(buf);//buf={W,o,r,l,d}
System.out.println(new String(buf));//World

int d=raf.read();
System.out.println("-1表示读到文件末尾d="+d);
//将光标(游标)移动到文件初始位置(0)
raf.seek(0);
System.out.println(raf.getFilePointer());
raf.close();
}


}

RandomAccessFile文件复制

//RandomAccessFile文件的拷贝
public class Test5RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "r");//拷贝的源文件只需要r模式
RandomAccessFile rafCopy=new RandomAccessFile("rafCopy.txt", "rw");//目标文件
int data=0;//用来存储实际读取到的字节 int值
while((data=raf.read())!=-1){//不等于-1表示没有读到文件末尾,继续读
System.out.println("data="+data);
rafCopy.write(data);//将读取到的字节写入rafCopy.txt文件中
}
System.out.println("复制完成");
raf.close();
rafCopy.close();
}


}


IO流-FileOutputStream

/*
 * 都是以字节为单位进行读写操作的
 * OutputStream 字节输出流    是所有字节输出流的父类  只能写内容
 * InputStream  字节输入流  是所有字节输入流的父类    只能读内容
 */
public class Test7OutputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream os=new FileOutputStream("os.txt");//向上造型
os.write("helloworld".getBytes());//将字符串变成字节数组
os.close();//关闭流释放资源
}


}


FileInputStream的使用

//字节输入流InputStream
public class Test8InputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is=new FileInputStream(new File("os.txt"));
//读文件
int d=0;
//is.read()读取一个字节
while((d=is.read())!=-1){//-1表示读取到文件末尾
System.out.print((char)d);
}
is.close();
}


}


IO流的文件复制

//输入输出流的文件复制
public class Test9FileCopy {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("osCopy.txt");//目标文件
FileInputStream fis=new FileInputStream("os.txt");//源文件
int d=0;
while((d=fis.read())!=-1){//读取源文件  以字节为单位读取
fos.write(d);//将读取到的字节写到目标文件中
}
System.out.println("复制完成");
fos.close();
fis.close();
}


}

IO流通过字节数组赋值

//创建字节数组复制文件
public class Test15FileCopy {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("e:"+File.separator+"360.exe");//原始文件
FileOutputStream fos=new FileOutputStream("d:"+File.separator+"360.exe");//目标文件
byte[] data=new byte[1024*8];//1G=1024M  1M=1024K 1K=1024B
int len=0;//表示实际读取到的字节量
//len=10   data={我,的,滑,板,鞋,0,0,0,0,0,0,0,0,0,...}
while((len=fis.read(data))!=-1){//data={鞋,0,0}
fos.write(data,0,len);//截取实际长度,去空格
}
fis.close();
fos.close();
System.out.println("复制完成");
}


}

猜你喜欢

转载自blog.csdn.net/Anthonybuer/article/details/71136238