---
title: "java中文件操作大全"
author: "jdlsfl"
date: "2008-05-25T22:19:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2008-05-25T22:19:00Z"
id: 209387
url: "https://jdlsfl.iteye.com/blog/209387"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // java中文件操作大全  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 本文为本人在工作学习中的总结，每个方法都经过测试，记在博客上一方面是为了自己日后工作方便，另一方面是为了给大家提供方便，节约时间。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 一.获得控制台用户输入的信息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 获得控制台用户输入的信息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public String getInputMessage() throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("请输入您的命令∶");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte buffer\[] = new byte\[1024];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int count = System.in.read(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        char\[] ch = new char\[count - 2];// 最后两位为结束符，删去不要  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        for (int i = 0; i &lt; count - 2; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            ch\[i] = (char) buffer\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String str = new String(ch);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return str;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 可以返回用户输入的信息，不足之处在于不支持中文输入，有待进一步改进。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 二.复制文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    //  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 1.以文件流的方式复制文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 以文件流的方式复制文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param src  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            文件源目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param dest  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            文件目的目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void copyFile(String src, String dest) throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileInputStream in = new FileInputStream(src);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File file = new File(dest);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (!file.exists())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            file.createNewFile();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileOutputStream out = new FileOutputStream(file);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int c;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte buffer\[] = new byte\[1024];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        while ((c = in.read(buffer)) != -1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            for (int i = 0; i &lt; c; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                out.write(buffer\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        in.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        out.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 该方法经过测试，支持中文处理，并且可以复制多种类型，比如txt，xml，jpg，doc等多种格式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    //  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 三.写文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 1.利用PrintStream写文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\** \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 文件输出示例  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void PrintStreamDemo()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            FileOutputStream out = new FileOutputStream("D:/test.txt");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            PrintStream p = new PrintStream(out);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            for (int i = 0; i &lt; 10; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p.println("This is " + i + " line");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        catch (FileNotFoundException e)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 2.利用StringBuffer写文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void StringBufferDemo() throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File file = new File("/root/sms.log");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (!file.exists())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            file.createNewFile();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileOutputStream out = new FileOutputStream(file, true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        for (int i = 0; i &lt; 10000; i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            StringBuffer sb = new StringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            sb.append("这是第" + i + "行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            out.write(sb.toString().getBytes("utf-8"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        out.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 该方法可以设定使用何种编码，有效解决中文问题。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    //  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 四.文件重命名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\** \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 文件重命名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param path  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            文件目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param oldname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            原来的文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param newname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            新文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void renameFile(String path, String oldname, String newname)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (!oldname.equals(newname))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{// 新的文件名和以前文件名不同时,才有必要进行重命名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File oldfile = new File(path + "/" + oldname);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File newfile = new File(path + "/" + newname);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if (newfile.exists())// 若在该目录下已经有一个文件和新文件名相同，则不允许重命名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                System.out.println(newname + "已经存在！");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                oldfile.renameTo(newfile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 五.转移文件目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    //  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    //  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    // 转移文件目录不等同于复制文件，复制文件是复制后两个目录都存在该文件，而转移文件目录则是转移后，只有新目录中存在该文件。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\** \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 转移文件目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param filename  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param oldpath  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            旧目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param newpath  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            新目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param cover  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *            若新目录下存在和转移文件具有相同文件名的文件时，是否覆盖新目录下文件，cover=true将会覆盖原文件，否则不操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void changeDirectory(String filename, String oldpath, String newpath, boolean cover)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (!oldpath.equals(newpath))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File oldfile = new File(oldpath + "/" + filename);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File newfile = new File(newpath + "/" + filename);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if (newfile.exists())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)