Java I/O笔记

I/O输入与输出

1.    File

 

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

File类定义了一些与平台无关额方法来操纵文件,例如,创建、删除文件和重命名文件。

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

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

 

编程举例:判断某个文件是否存在,存在删除,不存在创建。

package com.file;

 

import java.io.File;

import java.io.IOException;

import java.util.Date;

 

public class FileTest {

public static void main(String[] args) {

           File file = new File("1.txt");

           if(file.exists()) {

                    System.out.println(file.getName() + "is exist");

           } else {

                    try {

                             file.createNewFile();

                    } catch (IOException e) {

                             e.printStackTrace();

                    }

           }

           System.out.println("file name:" + file.getName());

           System.out.println("file absolutePath:" + file.getAbsolutePath());

           try {

                    System.out.println("file canonicalPath:" + file.getCanonicalPath());

                    System.out.println("file cnonicalFile:" + file.getCanonicalFile());

           } catch (IOException e) {

                    e.printStackTrace();

           }

           //获取文件的各种属性

System.out.println("file freeSpace:" + (file.getFreeSpace()/1024/1024/1024 + file.getFreeSpace()/1024/1024%1024/1000.0) + "GB");

           System.out.println("file parent:" + file.getParent());

           System.out.println("file path3:" + file.getPath());

System.out.println("file totalSpace:" + (file.getTotalSpace()/1024/1024/1024 + file.getTotalSpace()/1024/1024%1024/1000.0) + "GB");

System.out.println("file usableSpace:" + (file.getUsableSpace()/1024/1024/1024 + file.getUsableSpace()/1024/1024%1024/1000.0) + "GB");

           System.out.println("file parentFile:" + file.getParentFile());

           System.out.println("file is exit? " + (file.exists() ? "exits" : "not exits"));

           System.out.println("file is a file? " + (file.isFile() ? "file" : "not file"));

           System.out.println("file can read? " + (file.canRead() ? "can read" : "can not read"));

           System.out.println("File last modified time: " + new Date(file.lastModified()));

}

}

2.    RandomAccessFile

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

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

应用:多线程下载。每个线程下载的文件大小不同,且同时读取,从文件的不同位置开始读。

c)         RandomAccessFile类在随机(相对顺序而言)读写等长记录格式(文件存储的信息是一条一条的记录,例如:日志文件、员工信息单等)的文件时有很大的优势。

(读取想要读取的内容)

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

e)         两种构造方法:

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

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

f)          编程实例:向文件中写入三名员工的信息,每个员工含有姓名和年龄两个字段,然后按照第二名、第一名、第三名的顺序独处员工信息。

                         i.              保证姓名字段长度相同。

                   Employee

                  package com.file;

 

public class Employee {

   

    public final static int LEN = 8;       //姓名的最大字节数为8

    private int age;

    private String name;

    public Employee(String name, int age) {

       while(name.length() < LEN) {

           name += " ";

       }

       if(name.length() > 8) {

           name = name.substring(0, LEN);

       }

       this.name = name;

       this.age = age;

    }

    public int getAge() {

       return age;

    }

    public String getName() {

       return name;

    }

    public void setAge(int age) {

       this.age = age;

    }

   

    public void setName(String name) {

       this.name = name;

    }

   

}

       RandomAccessFile测试类

       package com.file;

 

import java.io.RandomAccessFile;

 

public class RandomAccessFileTest {

   

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

       Employee e1 = new Employee("长江", 20);

       Employee e2 = new Employee("欧阳圆成", 258);

       Employee e3 = new Employee("feng", 55);

      

       RandomAccessFile r1 = new RandomAccessFile("employee.txt", "rw");

//     r1.write(e1.getName().getBytes()); //不能正确的写入中文字符

//     r1.write(e1.getAge());   //只写入整数的一个字节

       r1.writeChars(e1.getName());

       r1.writeInt(e1.getAge());

       r1.writeChars(e2.getName());

       r1.writeInt(e2.getAge());

       r1.writeChars(e3.getName());

       r1.writeInt(e3.getAge());

      

       r1.close();

      

//     int len = 0;

//     byte[] b = new byte[8];

       String strName = "";

       int stepLen = Employee.LEN * 2 + 4;

       RandomAccessFile r2 = new RandomAccessFile("employee.txt", "r");

       r2.skipBytes(stepLen);

       for(int i=0; i<Employee.LEN; i++) {

           strName += r2.readChar();

       }

       System.out.println(strName.trim() + ": " + r2.readInt());

      

       //调回初始位置

       r2.seek(0);

       strName = "";

       for(int i=0; i<Employee.LEN; i++) {

           strName += r2.readChar();

       }

       System.out.println(strName.trim() + ": " + r2.readInt());

      

       //跳到第三个位置

       r2.seek(stepLen * 2);

       strName = "";

       for(int i=0; i<Employee.LEN; i++) {

           strName += r2.readChar();

       }

       System.out.println(strName.trim() + ": " + r2.readInt());

      

       r2.close();

      

    }

}

3.    节点流

a)         流是字节序列的抽象概念。

b)         文件是数据的静态存储形式,而流是指数据传输时的形态(连续的数据的集合)。

c)         Java的流分为两大类:节点流类和过滤流类(处理流)。

4.    InputStream

a)         程序可以从中连续读取字节的对象叫输入流,在Java中,用InputStream类来描述所有输入流的抽象概念。(此抽象类是表示字节输入流的所有类的超类)。

b)         有了垃圾回收器,为什么还要调用close方法?

                         i.              流是一种资源,垃圾回收器只能管理类的实例对象,不能管理系统资源。

5.    OutputStream

a)         程序可以向其中连续写入字节的对象叫输出流,在Java中,用OutputStream类来描述所有输出流的抽象概念。

b)         调用flush()方法清空缓冲区。

 

 

内存缓冲区中数据要及时存到硬盘。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值