本文介绍: Apache Commons包,FileUtils用法详解

FileUtils

org.apache.commons.io.FileUtils
下载地址commons-io

Maven依赖
<dependency>
    &lt;groupId&gt;commons-io</groupId&gt;
    <artifactId&gt;commons-io</artifactId&gt;
    <version&gt;2.10.0</version&gt;
</dependency&gt;
FileUtils是一个开源的Java库,提供了对文件目录进行操作工具类,包括复制、移动、删除重命名读取文件内容等常见的文件操作
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File source = new File("source.txt");
        File target = new File("target.txt");
        try {
            FileUtils.copyFile(source, target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File source = new File("source.txt");
        File target = new File("target.txt");
        try {
            FileUtils.moveFile(source, target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File file = new File("source.txt");
        try {
            FileUtils.forceDelete(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File file = new File("source.txt");
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File directory = new File("myDirectory");
        try {
            FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File file = new File("myFile.txt");
        FileUtils.deleteQuietly(file);
    }
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File file = new File("myFile.txt");
        try {
            FileUtils.writeStringToFile(file, "Hello, world!", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File source = new File("sourceDirectory");
        File target = new File("targetDirectory");
        try {
            FileUtils.copyDirectory(source, target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File directory = new File("myDirectory");
        try {
            FileUtils.forceMkdir(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File file = new File("myFile.txt");
        try {
            FileUtils.write(file, "Hello, world!", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.List;

public class FileUtilsDemo {
    public static void main(String[] args) {
        File directory = new File("myDirectory");
        List<File> files = (List<File>) FileUtils.listFiles(directory, null, false);
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}

原文地址:https://blog.csdn.net/jpfice/article/details/129479528

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_7167.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注