⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filecopy.java

📁 java对文件进行操作的例子
💻 JAVA
字号:
/** *该程序能独立实现复制文件, *并且,定义了一个可供其它程序使用的文件复制方法copy() **/import java.io.*;public class FileCopy {    /** 程序的main()方法,调用copy()方法. */    public static void main(String[] args) {        if (args.length != 2)    //检查参数            System.err.println("使用方法:java FileCopy <源文件名> <目的文件名>");        else {            //调用copy()方法,实现复制文件,并显示错误信息。            try { copy(args[0], args[1]); }            catch (IOException e) { System.err.println(e.getMessage()); }        }    }     /**     * 这个静态方法正真实现文件复制,     * 在复制文件之前,进行一些测试,以确定一切正常。     */    public static void copy(String from_name, String to_name)	throws IOException    {	//首先定义两个文件,它们都是File的实例        File from_file = new File(from_name);          File to_file = new File(to_name);         //首先确定源文件存在,并且是可读的文件        if (!from_file.exists())            abort("没有这样的源文件: " + from_name);        if (!from_file.isFile())            abort("不能复制目录: " + from_name);        if (!from_file.canRead())            abort("源文件不是可读文件: " + from_name);        //如果目标是一个目录,则将源文件名作为目标文件名        if (to_file.isDirectory())            to_file = new File(to_file, from_file.getName());                //如果目标文件存在,首先要确定它是可写的文件,并且在写入前询问用户。        //如果目标文件不存在,那么要确定该目录存在并且是可写的。        if (to_file.exists()) {            if (!to_file.canWrite())                abort("目标文件不是可写文件:" + to_name);                        //询问用户是否覆盖目标文件            System.out.print("是否覆盖现在的文件 " + to_file.getName() +			     "? (Y/N): ");            System.out.flush();                       //得到用户的反馈信息            BufferedReader in=		new BufferedReader(new InputStreamReader(System.in));            String response = in.readLine();            //检测用户反馈的信息,如果得到的答案是否定的,那么取消复制操作。            if (!response.equals("Y") && !response.equals("y"))                abort("目标文件不能被覆盖。");        }        else {               //如果文件不存在,检查目录是否存在,该目录是否是可写的。            //如果getParent()方法返回空值,说明该目录为当前目录            String parent = to_file.getParent();  //目标目录            if (parent == null)     //如果是空值,使用当前目录		parent = System.getProperty("user.dir");            File dir = new File(parent);          //将它转换成文件。            if (!dir.exists())                abort("目标目录不存在。 "+parent);            if (dir.isFile())                abort("指定的目标不是目录 " + parent);            if (!dir.canWrite())                abort("指定的目标目录不是可写的 " + parent);        }                // 如果程序运行到这里,证明一切正常,这时开始复制文件。        FileInputStream from = null;  //读取源文件的数据流        FileOutputStream to = null;   //写入目标文件的数据流        try {            from = new FileInputStream(from_file);  //创建输入流            to = new FileOutputStream(to_file);     //创建输出流            byte[] buffer = new byte[4096];                     int bytes_read;                         //缓存中的字节数目            //将一段字节流读到缓存中,然后将它们写出来,循环直到文件末尾(当read()返回-1时)停止            while((bytes_read = from.read(buffer)) != -1) //读取流直到文件末尾                to.write(buffer, 0, bytes_read);            //写入        }               //关闭流        finally {            if (from != null) try { from.close(); } catch (IOException e) { ; }            if (to != null) try { to.close(); } catch (IOException e) { ; }        }    }    /**抛出异常 */    private static void abort(String msg) throws IOException {         throw new IOException("文件复制: " + msg);     }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -