首页 > 学院 > 开发设计 > 正文

01 IO流 RandomAccessFile File

2019-11-06 07:52:00
字体:
来源:转载
供稿:网友

1、File类表示目录信息 listFiles()方法用于返回一个抽象路径名数组,这些路径名表示抽象路径名的目录中的子项(文件或目录)。 File[] listFiles() 返回值是抽象路径名数组。 例:获取当前目录下的内容:

public class TestListFiles { public static void main(String[] args) { File dir =new File("."); File[] subs =dir.listFiles(); for(File sub:subs ){ System.out.PRintln(sub); } }}

运行结果:

./.classpath./.project./.settings./bin./src“.” 表示的当前目录,本例当前目录,就是当前工程下。

2、FileFilter接口 用于抽象路径名的过滤器。 此接口的实例可传递给File类的listFiles(FileFilter)方法。用于返回满足该过滤器要求的子项。 例:获取当前目录下的后缀为ings的文件(扩展为’.txt’的文件);最后遍历listFiles方法返回的数组查看目录下的内容。

package com.wang;import java.io.File;import java.io.FileFilter;public class TestListFiles { public static void main(String[] args) { File dir =new File("."); File[] subs =dir.listFiles(new FileFilter(){ public boolean accept(File file){ return file.getName().endsWith("ings"); } }); for(File sub:subs ){ System.out.println(sub); } }}

运行结果:

./.settings

RandomaccessFile类。

package day01;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;/** * 批量写出字节 * * @author Administrator * */public class RandomAccessFileDemo1 { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile( "test.txt","rw" ); String str = "我爱达内苍老师!"; /* * byte[] getBytes() * 字符串的该方法用来将当前字符串按照 * 当前系统默认的字符集转换为对应的字节 * windows默认:gbk * unix默认:utf-8 */// byte[] data = str.getBytes(); /* * 重载的getBytes() * byte[] getBytes(String str) * 将当前字符串按照给定的字符集转换为 * 对应的字节 * 字符集名称不区分大小写,但通常使用大写 * 常用的: * gbk:国标编码,英文1字节,中文2字节 * utf-8:万国码,英文1字节,中文3字节 * iso8859-1:欧洲编码,不支持中文。 */ byte[] data = str.getBytes("UTF-8"); System.out.println( "字节数:"+data.length ); /* * void write(byte[] d) * 将给定的字节数组中的所有字节全部写出 */ raf.write(data); raf.close(); }}

运行结果:

字节数:22生成了test.txt文件

例2:

package day01;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;/** * 批量读取字节 * @author Administrator * */public class RandomAccessFileDemo2 { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile( "test.txt","r" ); /* * int read(byte[] d) * 一次性尝试读取给定的字节数组的length * 个字节,并存入到给定的数组中。返回值 * 为实际读取到的字节量。若返回值为-1, * 表示读取到文件末尾了。 */ byte[] data = new byte[100]; int len = raf.read(data); System.out.println("实际读取到了:"+len); /* * 字符串提供了构造方法允许将给定的字节数组 * 按照给定的字符集转换为对应的字符串 * * String(byte[] d,String charset) * 将给定的字节数组中的所有字节按照给定的 * 字符集转换为字符串。 * * String(byte[] d, * int offset, * int len, * String charset) * 将给定的字节数组中从下标为offset处的 * 字节开始,连续len个字节,按照给定的字 * 符集转换为对应的字符串。 */ String str = new String(data,0,len,"UTF-8"); System.out.println(str); raf.close(); }}

运行结果:

实际读取到了:22我爱达内苍老师!

例3:

package day01;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;/** * RandomAccessFile是基于指针进行读写操作的 * @author Administrator * */public class RandomAccessFileDemo3 { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile( "demo.dat","rw" ); /* * RAF总是基于指针当前位置进行读写操作的。无论是读还是写,指针都会自动向后移动。刚创建好的RAF,指针是指向文件的第一个字节的。指针的位置用下标表示,0表示文件的第一个字节的位置。 * long getFilePointer()获取当前指针的位置 */ long position = raf.getFilePointer(); System.out.println("pos:"+position); String str = "你好"; byte[] data = str.getBytes(); raf.write(data); position = raf.getFilePointer(); System.out.println("pos:"+position); raf.close(); }}

运行结果:

pos:0pos:6

例4:

package day01;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;/** * RandomAccessFile提供了很多让变读写数据类型 * 的方法 * @author Administrator * */public class RandomAccessFileDemo4 { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile( "raf.dat","rw" ); //0 System.out.println( "pos:"+raf.getFilePointer()); //写出一个int值 /* * vvvvvvvv * 01111111 11111111 11111111 11111111 * max >>> 24 */ int max = Integer.MAX_VALUE;// raf.write(max>>>24);// raf.write(max>>>16);// raf.write(max>>>8);// raf.write(max); /* void writeInt(int d)将给定的int值写出。该方法会连续写4个字节。 */ raf.writeInt(max); System.out.println( "pos:"+raf.getFilePointer()); /* 写出一个long值 */ raf.writeLong(123L); System.out.println( "pos:"+raf.getFilePointer()); /* * 写一个double值 */ raf.writeDouble(123.123); System.out.println( "pos:"+raf.getFilePointer()); /* * 先将指针移动到文件最开始 * void seek(long pos)将指针移动到指定位置 */ raf.seek(0); System.out.println( "pos:"+raf.getFilePointer()); /* * 先读int值 */ int i = raf.readInt(); System.out.println(i); System.out.println( "pos:"+raf.getFilePointer()); long l = raf.readLong(); System.out.println(l); double d = raf.readDouble(); System.out.println(d); raf.close(); }}

运行结果:

pos:0pos:4pos:12pos:20pos:02147483647pos:4123123.123

例5:首先将草原歌曲 - 我要去西藏.mp3复制到工作路径下: 使用RandomAccessFile类copy首mp3.

package day01;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;/** * 若想提高读写效率,必须减少读写次数。可以通过提高读写的数据量,来减少读写的次数,从而提高读写效率。 * @author Administrator * */public class CopyDemo2 { public static void main(String[] args) throws IOException { RandomAccessFile src = new RandomAccessFile( "草原歌曲 - 我要去西藏.mp3","r" ); RandomAccessFile des = new RandomAccessFile( "草原歌曲 - 我要去西藏d.mp3","rw" ); //创建10k缓冲区 byte[] buf = new byte[1024*10]; //byte[] buf =new byte[10240*100]; //保存每次实际读取到的字节量 int len = -1; long start = System.currentTimeMillis(); while((len = src.read(buf))!=-1 ){ /* * write(byte[] d,int offset,int len) 将给定的字节数组中从下标为offset处开始连续len个字节写出。 */ des.write(buf,0,len); } long end = System.currentTimeMillis(); System.out.println( "复制完毕!耗时:"+(end-start)+"毫秒"); src.close(); des.close(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表