file.getParentFile.mkdirs()

来源:互联网 发布:qq刷圈圈软件 编辑:程序博客网 时间:2024/05/24 00:01
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class Test {
  public static void main(String[] args) {
    /**
     * mkdirs()创建多层目录,mkdir()创建单层目录
     * writeObject时才创建磁盘文件。
     * 若不创建文件,readObject出错。
     */

    String tablename = "hash1";
    File file = new File("data/1/" + tablename);
    boolean mk=file.getParentFile().mkdir();
    System.out.println("create:"+mk);//false
    mk=file.getParentFile().mkdirs();
    System.out.println("create:"+mk);//true
    System.out.println("file:" + file.getParentFile());

    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(new FileOutputStream(file));
      out.writeObject(new Integer(1));
      out.flush();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (out != null)
          out.close();
      } catch (IOException e1) {
      }
    }
    ObjectInputStream oin = null;
    try{
      oin = new ObjectInputStream(new FileInputStream(file));
      Integer i=(Integer) oin.readObject();
      System.out.println("i:"+i);
     }
     catch (Exception e)
     {
        e.printStackTrace();
     }
     finally{
        try{
           if (oin != null)
             oin.close();
        }
        catch (IOException e1){
        }
     }
  }
}
0 0
原创粉丝点击