java IOUtils.java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java IOUtils.java相关的知识,希望对你有一定的参考价值。
package fr.hugo4715.loadBalancer.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOUtils {
public static void copy(File sourceLocation, File targetLocation) throws IOException {
System.out.println("Copying " + sourceLocation.getAbsolutePath() + " to " + targetLocation);
if (sourceLocation.isDirectory()) {
copyDirectory(sourceLocation, targetLocation);
} else {
copyFile(sourceLocation, targetLocation);
}
}
private static void copyDirectory(File source, File target) throws IOException {
if (!target.exists()) {
target.mkdir();
}
for (String f : source.list()) {
copy(new File(source, f), new File(target, f));
}
}
private static void copyFile(File source, File target) throws IOException {
try (
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target)
) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
public static void deleteDir(File file) {
System.out.println("Deleting " + file.getAbsolutePath());
File[] contents = file.listFiles();
if (contents != null) {
for (File f : contents) {
deleteDir(f);
}
}
file.delete();
}
}
以上是关于java IOUtils.java的主要内容,如果未能解决你的问题,请参考以下文章
Java Switch
Java Math
Java 布尔运算
java [Java] Java常用代码#java
Java - 35 Java 实例
Java While 循环