springboot 常用工具(IOUtils、FileUtils)


springboot 常用工具(IOUtils、FileUtils)

****************************

相关类

IOUtils:输入输出流操作

public class IOUtils {

*********************
静态方法

****************
使用缓冲读取数据

    public static InputStream toBufferedInputStream(InputStream input) throws IOException {
    public static InputStream toBufferedInputStream(InputStream input, int size) throws IOException {

    public static BufferedInputStream buffer(InputStream inputStream) {
    public static BufferedInputStream buffer(InputStream inputStream, int size) {

    public static BufferedReader toBufferedReader(Reader reader) {
    public static BufferedReader toBufferedReader(Reader reader, int size) {

    public static BufferedReader buffer(Reader reader) {
    public static BufferedReader buffer(Reader reader, int size) {

****************
使用缓冲输出数据

    public static BufferedWriter buffer(Writer writer) {
    public static BufferedWriter buffer(Writer writer, int size) {

    public static BufferedOutputStream buffer(OutputStream outputStream) {
    public static BufferedOutputStream buffer(OutputStream outputStream, int size) {

***************
将数据读入内存

    public static byte[] toByteArray(InputStream input) throws IOException {
    public static byte[] toByteArray(InputStream input, long size) throws IOException {
    public static byte[] toByteArray(InputStream input, int size) throws IOException {
    public static byte[] toByteArray(Reader input, Charset encoding) throws IOException {
    public static byte[] toByteArray(Reader input, String encoding) throws IOException {

    public static byte[] toByteArray(URI uri) throws IOException {
    public static byte[] toByteArray(URL url) throws IOException {
    public static byte[] toByteArray(URLConnection urlConn) throws IOException {

****************
将数据转换为字符数组

    public static char[] toCharArray(InputStream is, Charset encoding) throws IOException {
    public static char[] toCharArray(InputStream is, String encoding) throws IOException {
    public static char[] toCharArray(Reader input) throws IOException {

*******************
将数据转换为字符串

    public static String toString(InputStream input, Charset encoding) throws IOException {
    public static String toString(InputStream input, String encoding) throws IOException {
    public static String toString(Reader input) throws IOException {
    public static String toString(URI uri, Charset encoding) throws IOException {
    public static String toString(URI uri, String encoding) throws IOException {
    public static String toString(URL url, Charset encoding) throws IOException {
    public static String toString(URL url, String encoding) throws IOException {
    public static String toString(byte[] input, String encoding) throws IOException {

****************
resource转换为string、byte[]、URL

    public static String resourceToString(String name, Charset encoding) throws IOException {
    public static String resourceToString(String name, Charset encoding, ClassLoader classLoader) throws IOException {

    public static byte[] resourceToByteArray(String name) throws IOException {
    public static byte[] resourceToByteArray(String name, ClassLoader classLoader) throws IOException {

    public static URL resourceToURL(String name) throws IOException {
    public static URL resourceToURL(String name, ClassLoader classLoader) throws IOException {

*******************
读取行数据转换为字符串

    public static List<String> readLines(InputStream input, Charset encoding) throws IOException {
    public static List<String> readLines(InputStream input, String encoding) throws IOException {
    public static List<String> readLines(Reader input) throws IOException {


    public static LineIterator lineIterator(Reader reader) {
    public static LineIterator lineIterator(InputStream input, Charset encoding) throws IOException {
    public static LineIterator lineIterator(InputStream input, String encoding) throws IOException {

*******************
转换为输入流

    public static InputStream toInputStream(CharSequence input, Charset encoding) {
    public static InputStream toInputStream(CharSequence input, String encoding) throws IOException {

    public static InputStream toInputStream(String input, Charset encoding) {
    public static InputStream toInputStream(String input, String encoding) throws IOException {

******************
将数据写入到输出流

    public static void write(byte[] data, OutputStream output) throws IOException {
    public static void writeChunked(byte[] data, OutputStream output) throws IOException {

    public static void write(byte[] data, Writer output, Charset encoding) throws IOException {
    public static void write(byte[] data, Writer output, String encoding) throws IOException {
    public static void write(char[] data, Writer output) throws IOException {
    public static void writeChunked(char[] data, Writer output) throws IOException {

    public static void write(char[] data, OutputStream output, Charset encoding) throws IOException {
    public static void write(char[] data, OutputStream output, String encoding) throws IOException {

    public static void write(CharSequence data, Writer output) throws IOException {
    public static void write(CharSequence data, OutputStream output, Charset encoding) throws IOException {
    public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {

    public static void write(String data, Writer output) throws IOException {
    public static void write(String data, OutputStream output, Charset encoding) throws IOException {
    public static void write(String data, OutputStream output, String encoding) throws IOException {


    public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding) throws IOException {
    public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, String encoding) throws IOException {
    public static void writeLines(Collection<?> lines, String lineEnding, Writer writer) throws IOException {

***********************
输入流复制到输出流

    public static int copy(InputStream input, OutputStream output) throws IOException {
    public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
    public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
    public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length) throws IOException {
    public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length, byte[] buffer) throws IOException {

    public static void copy(InputStream input, Writer output, Charset inputEncoding) throws IOException {
    public static void copy(InputStream input, Writer output, String inputEncoding) throws IOException {

    public static int copy(Reader input, Writer output) throws IOException {
    public static long copyLarge(Reader input, Writer output) throws IOException {
    public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException {
    public static long copyLarge(Reader input, Writer output, long inputOffset, long length) throws IOException {
    public static long copyLarge(Reader input, Writer output, long inputOffset, long length, char[] buffer) throws IOException {

    public static void copy(Reader input, OutputStream output, Charset outputEncoding) throws IOException {
    public static void copy(Reader input, OutputStream output, String outputEncoding) throws IOException {

******************
比较流是否相等

    public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
    public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
    public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IOException {

******************
跳过流

    public static long skip(InputStream input, long toSkip) throws IOException {
    public static long skip(ReadableByteChannel input, long toSkip) throws IOException {
    public static long skip(Reader input, long toSkip) throws IOException {

    public static void skipFully(InputStream input, long toSkip) throws IOException {
    public static void skipFully(ReadableByteChannel input, long toSkip) throws IOException {
    public static void skipFully(Reader input, long toSkip) throws IOException {

********************
读取流数据

    public static int read(Reader input, char[] buffer, int offset, int length) throws IOException {
    public static int read(Reader input, char[] buffer) throws IOException {
    public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException {
    public static int read(InputStream input, byte[] buffer) throws IOException {
    public static int read(ReadableByteChannel input, ByteBuffer buffer) throws IOException {

    public static void readFully(Reader input, char[] buffer, int offset, int length) throws IOException {
    public static void readFully(Reader input, char[] buffer) throws IOException {
    public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException {
    public static void readFully(InputStream input, byte[] buffer) throws IOException {
    public static byte[] readFully(InputStream input, int length) throws IOException {
    public static void readFully(ReadableByteChannel input, ByteBuffer buffer) throws IOException {

FileUtils:文件操作

public class FileUtils {

*******************
静态方法

**************
获取文件、目录

    public static File getFile(File directory, String... names) {
    public static File getFile(String... names) {

    public static String getTempDirectoryPath() {
    public static File getTempDirectory() {

    public static String getUserDirectoryPath() {
    public static File getUserDirectory() {


***************
获取文件输入流

    public static FileInputStream openInputStream(File file) throws IOException {
    public static FileOutputStream openOutputStream(File file) throws IOException {
    public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {


****************
列出文件、目录

    public static Collection<File> listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
    public static Collection<File> listFiles(File directory, String[] extensions, boolean recursive) {
    public static Collection<File> listFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {

    public static Iterator<File> iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
    public static Iterator<File> iterateFiles(File directory, String[] extensions, boolean recursive) {
    public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {

*******************
判断文件是否相等

    public static boolean contentEquals(File file1, File file2) throws IOException {
    public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) throws IOException {


******************
url、文件转换

    public static File toFile(URL url) {
    public static File[] toFiles(URL[] urls) {

    public static URL[] toURLs(File[] files) throws IOException {
    static String decodeUrl(String url) {

****************
文件、目录复制操作

    public static void copyFile(File srcFile, File destFile) throws IOException {
    public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    public static long copyFile(File input, OutputStream output) throws IOException {

    public static void copyFileToDirectory(File srcFile, File destDir) throws IOException {
    public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) throws IOException {

    public static void copyDirectory(File srcDir, File destDir) throws IOException {
    public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException {
    public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException {
    public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException {

    public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException {

    public static void copyToDirectory(File src, File destDir) throws IOException {
    public static void copyToDirectory(Iterable<File> srcs, File destDir) throws IOException {


*****************
url、字节流、文件操作

    public static void copyURLToFile(URL source, File destination) throws IOException {
    public static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout) throws IOException {

    public static void copyInputStreamToFile(InputStream source, File destination) throws IOException {
    public static void copyToFile(InputStream source, File destination) throws IOException {

******************
删除目录

    public static void deleteDirectory(File directory) throws IOException {
    public static boolean deleteQuietly(File file) {

******************
目录判断

    public static boolean directoryContains(File directory, File child) throws IOException {
    public static void cleanDirectory(File directory) throws IOException {

*******************
读取文件操作

    public static String readFileToString(File file, Charset encoding) throws IOException {
    public static String readFileToString(File file, String encoding) throws IOException {

    public static byte[] readFileToByteArray(File file) throws IOException {

    public static List<String> readLines(File file, Charset encoding) throws IOException {
    public static List<String> readLines(File file, String encoding) throws IOException {

    public static LineIterator lineIterator(File file, String encoding) throws IOException {
    public static LineIterator lineIterator(File file) throws IOException {

***************
输出到文件

    public static void writeStringToFile(File file, String data, Charset encoding) throws IOException {
    public static void writeStringToFile(File file, String data, String encoding) throws IOException {
    public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException {
    public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {

    public static void write(File file, CharSequence data, Charset encoding) throws IOException {
    public static void write(File file, CharSequence data, String encoding) throws IOException {
    public static void write(File file, CharSequence data, Charset encoding, boolean append) throws IOException {
    public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {

    public static void writeByteArrayToFile(File file, byte[] data) throws IOException {
    public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
    public static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws IOException {
    public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException {

    public static void writeLines(File file, String encoding, Collection<?> lines) throws IOException {
    public static void writeLines(File file, String encoding, Collection<?> lines, boolean append) throws IOException {
    public static void writeLines(File file, Collection<?> lines) throws IOException {
    public static void writeLines(File file, Collection<?> lines, boolean append) throws IOException {
    public static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding) throws IOException {
    public static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding, boolean append) throws IOException {
    public static void writeLines(File file, Collection<?> lines, String lineEnding) throws IOException {
    public static void writeLines(File file, Collection<?> lines, String lineEnding, boolean append) throws IOException {

****************
强制删除创建文件、目录

    public static void forceDelete(File file) throws IOException {
    public static void forceDeleteOnExit(File file) throws IOException {
    public static void forceMkdir(File directory) throws IOException {
    public static void forceMkdirParent(File file) throws IOException {

****************
移动文件目录

    public static void moveDirectory(File srcDir, File destDir) throws IOException {
    public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException {
    public static void moveFile(File srcFile, File destFile) throws IOException {
    public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException {
    public static void moveToDirectory(File src, File destDir, boolean createDestDir) throws IOException {

****************
文件目录大小

    public static long sizeOf(File file) {
    public static BigInteger sizeOfAsBigInteger(File file) {
    public static long sizeOfDirectory(File directory) {
    public static BigInteger sizeOfDirectoryAsBigInteger(File directory) {

*****************
文件新旧

    public static boolean isFileNewer(File file, File reference) {
    public static boolean isFileNewer(File file, Date date) {
    public static boolean isFileNewer(File file, long timeMillis) {

    public static boolean isFileOlder(File file, File reference) {
    public static boolean isFileOlder(File file, Date date) {
    public static boolean isFileOlder(File file, long timeMillis) {

******************
其他方法

    public static void touch(File file) throws IOException {  
    public static File[] convertFileCollectionToFileArray(Collection<File> files) {
 
    public static boolean waitFor(File file, int seconds) {

    public static String byteCountToDisplaySize(BigInteger size) {
    public static String byteCountToDisplaySize(long size) {

    public static long checksumCRC32(File file) throws IOException {
    public static Checksum checksum(File file, Checksum checksum) throws IOException {

*********************************

示例

*********************

controller 层

@RestController
public class HelloController {

    @Value("classpath:/static/file/hello.txt")
    private Resource resource;

    @Value("classpath:/static/file/hello.txt")
    private File file;

    @Value("http://www.baidu.com")
    private Resource resource2;

    @Value("http://www.baidu.com")
    private URL url;

    @RequestMapping("/hello2")
    public String hello2() throws Exception{
        System.out.println("hello2 输出:");
        System.out.println(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8)+"\n");

        return "success 2";
    }

    @RequestMapping("/hello3")
    public String hello3() throws Exception{
        System.out.println("hello3 输出:");
        System.out.println(FileUtils.readFileToString(file,StandardCharsets.UTF_8)+"\n");

        return "success 3";
    }

    @RequestMapping("/hello4")
    public String hello4() throws Exception{
        System.out.println("hello4 输出:");
        System.out.println(IOUtils.toString(resource2.getInputStream(), StandardCharsets.UTF_8));
        System.out.println(IOUtils.toString(url, StandardCharsets.UTF_8));

        return "success 4";
    }
}

*************************

控制台输出

hello2 输出:
hello world
hello world 2
hello world 3
hello world 4

hello3 输出:
hello world
hello world 2
hello world 3
hello world 4

hello4 输出:
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
发布了320 篇原创文章 · 获赞 91 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/103629494
今日推荐