Java File 文件操作

java.io包里的类:

  • InputStream/OutputStream : 以字节为单位读写文件内容,一次读/写一个字节,常用于读二进制文件,如图片、音频、视频等文件

  • FileInputStream/FileOutputStream : 以字节为单位读写文件内容,一次读写多个字节

  • Reader/Writer : 以字符为单位,一次读写一个字符

  • BufferedReader/BufferedWriter : 以行为单位,一次读写一行,以换行符为结束标志

java.nio (new i/o)(jdk 1.4加入)
特点:非阻塞

创建方法

  1. boolean createNewFile() 不存在返回true 存在返回false
  2. boolean mkdir() 创建目录
  3. boolean mkdirs() 创建多级目录

删除方法

  1. boolean delete()
  2. boolean deleteOnExit() 文件使用完成后删除

判断方法

  1. boolean canExecute()判断文件是否可执行
  2. boolean canRead()判断文件是否可读
  3. boolean canWrite() 判断文件是否可写
  4. boolean exists() 判断文件是否存在
  5. boolean isDirectory() 判断是否是一个目录
  6. boolean isFile() 判断是否是一个文件
  7. boolean isHidden()8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断

获取方法

  1. String getName() 返回文件名
  2. String getPath()
  3. String getAbsolutePath()
  4. String getParent()//如果没有父目录返回null
  5. long lastModified()//获取最后一次修改的时间
  6. long length()
  7. boolean renameTo(File f)
  8. File[] liseRoots()//获取机器盘符
  9. String[] list()
  10. String[] list(FilenameFilter filter)

具体文件操作代码:

  1. 创建文件/文件夹 :

//创建文件夹
    public static void createFolder(String path){
        File file = new File(path);
        file.mkdirs();
    }
    
    //创建文件
    public static boolean createFile(String fileName){
        File file = new File(fileName);
        System.out.println(file.getAbsolutePath());
        boolean isSucess = false;
        try {
            isSucess = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSucess;
    }
  1. 写文件:

//写入文件
    public static void writeFile(String fileName){
        File file = new File(fileName);
        BufferedWriter bWriter = null;
        try {
            FileWriter writer = new FileWriter(file);
            bWriter = new BufferedWriter(writer);
            bWriter.write("hello ");
            bWriter.newLine();
            bWriter.write('a');
            bWriter.write(27);
            bWriter.write("world");
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (bWriter != null) {
                try {
                    bWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  1. 读文件:

//读取文件
    public static void readFile(String fileName){
        File file = new File(fileName);
        BufferedReader bReader = null;
        try {
            FileReader reader = new FileReader(file);
            bReader = new BufferedReader(reader);
            String line = bReader.readLine();
            while(line != null){
                System.out.println(line);
                line = bReader.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (bReader != null) {
                    bReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  1. 列出当前目录下所有文件:

//列出当前目录下的所有文件
    public static void listFile(String folderPath){
        File file = new File(folderPath);
        for(String fileName : file.list()){
            System.out.println("fileName : "+ fileName);
        }
    }
  1. 删除文件:

//删除文件
    public static void delFile(String folderPath){
        File file = new File(folderPath);
        System.out.println(folderPath);
        file.delete();
    }
  1. 拷贝文件:

  2. 方法一:
public static void copyFile(String oldPath, String newPath){
        File oldFile = new File(oldPath);
        File newFile = new File(newPath);
        BufferedReader bReader = null;
        BufferedWriter bWriter = null;
        try {
            FileReader fr = new FileReader(oldFile);
            bReader = new BufferedReader(fr);
            String all = "";
            String line = bReader.readLine();
            System.out.println("拷贝中。。。请稍候");
            while(line != null){
                all += line;
                line = bReader.readLine();
            }
            FileWriter fw = new FileWriter(newFile);
            bWriter = new BufferedWriter(fw);
            bWriter.write(all);
            System.out.println("完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                bReader.close();
                bWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  1. 方法二:
public static void copyFile2(File source, File dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            System.out.println("拷贝中。。。请稍候");
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
            System.out.println("完成!");
        } finally {
            input.close();
            output.close();
        }
    }
  1. 方法三:
public static void copyFiles(File source, File dest){
        try {
            System.out.println("拷贝中。。。请稍候");
            Files.copy(source.toPath(), dest.toPath());
            System.out.println("完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,917评论 18 399
  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 13,407评论 2 95
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,244评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,359评论 19 139
  • 时日过去,他忘记了自己所说的一切。有一天,他开始介意所有它曾说过“不介意”的事情。 下一次,当你听到“不介意”这三...
    金莲月阅读 2,701评论 0 2