All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.zcltd.btg.sutil.FileUtil Maven / Gradle / Ivy
package cn.zcltd.btg.sutil;
import java.io.*;
/**
* util:file
*/
public final class FileUtil {
public static final String ENCODING_UTF_8 = "UTF-8";
public static final String ENCODING_GBK = "GBK";
/**
* 获取文件后缀名
*
* @param file 文件对象
* @return 后缀名
*/
public static String getSuffix(File file) {
String fileName = file.getName();
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* 拷贝文件
*
* @param file 文件对象
* @param copyFile 目标文件对象
*/
public static void copyTo(File file, File copyFile) {
try {
if (!file.exists()) {
throw new RuntimeException("file not exists," + file.getPath());
}
File copyDir = copyFile.getParentFile();
if (!copyDir.exists() || !copyDir.isDirectory()) {
boolean isSuccess = copyDir.mkdirs();
if (!isSuccess) {
throw new RuntimeException("directory does not exist and create the failure," + copyDir.getCanonicalPath());
}
}
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(copyFile);
byte[] buffer = new byte[1024];
int ins;
while ((ins = in.read(buffer)) != -1) {
out.write(buffer, 0, ins);
}
out.close();
in.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 从指定文件中读取内容
*
* @param is 输入流
* @return 内容
*/
public static byte[] read(InputStream is) {
byte[] buffer;
try {
int size = is.available();
buffer = new byte[size];
is.read(buffer);
is.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
return buffer;
}
/**
* 从指定文件中读取内容
*
* @param file 文件
* @return 内容
*/
public static byte[] read(File file) {
if (!file.exists()) {
throw new RuntimeException("file not exists:" + file.getAbsolutePath());
}
if (file.isDirectory()) {
throw new RuntimeException("file is a folder:" + file.getAbsolutePath());
}
try {
InputStream is = new FileInputStream(file);
return read(is);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 从指定文件中读取内容
*
* @param filePath 文件路径
* @return 内容
*/
public static byte[] read(String filePath) {
File file = new File(filePath);
return read(file);
}
/**
* 从输入流中读取内容为字符串
*
* @param is 输入流
* @param encoding 字符编码
* @return 内容
*/
public static String readToString(InputStream is, String encoding) {
byte[] bytes = read(is);
try {
return new String(bytes, encoding);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 从输入流中读取内容为字符串
*
* @param is 输入流
* @return 内容
*/
public static String readToString(InputStream is) {
byte[] bytes = read(is);
try {
return new String(bytes);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 从指定文件中读取内容为字符串
*
* @param file 文件
* @param encoding 字符编码
* @return 内容
*/
public static String readToString(File file, String encoding) {
byte[] bytes = read(file);
try {
return new String(bytes, encoding);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 从指定文件中读取内容为字符串
*
* @param file 文件
* @return 内容
*/
public static String readToString(File file) {
return new String(read(file));
}
/**
* 从指定文件中读取内容为字符串
*
* @param filePath 文件路径
* @param encoding 字符编码
* @return 内容
*/
public static String readToString(String filePath, String encoding) {
byte[] bytes = read(filePath);
try {
return new String(bytes, encoding);
} catch (Exception e) {
throw new RuntimeException("read file error:" + filePath, e);
}
}
/**
* 从指定文件中读取内容为字符串
*
* @param filePath 文件路径
* @return 内容
*/
public static String readToString(String filePath) {
return new String(read(filePath));
}
/**
* 将内容写入到指定文件
*
* @param is 输入流
* @param os 输出流
*/
public static void write(InputStream is, OutputStream os) {
try {
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
os.close();
is.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将内容写入到指定文件
*
* @param os 输出流
* @param content 内容
*/
public static void write(OutputStream os, byte[] content) {
try {
os.write(content);
os.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将内容写入到指定文件
*
* @param os 输出流
* @param content 内容
* @param encoding 编码
*/
public static void write(OutputStream os, String content, String encoding) {
byte[] bytes;
try {
bytes = content.getBytes(encoding);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
write(os, bytes);
}
/**
* 将内容写入到指定文件
*
* @param file 文件
* @param content 内容
*/
public static void write(File file, byte[] content) {
if (file.isDirectory()) {
throw new RuntimeException("file is a folder:" + file.getAbsolutePath());
}
try {
if (!file.exists()) {
boolean isCreate = file.createNewFile();
if (!isCreate) {
throw new RuntimeException("create file error:" + file.getAbsolutePath());
}
}
FileOutputStream os = new FileOutputStream(file);
write(os, content);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将内容写入到指定文件
*
* @param file 文件
* @param content 内容
* @param encoding 编码
*/
public static void write(File file, String content, String encoding) {
byte[] bytes;
try {
bytes = content.getBytes(encoding);
} catch (Exception e) {
throw new RuntimeException("write file error:" + file.getAbsolutePath(), e);
}
write(file, bytes);
}
/**
* 将内容写入到指定文件
*
* @param file 文件
* @param content 内容
*/
public static void write(File file, String content) {
write(file, content.getBytes());
}
/**
* 将内容写入到指定文件
*
* @param filePath 文件路径
* @param content 内容
*/
public static void write(String filePath, byte[] content) {
File file = new File(filePath);
write(file, content);
}
/**
* 将内容写入到指定文件
*
* @param filePath 文件路径
* @param content 内容
* @param encoding 编码
*/
public static void write(String filePath, String content, String encoding) {
File file = new File(filePath);
byte[] bytes;
try {
bytes = content.getBytes(encoding);
} catch (Exception e) {
throw new RuntimeException("write file error:" + filePath, e);
}
write(file, bytes);
}
/**
* 将内容写入到指定文件
*
* @param filePath 文件路径
* @param content 内容
*/
public static void write(String filePath, String content) {
File file = new File(filePath);
write(file, content.getBytes());
}
}