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

三、写文件

四、文件重命名

五、转移文件目录

六、读文件

七、创建文件(文件夹)

八、删除文件(目录)

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

原文地址:https://www.cnblogs.com/yourGod/p/9172539.html