java file和randoM_Java IO系统之File类和RandomAccessFile类学习笔记

File类

An abstractrepresentation of file and directory pathnames.

User interfacesand operating systems use system-dependentpathname strings to namefiles and directories. This class presents an abstract, system-independent viewof hierarchical pathnames. Anabstract pathname has two components:

An optional system-dependentprefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\\\" for a Microsoft Windows UNC pathname, and

A sequence of zero or more stringnames.

File类是IO包中唯一代表磁盘文件本身的对象,File类定义了一些与平台无关的方法操纵文件,通过调用File类提供的各种方法,我们能够创建,删除文件,重命名文件,判断文件的读写权限及是否存在,设置和查询文件的最近修改时间。

在Java中,目录也被当作File使用,只是多了一些目录特有的功能,比如可以用list方法列出目录中的文件名。在Unix下的路径分隔符为(/),在Dos下的路径分隔符为(\),Java可以正确处理Unix和Dos的路径分隔符,即使我们在Windows环境下使用(/)作为路径分隔符,Java仍然能够正确处理。

RandomAccessFile类

Instances of thisclass support both reading and writing to a random access file. A random accessfile behaves like a large array of bytes stored in the file system. There is akind of cursor, or index into the implied array, called the file pointer;input operations read bytes starting at the file pointer and advance the file pointerpast the bytes read.

If the randomaccess file is created in read/write mode, then output operations are alsoavailable; output operations write bytes starting at the file pointer andadvance the file pointer past the bytes written. Output operations that writepast the current end of the implied array cause the array to be extended. Thefile pointer can be read by the getFilePointer method and set by the seekmethod.

RandomAccessFile在等长记录格式文件的随机(相对顺序而言)读取时有很大的优势,但该类仅限于操作文件,不能访问其他的IO设备,如网络、内存映像等。

一个关于RandomAccessFile类的例子:

import java.io.*;

public class RandomFileTest

{

public static void main(String [] args) throws Exception

{

Employee e1 = new Employee("Zhangsan",23);

Employee e2 = new Employee("Lisi",24);

Employee e3 = new Employee("Wangwu",25);

RandomAccessFile ra = new RandomAccessFile("I://1.txt","rw");

ra.write(e1.name.getBytes());

ra.writeInt(e1.age);

ra.write(e2.name.getBytes());

ra.writeInt(e2.age);

ra.write(e3.name.getBytes());

ra.writeInt(e3.age);

ra.close();

RandomAccessFile raf = new RandomAccessFile("I://1.txt","r");

int len = 8;

raf.skipBytes(12);

System.out.println("第二个员工信息");

String str = "";

for(int i = 0;i

str = str+(char)raf.readByte();

System.out.println("name:"+str);

System.out.println("age:"+raf.readInt());

System.out.println("第一个员工的信息:");

raf.seek(0);

str = "";

for(int i = 0;i

str = str+(char)raf.readByte();

System.out.println("name:"+str);

System.out.println("age:"+raf.readInt());

System.out.println("第三个员工的信息:");

raf.skipBytes(12);

str = "";

for(int i = 0;i

str = str+(char)raf.readByte();

System.out.println("name:"+str.trim());

System.out.println("age:"+raf.readInt());

raf.close();

}

}

class Employee

{

String name;

int age;

final static int LEN = 8;

public Employee(String name,int age)

{

if(name.length()>LEN)

{

name = name.substring(0,8);

}

else

{

while(name.length()

name = name+"\u0000";

}

this.name = name;

this.age = age;

}

}

关于String.substring(intbeginIndex,int endIndex)方法的细节

该方法可以用于驱除一个字符串中的部分子字符串,但是要注意的一个细节是:子字符串中的第一个字符对应的是愿字符串中的脚标为beginIndex处的字符,但最后的字符对应的是原字符串中的脚标为endIndex-1处地字符,而不是endIndex处的字符。 ----------------------

android培训、

java培训、期待与您交流! ---------------------- 详细请查看:

http://edu.csdn.net/heima

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值