至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html

package com.wz.apache.fileUtils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import javax.print.attribute.standard.Copies;
import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Scanner; /**
* 测试io工具包
* 本人英语水平不过关,简单理解下
* (注释的第一行是测试的方法,第二行是官方文档的解释,第三行是我测试做的结论)
* @author WZLOVE
* @create 2018-04-30 12:30
*/
public class MainTest { public static void main(String[] args) {
String path = "H:\\javaTest\\iotest";
String path1 = "H:\\javaTest\\iotest\\1.txt";
String path2 = "H:\\javaTest\\iotest\\2.txt";
String path3 = "H:\\MyTest";
String path4 = "H:\\MyTest\\test";
File file = new File(path);
File file1 = new File(path1);
File file2 = new File(path2);
File file3 = new File(path3);
File file4 = new File(path4);
FileUtils fu = new FileUtils(); // 复制操作 // cleanDirectory(File directory)
// Cleans a directory without deleting it.
// 删除该文件夹下的所有东西(如果该路径是文件或者路径不存在是会报java.lang.IllegalArgumentException异常)
try {
FileUtils.cleanDirectory(file);
System.out.println("删除成功");
} catch (IOException e) {
e.printStackTrace();
} // contentEquals(File file1, File file2)
// Compares the contents of two files to determine if they are equal or not.
// 测试两个文件的内容相不相同
try {
System.out.println(FileUtils.contentEquals(file1,file2));
} catch (IOException e) {
e.printStackTrace();
} // copyDirectory(File srcDir, File destDir)
// Copies a whole directory to a new location preserving the file dates.
// 复制第一个参数的文件夹下的所有内容复制到第二个参数的文件夹下
try {
FileUtils.copyDirectory(file,file3);
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} // copyDirectoryToDirectory(File srcDir, File destDir)
// Copies a directory to within another directory preserving the file dates.
// 将第一个参数的整个文件夹(即iotest文件夹)复制到第二个路径下
try {
FileUtils.copyDirectoryToDirectory(file, file3);
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} // copyFile(File srcFile, File destFile)
// Copies a file to a new location preserving the file date.
// 将一个文件的内容复制到另一个文件中
try {
FileUtils.copyFile(file1, file2);
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} // copyFileToDirectory(File srcFile, File destDir)
// Copies a file to a directory preserving the file date.
// 将一个文件复制到另一个文件夹下(不做测试了) System.out.println("=================================================="); // 读写操作 // readFileToString(File file, String encoding)
// Reads the contents of a file into a String.
// 读取文件内容返回String,原样输出
try {
String str = FileUtils.readFileToString(file1, "UTF-8");
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} // readLines(File file, String encoding)
// Reads the contents of a file line by line to a List of Strings.
// 返回String集合
try {
List<String> strList = FileUtils.readLines(file1, "UTF-8");
for (String s : strList) {
System.out.println(s);
}
System.out.println(strList.size());
} catch (IOException e) {
e.printStackTrace();
} // writeStringToFile(File file, String data, String encoding)
// Writes a String to a file creating the file if it does not exist.
// 将String数据写入文件(只能输入一行,如果后续还有输入,将覆盖前面的内容,下一个方法将是追加)
try {
Scanner in = new Scanner(System.in);
while (true){
System.out.println("请输入内容(结束请输入break):");
String str = in.nextLine();
if(!str.equals("break")){
FileUtils.writeStringToFile(file2, str, "UTF-8");
} else {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} // writeStringToFile(File file, String data, String encoding, boolean append)
// Writes a String to a file creating the file if it does not exist.
// 将String数据写入文件,多个string数据就追加(记住一点文件里不会换行)
try {
Scanner in = new Scanner(System.in);
while (true){
System.out.println("请输入内容(结束请输入break):");
String str = in.nextLine();
if(!str.equals("break")){
FileUtils.writeStringToFile(file2, str, "UTF-8",true);
} else {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} System.out.println("============================================");
// 网络相关 // copyURLToFile(URL source, File destination)
// Copies bytes from the URL source to a file destination.
// 获取一个网页的源代码,写入本地文件,还可以向本地写图片的等等
// 下面是两个例子,一个获取页面,一个获取图片(如果文件不存在会自动创建)
try {
FileUtils.copyURLToFile(new URL("https://blog.csdn.net/lixin2302007/article/details/78354929"),
new File("H:\\javaTest\\iotest\\test.html"));
System.out.println("复制完毕");
} catch (IOException e) {
e.printStackTrace();
}
// 写图片
try {
FileUtils.copyURLToFile(new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525082437668&di=18d455af6f3ffedc1f4d2e022cfe4af5&imgtype=0&src=http%3A%2F%2Fpic.eastlady.cn%2Fuploads%2Ftp%2F201705%2F9999%2F7e03b578dc.jpg"),
new File("H:\\javaTest\\iotest\\1.jpg"));
System.out.println("复制完毕");
} catch (IOException e) {
e.printStackTrace();
} } }

apache.commons.io.FileUtils的常用操作的更多相关文章

  1. Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils

    1.错误叙述性说明 警告: Could not create JarEntryRevision for [jar:file:/D:/MyEclipse/apache-tomcat-7.0.53/web ...

  2. java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.getTempDirectory()Ljava/io/File;

    我出现这个问题的原因是使用ueditor上传图片 如果不是commons.io的jar包缺失,就是jar包有冲突 另外:最新的ueditor(1.4.3.1)使用的是commons-io-2.4.ja ...

  3. org.apache.commons.io——FileUtils学习笔记

    FileUtils类的应用 1.写入一个文件: 2.从文件中读取: 3.创建一个文件夹,包括文件夹: 4.复制文件和文件夹: 5.删除文件和文件夹: 6.从URL地址中获取文件: 7.通过文件过滤器和 ...

  4. java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.copyInputStreamToFile(Ljava/io/InputStream;Ljava/io/File;)V

    昨天用的好好的,今天就不行了 也懒得搞 具体原因就是引用的问题 找了个能用的pom 直接贴过去完事儿

  5. Apache Commons IO之FileUtils的常用方法

    Apache Commons IO 在学习io流的时候研究(翻译)了一下这个,只有FileUtils的某些方法,并不全面,还请谅解 org.apache.commons.io 这个包下定义了基于 st ...

  6. Java (三)APACHE Commons IO 常规操作

    上一篇:Java (二)基于Eclipse配置Commons IO的环境 例1:查看文件.文件夹的长度(大小). 1 import java.io.File; 2 3 import org.apach ...

  7. Apache Commons IO入门教程(转)

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  8. [转]Apache Commons IO入门教程

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  9. .apache.commons.io 源代码学习(一)

    java的初学者,准备通读各种高水平源代码,提升能力. 为了避免自己的惰性,写博客. 版本:2.5 开发平台:netbeans. 今天是第一天,网上先看个例子:http://www.importnew ...

随机推荐

  1. http跳转https方法:百度云如何让http自动跳转到https【免费SSL证书使用FAQ】

    之前的一篇文章已经给大家提供了免费SSL证书的申请方法,这一篇文章是告诉大家在使用免费的SSL证书时可能会遇到的问题[怎么让http自动跳转到https以及http与https同时使用]的解决方法. ...

  2. 2019CSUST集训队选拔赛题解(二)

    凛冬将至 Description 维斯特洛大陆的原住民是森林之子,他们长得如孩童一般,善于使用石器,威力值35,用树叶树枝作为衣物,在森林里繁衍生息,与万物和平相处.他们会使用古老的魔法(比如绿之视野 ...

  3. which命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/jkin/p/10289085.html Linux which命令用于查找文件. which指令会在环境变量$PATH ...

  4. 对于新手来说,Python 中有哪些难以理解的概念?

    老手都是从新手一路过来的,提起Python中难以理解的概念,可能很多人对于Python变量赋值的机制有些疑惑,不过对于习惯于求根究底的程序员,只有深入理解了某个事物本质,掌握了它的客观规律,才能得心应 ...

  5. dubbo支持协议及具体对比

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...

  6. sprint2(第三天)

    展板 燃尽图

  7. 四则运算(window窗体程序)

    我的第一个程序 忙活了半个下午做出来了,勉强可以见人,虽然还有些瑕疵,但是我也尽力了...... 我做的是一个能对0--10之间的整数进行四则运算的,题目的数据是程序自动生成的,而且程能接收用户输入的 ...

  8. Git的基本使用方法和安装&心得体会(使用git命令行)

    这是补发的,使用命令行操作的. (1)选择本地repository的路径 找到后点鼠标右键,选择git bash here. (2) clone到本地 在命令行输入 git clone ADDRESS ...

  9. IEEE 802.11 无限局域网

    (1)无线通讯的两个重要特征 ——Hidden node problem 双方虽然听不到对方的讯号,但同时传送给相同的对象导致了碰撞(这个时候双方都不知道发生了碰撞) ——Exposed node p ...

  10. 团队作业8——测试与发布(Beta阶段)之展示博客

    展示博客 1. 团队成员的简介和个人博客地址,团队的源码仓库地址. a.陈福鹏 擅长技术:java.web等网站方面技术: 博客:http://www.cnblogs.com/royalchen/b. ...