文件的操作java_java文件操作总结

RandomAccessFile 相关API的使用

pointer=0; 文件指针

写方法 raf.write(int) -->只写一个字节 后8位,同时指针指向下一个位置,准备再次写入

读方法 int b = raf.read()-->读一个字节

读写文件完成后一定要关闭

public static void testrandomaccessfile() throws IOException {

File demo=new File("demo");

if(!demo.exists())

demo.mkdir();

File file=new File(demo,"raf.dat");

if(!file.exists())

file.createNewFile();

RandomAccessFile raf=new RandomAccessFile(file,"rw");

//指针位置

System.out.println(raf.getFilePointer());

raf.write('A');//只写了一个字节

System.out.println(raf.getFilePointer());

raf.write('B');

int i=0x7fffffff;

//用write方法 每次只能写一个字节,如果要把i写进去就得写4次

raf.write(i>>>24);//高8位

raf.write(i>>>16);

raf.write(i>>>8);

raf.write(i);

System.out.println(raf.getFilePointer());

//可以直接写一个int

raf.writeInt(i);

String s ="中";

byte[] gbk=s.getBytes("gbk");

raf.write(gbk);//写入字节数组

System.out.println(raf.length());

//读文件必须把指针移到头部

raf.seek(0);

//一次性读取。把文件中内容读取到字节数组中

byte[] buf=new byte[(int)raf.length()];

raf.read(buf);

System.out.println(Arrays.toString(buf));

for(byte b:buf){

System.out.println(Integer.toHexString(b&0xff)+" ");

}

raf.close();

}

writeInt()方法源码,每次写入一位

c499033bffca?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

IO流(输入流,输出流)

字节流,字符流

1.字节流

1)InputStream、OutputStream

InputStream抽象了应用程序读取数据的方式

OutputStream抽象了应用程序写出数据的方式

2)EOF=End 读到-1就读到结尾

3)输入流基本方法

int b=in.read(); 读取一个字节无符号填充到int的低八位。-1是EOF

in.read(byte[] buf)读取数据填充到字节数组buf

in.read(byte[] buf,int start,int size)读取数据填充到字节数组buf,从buf的start位置开始,存放size长度的数据

4)输出流基本方法

out.write(int b)写出一个byte到流,b的低8位

out.write(byte[] buf)将buf字节数组都写入到流

out.write(byte[] buf,int start,int size)

5)FileInputStream--->具体实现了在文件上读取数据

6)FileOutputStream 实现了向文件中写出byte数据的方法

7)DataOutputStream/DataInputStream

对流功能的扩展,可以更加方便的读取Int,long,字符等类型数据

DataOutputStream

writeInt()/writeDouble()/writeUTF()

DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));

8)BufferedInputStream&BufferedOutputStream

这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO的性能

从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:

FileOutputStream--->write方法党羽与一滴一滴把水转移过去

DataOutputStream--->writexxx()方法会方便一些,相当于一瓢一瓢转移

BufferedOutputStream--->write方法更方便,相当于一瓢一瓢水先放入桶中,再从桶中导入到另一个缸中

使用in.read()方法

/*

读取指定文件内容,按照16进制输出到控制台

并且每输出10个byte换行

*/

public static void printHex(String filename) throws IOException {

//把文件作为字节流进行读操作

FileInputStream in=new FileInputStream(filename);

int b;

int i=1;

while((b=in.read())!=-1){

if(b<=0xf) {

//单位数前面补0

System.out.print("0");

}

System.out.print(Integer.toHexString(b)+" ");//将整形b转换为16进制的字符串

if(i++%10==0)

System.out.println();

}

in.close();

}

使用in.read(byte[] buf , int start , int size)

将数据读取到字节数组中,返回数据大小,循环输出字节数组

