Java_RandomAccessFile和IO流
来源:互联网 发布:c语言中最大公约数 编辑:程序博客网 时间:2024/06/07 11:19
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();//关闭流释放资源
}
}
* 都是以字节为单位进行读写操作的
* 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();
}
}
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();
}
}
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("复制完成");
}
}
0 0
- Java_RandomAccessFile和IO流
- File和IO流
- IO流和异常
- Map 和 IO流
- 输入和输出--IO流
- io流,关键字和异常
- Java XML和IO流
- File类和IO流
- System和IO流笔记
- File类 和 io流
- File类和IO流
- 类型转换和io流
- Io流和操作总结
- IO流加强(总结)--IO流总结和练习
- 字节输入输出流:java.io.FileInputStream和java.io.FileOutputStream
- 字符输入输出流:java.io.FileReader和java.io.FileWriter
- 字符串输入输出流:java.io.BufferedReader和java.io.BufferedWriter
- Java网络编程(三) java 流io和块io
- 数据流中的中位数
- EA&UML日拱一卒--序列图(Sequence Diagram)::同步/异步
- hdu5782
- 滑动窗口的最大值
- ViewPager使用版本一
- Java_RandomAccessFile和IO流
- Easy-14
- 矩阵中的路径
- 1.docker的安装
- 机器人的运动范围
- POJ2245Lotto
- 腾讯云服务器的配置与部署(二):MySQL的配置安装、云服务器与云数据库的连接
- C++11 lambda 表达式解析
- Mysql like 用法