JAVA编码(10)——文件夹、文件创建删除和读取文件内容

/**
     * 创建文件目录 (文件夹)
     * @return
     */
    protected boolean createDir() {
//        File file = new File("F:/test");
        File file = new File("F:/test/test01");
        System.out.println("file.getAbsolutePath():"+file.getAbsolutePath());
        System.out.println("file.getAbsoluteFile():"+file.getAbsoluteFile());
        if (file.isDirectory()) {
            return file.isDirectory();
        }
        //创建单层文件夹
//        return file.mkdir();
        //创建多层文件夹
        return file.mkdirs();
    }
    
    /**
     * 删除文件目录  (文件夹)
     * @return
     */
    protected boolean deleteDir() {
        File file = new File("F:/test");
        if (file.exists() && file.isDirectory()) {
            return file.delete();
        }
        return false;
    }
    
    /**
     * 创建文件
     * @return
     */
    protected boolean createFile() {
        try {
            File file = new File("F:/test.txt");
            if (!file.exists()) {
                return file.createNewFile();
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 删除文件
     * @return
     */
    protected boolean deleteFile() {
        File file = new File("F:/test.txt");
        if (file.exists() && file.isFile()) {
            return file.delete();
        }
        return false;
    }
    
    /**
     * 在同一个文件夹下创建多个文件夹
     * @return
     */
    protected boolean createDirs() {
        File file = new File("F:/test/test01/test02");
        if (!file.exists()) {
            return file.mkdirs();
        }
        File file2 = new File("F:/test/test01/test03");
        if (!file2.exists()) {
            return file2.mkdir();
        }
        return false;
    }
    
    /**
     * 文件文件夹  及  文件
     * @return
     */
    protected boolean createDirFile() {
        try {
            String filePath = "F:/test";
            String fileName = "test.txt";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            filePath = filePath + "/" + fileName;
            File file2 = new File(filePath);
            if (!file2.exists()) {
                file2.createNewFile();
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 从一个文件写入另一个文件中
     * a.txt必须存在
     * b.txt没有会自动创建
     * @return
     */
    protected boolean writeFileToFile() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("F:/a.txt");
            fos = new FileOutputStream("F:/b.txt");
            byte[] b = new byte[1024*1024];
            int length = 0;
            while((length = fis.read(b)) > 0){
                fos.write(b, 0, length);
            }
            fos.flush();
            close(fos,fis);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    
    /**
     * 将文件中的内容读取成字符串
     * @return
     */
    @SuppressWarnings("resource")
    protected boolean readFileToString() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("F:/a.txt");
            byte[] b = new byte[1024*1024];
            int temp = -1;
            while((temp = fis.read(b)) != -1){
                String str = new String(b, 0, temp);
                System.out.println(str);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
    
    /**
     * 1.将字节数组以字符的形式输出
     * @return
     */
    protected boolean readFileToByteArray() {
        ByteArrayInputStream bis = null;
        ByteArrayOutputStream bos = null;
        try {
            //将一个字符串转化成一个字节数组
            byte[] b = "abcdefg".getBytes();
            bis = new ByteArrayInputStream(b);
            int temp = -1;
            while ((temp = bis.read()) != -1) {
                System.out.print((char)temp);
            }
            System.out.println("========================");
            bos = new ByteArrayOutputStream();
            bos.write("abcdefg".getBytes());
            byte[] b1 = bos.toByteArray();
            System.out.println(new String(b1));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * 关闭流对象,释放资源
     * @param obj
     */
    public void close(Closeable...obj){
        for (int i = 0; i < obj.length; i++) {
            try {
                if (obj[i]!=null) obj[i].close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 关闭流对象,释放资源
     * @param t
     */
    public <T extends Closeable> void close1(T...t){
        for (int i = 0; i < t.length; i++) {
            try {
                if (t[i]!=null) t[i].close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }

 

posted @ 2015-04-05 19:04  xu_shuyi  阅读(145)  评论(0)    收藏  举报