zoukankan      html  css  js  c++  java
  • JAVA对文件的操作

    文件的类型依据用途分为:

    • 文本
    • 图片
    • 音频
    • 视频
    • 数据

    文件的操作类型分为:

    • 创建
    • 打开
    • 写入
    • 修改
    • 保存
    • 关闭

    文件的状态分为: 

    • 有(空间)
    • 无(空间)
    • 是否修改(时间)

    -------------------------------------------------以下部分是JAVA的操作代码---------------------------------------------------------

    一、获得控制台用户输入信息

    public static String getIntputMessage() throws IOException {
            System.out.println("请输入命令:");
            byte buffer[] = new byte[1024];
            int count = System.in.read(buffer);
            char[] ch = new char[count];//最后的两位是结束符,删除不要[但是运行结果中,如果-2会丢失字符]
            for (int i = 0 ; i< count;i++){
                ch[i] = (char) buffer[i];
            }
            String string = new String(ch);
            return string;
        }

    二、复制文件

      

    //以文件流方式复制文件
        public void copyFile(String src,String dest) throws IOException {
            FileInputStream in = new FileInputStream(src);
            File file = new File(dest);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            int c;
            byte bufferr[] = new byte[1024];
            while ((c = in.read(bufferr)) != -1) {
                for (int i = 0; i < c; i++) {
                    out.write(bufferr[i]);
                }
            }
            in.close();
            out.close();
        }//该方法可以实现多种类型的复制,比如txt,xml,jpg,doc等多种文件格式

      但同时,封装好的 Apache Commons IO的方法FileUtils.copyFile(source,dest);和JDK 的 Files.copy(Path path,OutputStream outputstream)可以直接实现文件的复制。

    三、写文件

    四、文件重命名

    五、转移文件目录

    六、读文件

    七、创建文件(文件夹)

    八、删除文件(目录)

    -------------------------------------------------以下部分是关于文件、文件夹的关系----------------------------------------------

  • 相关阅读:
    create-react-app
    简单的PHP的任务队列
    Yii框架中使用PHPExcel导出Excel文件
    Android 使用全局变量的问题
    Android 退出整个应用程序
    new DialogInterface.OnClickListener()报错的解决办法
    Yii 日期时间过滤列 filter
    Yii 时间戳格式化显示的问题
    PullToRefreshListView 应用讲解
    Android:Layout_weight的深刻理解
  • 原文地址:https://www.cnblogs.com/yourGod/p/9172539.html
Copyright © 2011-2022 走看看