Java中的File类和IO包中的其他流

File类(Package java.io)

  用于操作文件的类。

  常用方法:1.构造函数:public File​(String pathname);//传入文件路径和文件名;

       2.public boolean isFile();//判断传入的路径是否是文件;

       3.public boolean createNewFile() throws IOException;//创建文件,如果文件存在则创建失败,和IO流不一样;

       4.public boolean isDirectory();//判断传入的路径是否是文件夹;

       5.public boolean mkdirs();//创建多级目录,如果目录存在则创建失败;

       6.public boolean delete();//删除失败返回false;

       7.public void deleteOnExit();//在程序退出时删除指定文件;

       8.public boolean exists();//判断文件或文件夹是否存在;

package second.study;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 将文件夹的绝对路径存放到指定文件中
 * 
 * @param args
 */

public class Test {

    public static void main(String[] args) throws IOException {
        File dir = new File("F:\\JAVA_study\\代码");
        List<File> list = new ArrayList<File>();
        if (!dir.exists()) {
            throw new RuntimeException("文件夹不存在");
        }
        file2List(dir, list);
        File desFile = new File(dir, "dirname.txt");
        list2File(list, desFile.toString());
    }

    /**
     * 将文件夹中的内容存放到制定集合中
     * 
     * @param dir
     * @param list
     */
    public static void file2List(File dir, List<File> list) {

        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                file2List(file, list);
            } else {
                list.add(file);
            }
        }
    }

    /**
     * 将列表中的文件存放到指定文件中
     * @param list
     * @param desString
     */
    public static void list2File(List<File> list, String desString) {
        BufferedWriter bufw = null;
        try {
            bufw = new BufferedWriter(new FileWriter(desString));
            for (File file : list) {
                bufw.write(file.getAbsolutePath());
                bufw.newLine();
                bufw.flush();
            }
        } catch (Exception ex) {
            throw new RuntimeException("目的地文件创建错误");
        } finally {
            try {
                if (bufw != null)
                    bufw.close();
            } catch (Exception ex) {
                throw new RuntimeException("目的地文件关闭错误");
            }
        }
    }
}

 对象流(ObjectInputStream,ObjectOutputStream)

  也就是对象的持久化

  ObjectInputStream

  主要方法:1.构造方法:public ObjectInputStream​(InputStream in) throws IOException;//将流与对象文件关联;

       2.public final Object readObject() throws IOException, ClassNotFoundException;//读文件中的对象;

  ObjectOutputStream

  主要方法:1.构造方法:public ObjectOutputStream​(OutputStream out) throws IOException;//将将流与对象文件关联;

        2.public final void writeObject​(Object obj) throws IOException;//将对象写入到文件中;

package second.study;

import java.io.Serializable;

public class Person implements Serializable{
    
//    目前理解:一般应该是不覆盖的,但是不覆盖eclipse会有黄叹号。
    private static final long serialVersionUID = 1L;
    private String name;
//    如果不希望该值别序列化,可以使用transient关键字。
    transient private int age;
//    现在可以序列化方法区内的数据了?
    static String country = "cn";
    public Person(String name, int age, String country) {
        super();
        this.name = name;
        this.age = age;
        Person.country = country;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", country=" + country + "]";
    }
    
}
package second.study;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Test {

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

        writeObj();
        readObj();
    }

    public static void writeObj() throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person("Zhangsan", 20, "kr"));
        oos.close();
    }

    public static void readObj() throws Exception {
        ObjectInputStream oin = new ObjectInputStream(new FileInputStream("obj.txt"));
        Person p = (Person)oin.readObject();
        oin.close();
        System.out.println(p);
    }
}

RandomAccessFile

  该类不是IO体系中的子类,而是直接继承Object类。但是它是IO包中的成员,因为它具备读写方法。其内部封装了一个数组,可以通过指针对数组的元素进行操作,可以通过getFilePointer()方法获取指针的位置,同时可以使用seek​()方法设置指针的位置

  主要方法:1.构造函数:public RandomAccessFile​(File file, String mode) throws FileNotFoundException;//只能操作文件,mode:"r","rw","rws","rwd"。

       2.构造函数:public RandomAccessFile​(String name, String mode) throws FileNotFoundException;

       3.public long getFilePointer() throws IOException;//获取指针的位置

       4.public void seek​(long pos) throws IOException;//设置指针的位置

       5.public int skipBytes​(int n) throws IOException;//跳过字节数,返回实际跳过的字节数

DataInputStream(java.io)

  用于操作基本数据类型。可以直接将基本数据类型写入文件中,而不必将基本数据类型转换成字节。

猜你喜欢

转载自www.cnblogs.com/star-491849224/p/12513711.html
今日推荐