<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
package com.ylink.cms.lucene;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
public class test1 {
@Test
public void showInfoByIOUtils() throws IOException {
URL url = new URL("https://www.baidu.com");
InputStream in = url.openStream();
System.out.println(in);
try {
System.out.println(IOUtils.toString(in));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void editFile() throws IOException {
String fileName = "f:/1.txt";
File file = new File(fileName);
String fileContent = "";
try {
fileContent = FileUtils.readFileToString(file, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
fileContent += "Helloworld,我也是";
try {
FileUtils.writeStringToFile(file, fileContent, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void copyFile() throws IOException {
try {
File src = new File("f:/1.txt");
File dest = new File("f:/2.txt");
FileUtils.copyFile(src, dest);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void download() throws IOException {
try {
InputStream in = new URL("http://www.baidu.com/img/baidu_logo.gif").openStream();
byte[] gif = IOUtils.toByteArray(in);
FileUtils.writeByteArrayToFile(new File("f:/test.gif"), gif);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void copy2dir() throws IOException {
try {
File srcfile = new File("f:/1.txt");
File destDir = new File("f:file");
FileUtils.copyFileToDirectory(srcfile, destDir);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void copyUrl2File() throws Exception {
URL url = new URL("http://www.163.com");
File file = new File("f:\\163.html");
FileUtils.copyURLToFile(url, file);
}
@Test
public void cleanOrDeleteDirectory() throws IOException {
try {
File dir = new File("f:\\file");
FileUtils.deleteDirectory(dir);
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
@Test
public void deleteFile() throws IOException {
try {
File file = new File("f:\\file");
FileUtils.deleteQuietly(file);
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
}