public static void printHexByByteArray(String filename)throws IOException{

FileInputStream in=new FileInputStream(filename);

byte[] buf=new byte[20*1024];

//从in中批量读取字节,放入到buf这个字节数组中,从第0个位置开始放,

//最多放buf.length个 返回的是读到的字节的个数

/*

int bytes=in.read(buf,0, buf.length);//一次性读完,说明字节数组足够大

int j=1;

for(int i=0;i

if((buf[i]&0xff)>0&&(buf[i]&0xff)<=0xf){

System.out.print("0");

}

System.out.print(Integer.toHexString(buf[i])+" ");

if(j++%10==0)

System.out.println();

}*/

//循环利用buf数组,防止开辟数组空间不够用情况

int bytes=0;

int j=1;

while((bytes=in.read(buf,0,buf.length))!=-1){

for(int i=0;i

//为了避免数据类型转换错误,将高24位清零

System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");

if(j++%10==0)

System.out.println();

}

}

}

文件的复制

//如果文件不存在直接创建,如果存在,删除后创建。 如果append=true,不存在的时候则追加

FileOutputStream out=new FileOutputStream("demo/out.dat");

public static void copyFile(File srcFile,File destFile)throws IOException{

if(!srcFile.exists())

throw new IllegalArgumentException("文件"+srcFile+"不存在");

if(!srcFile.isFile())

throw new IllegalArgumentException(srcFile+"不是文件");

FileInputStream in=new FileInputStream(srcFile);

FileOutputStream out=new FileOutputStream(destFile);

byte[] buf=new byte[8*1024];

int b;

while((b=in.read(buf,0,buf.length))!=-1){

out.write(buf,0,b);

out.flush();//最好加上

}

in.close();

out.close();

}

//利用带缓冲的字节流

public static void copytFileByBuffer(File srcFile,File destFile)throws IOException{

if(!srcFile.exists())

throw new IllegalArgumentException("文件"+srcFile+"不存在");

if(!srcFile.isFile())

throw new IllegalArgumentException(srcFile+"不是文件");

BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

int c;

while((c=bis.read())!=-1){

bos.write(c);

bos.flush();//刷新缓冲区

}

bis.close();

bos.close();

}

2.字符流

1)编码问题

2)认识文本和文本文件

java的文本 (char)是16位无符号整数,是字符的unicode编码(双字节编码)

文件是byte byte byte...的数据序列

文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果

3)字符流 (reader writer)---> 操作的是文本文件

字符的处理 一次处理一个字符

字符的底层仍然是基本的字节序列

字符流的基本实现

InputStreamReader 完成byte流解析为char流,按照编码解析

OutputStreamWriter 提供char流到byte流,按照编码处理

//注意编码问题

public class IsrAndOswDemo {

public static void main(String[] args) throws IOException {

FileInputStream in=new FileInputStream("G:\\11.txt");

InputStreamReader isr=new InputStreamReader(in,"utf-8");//默认项目的编码

FileOutputStream out=new FileOutputStream("G:\\12.txt");

OutputStreamWriter osw=new OutputStreamWriter(out,"utf-8");

// int c;

// while((c=isr.read())!=-1){

// System.out.print((char)c);

// }

char[] buffer=new char[8*1024];

int c;

while((c=isr.read(buffer,0,buffer.length))!=-1){

String s=new String(buffer,0,c);

System.out.println(s);

osw.write(buffer,0,c);

osw.flush();

}

isr.close();

osw.close();

}

}

FileReader/FileWriter

public class FrAndFwDemo {

public static void main(String[] args) throws IOException {

FileReader fr=new FileReader("g:\\11.txt");

FileWriter fw=new FileWriter("g:\\13.txt");

char[] buffer=new char[2056];

int c;

while((c=fr.read(buffer,0,buffer.length))!=-1){

fw.write(buffer,0,c);

fw.flush();

}

fr.close();

fw.close();

}

}

字符流的过滤器

BufferedReader ---> readLine 一次读一行

BufferedWriter/PrintWriter --->写一行

public class BrAndBwOrPwDemo {

public static void main(String[] args) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("g:\\11.txt")));

// BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("g:\\123.txt")));

PrintWriter pw=new PrintWriter("g:\\1234.txt");

String line;

while((line=br.readLine())!=null){

System.out.println(line);

pw.println(line);

pw.flush();

// bw.write(line);

// //单独写出换行操作

// bw.newLine();//换行操作

// bw.flush();

}

pw.close();

br.close();

// bw.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值