Java中IO的相关操作——File类和RandomAccessFile类

来源:互联网 发布:mac同时显示两个窗口 编辑:程序博客网 时间:2024/05/06 18:56

今天看了张孝祥老师的《JAVA高级视频IO输入与输出》。对IO的学习有了更进一步的认识。

一. File

a) File类是IO包中唯一代表磁盘文件本身信息的类,而不是文件中的内容。

b) File类定义了一些与平台无关的方法来操作文件。

c) Java中的目录被当作一种特殊的文件使用,list方法可以返回目录中的所有子目录和文件名。

d) 在Unix下的路径分隔符为“/”,在Dos下的路径分隔符为“\”,Java可以正确处理UnixDos的路径分隔符。

案例分析如下:

package FileTest;
import java.io.*;
import java.util.*;
public class FileTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  File f=new File("D://text.txt");
  if(f.exists()){
   System.out.println("已经存在!");
   //f.delete();
  }else{
   try {
    f.createNewFile();
    System.out.println("已经创建!");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("File name\t"+f.getName());
  System.out.println("File path\t"+f.getPath());
  System.out.println("File absolutePath\t"+f.getAbsolutePath());
  System.out.println("File parent\t"+f.getParent());
  System.out.println(f.exists()?"exists":"not exists");
  System.out.println(f.canRead()?"Read":"not Read");
  System.out.println(f.isDirectory()?"is Directory":"not Directory");
  System.out.println("File last modified\t"+new Date(f.lastModified()));
 }

}

 

二. RandomAccessFile

a) RandomAccessFile类提供了众多的文件访问方法。

b) RandomAccessFile类支持“随机访问”方式。

c) RandomAccessFile类在随机(相对顺序而言)读写等长记录格式的文件时有很大的优势。

d) RandomAccessFile类仅限于操作文件,不能访问其他的IO设备,如网络,内存映像等。

e) 两种构造方法:

i. new RandomAccessFile(f,”rw”);//读写方式

ii. new RandomAccessFile(f,”r”);//只读方式

f) RandomAccessFile案例分析

 

package RandomFileTest;

public class Employee {
 public String name=null;
 public int age=0;
 public static final int LEN=8;
 
 public Employee(String name,int age){     //当对象的属性值少于所定长度时要补充“\u0000,当大于长度时要截取。
  if(name.length()>LEN){  
   this.name=name.substring(0,8);
  }else{
   while(name.length()<LEN){        
    name+="\u0000";
   }
  }
  this.name=name;
  this.age=age;
 }
}

 

 

package RandomFileTest;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomFileTest {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  //实例化对象
  Employee e1=new Employee("张三",23);
  Employee e2=new Employee("lisi",22);
  Employee e3=new Employee("wangwu",25);
  //实例化RandomAccessFile类
  RandomAccessFile raw=new RandomAccessFile("employee.txt","rw" );
  //raw.write(e1.name.getBytes());  //按字节写到文件中
  raw.writeChars(e1.name);    //把String字符写到文件中
  raw.writeInt(e1.age);     //把整数以四个字节的形式全部写入文件
  raw.writeChars(e2.name);
  //raw.write(e2.name.getBytes());
  raw.writeInt(e2.age);
  raw.writeChars(e3.name);
  //raw.write(e3.name.getBytes());
  raw.writeInt(e3.age);
  raw.close();
  
  RandomAccessFile rar=new RandomAccessFile("employee.txt","r" );
//  int len=0;
//  byte[] names=new byte[Employee.LEN]; //实例化一个字节数组
//  len=rar.read(names);     //读取一个字节数组长度的字节到此数组中
//  String name=new String(names);   //将字节数组实例化成一个字符串
  
  String name ="";
  for (int i = 0; i < Employee.LEN; i++) {
   name+=rar.readChar();    //读取一个字符
  }
  int age=rar.readInt();     //读取一个Int型的数字 
  System.out.println(name+age);
  
  name ="";
  for (int i = 0; i < Employee.LEN; i++) {
   name+=rar.readChar();
  }
  age=rar.readInt();  
  System.out.println(name+age);
  
  name ="";
  for (int i = 0; i < Employee.LEN; i++) {
   name+=rar.readChar();
  }
  age=rar.readInt();  
  System.out.println(name+age);
  
 }

}

 

v. Raf.Seek0 //绝对定位:定位到文件的第n个字节处,不是向前向后移动多少字节,文件指示器的位置

vi. raf.skipBytes(12) //跳转n个字节,定位

vii. raf.read(buf) //读取buf字节数组 ,返回int类型表示读取的长度。

xiii. 输入中文的问题,在java中中文字符与英文字符都是占两个字符,而本地机器的编码是 英文字母占一个字符,汉字占两个字节)。对于这样的问题可以用String的构造方法指定在编码格式的情况下进行转化,如:new String(name.getBytes(),"gbk")进行转换。

xiv. 我们希望姓名部分是等长的。我们不要用字节比较,要用字符。

xvii. readChar 一次读取一个字符(Uncode编码(英汉均占2个字符))

原创粉丝点击