JAVA 文件操作

源文地址

平常经常使用JAVA对文件进行读写等操作,这里汇总一下常用的文件操作。

1、创建文件

    public static boolean createFile(String filePath){  
        boolean result = false;  
        File file = new File(filePath);  
        if(!file.exists()){  
            try {  
                result = file.createNewFile();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

        return result;  
    }  

2、创建文件夹

    public static boolean createDirectory(String directory){  
        boolean result = false;  
        File file = new File(directory);  
        if(!file.exists()){  
            result = file.mkdirs();  
        }  

        return result;  
    }  

3、删除文件


    public static boolean deleteFile(String filePath){  
        boolean result = false;  
        File file = new File(filePath);  
        if(file.exists() && file.isFile()){  
            result = file.delete();  
        }  

        return result;  
    }  

4、删除文件夹

递归删除文件夹下面的子文件和文件夹


    public static void deleteDirectory(String filePath){  
        File file = new File(filePath);  
        if(!file.exists()){  
            return;  
        }  

        if(file.isFile()){  
            file.delete();  
        }else if(file.isDirectory()){  
            File[] files = file.listFiles();  
            for (File myfile : files) {  
                deleteDirectory(filePath + "/" + myfile.getName());  
            }  

            file.delete();  
        }  
    }  

5、读文件

(1)以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件

    public static String readFileByBytes(String filePath){  
        File file = new File(filePath);  
        if(!file.exists() || !file.isFile()){  
            return null;  
        }  

        StringBuffer content = new StringBuffer();  

        try {  
            byte[] temp = new byte[1024];  
            FileInputStream fileInputStream = new FileInputStream(file);  
            while(fileInputStream.read(temp) != -1){  
                content.append(new String(temp));  
                temp = new byte[1024];  
            }  

            fileInputStream.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return content.toString();  
    }  
(2)以字符为单位读取文件,常用于读文本,数字等类型的文件,支持读取中文

    public static String readFileByChars(String filePath){  
        File file = new File(filePath);  
        if(!file.exists() || !file.isFile()){  
            return null;  
        }  

        StringBuffer content = new StringBuffer();  
        try {  
            char[] temp = new char[1024];  
            FileInputStream fileInputStream = new FileInputStream(file);  
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");  
            while(inputStreamReader.read(temp) != -1){  
                content.append(new String(temp));  
                temp = new char[1024];  
            }  

            fileInputStream.close();  
            inputStreamReader.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return content.toString();  
    }  
(3)以行为单位读取文件,常用于读面向行的格式化文件

    public static List<String> readFileByLines(String filePath){  
        File file = new File(filePath);  
        if(!file.exists() || !file.isFile()){  
            return null;  
        }  

        List<String> content = new ArrayList<String>();  
        try {  
            FileInputStream fileInputStream = new FileInputStream(file);  
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");  
            BufferedReader reader = new BufferedReader(inputStreamReader);  
            String lineContent = "";  
            while ((lineContent = reader.readLine()) != null) {  
                content.add(lineContent);  
                System.out.println(lineContent);  
            }  

            fileInputStream.close();  
            inputStreamReader.close();  
            reader.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return content;  
    }  

6、写文件

字符串写入文件的几个类中,FileWriter效率最高,BufferedOutputStream次之,FileOutputStream最差。

(1)通过FileOutputStream写入文件
    public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{  
        File file = new File(filePath);  
        synchronized (file) {  
            FileOutputStream fos = new FileOutputStream(filePath);  
            fos.write(content.getBytes("GBK"));  
            fos.close();  
        }  
    }  
(2)通过BufferedOutputStream写入文件

    public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{  
        File file = new File(filePath);  
        synchronized (file) {  
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));  
            fos.write(content.getBytes("GBK"));  
            fos.flush();  
            fos.close();  
        }  
    }  
(3)通过FileWriter将字符串写入文件

    public static void writeFileByFileWriter(String filePath, String content) throws IOException{  
            File file = new File(filePath);  
            synchronized (file) {  
                FileWriter fw = new FileWriter(filePath);  
                fw.write(content);  
                fw.close();  
            }  
        }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值