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)可以直接实现文件的复制。

    三、写文件

    四、文件重命名

    五、转移文件目录

    六、读文件

    七、创建文件(文件夹)

    八、删除文件(目录)

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

  • 相关阅读:
    SpringBoot(一)_快速实战搭建项目
    maven入门 (二)_私服安装与上传下载
    maven入门(一)
    Ionic 2 开发(一)_安装与目录结构
    echarts添加点击事件
    win下 git gui 使用教程
    安装centos6.3
    PAT甲级题分类汇编——图
    PAT甲级题分类汇编——树
    PAT甲级题分类汇编——理论
  • 原文地址:https://www.cnblogs.com/yourGod/p/9172539.html
Copyright © 2011-2022 走看看