/**
* 将byte数组写入文件
*
* @param path
* @param fileName
* @param content
* @throws IOException
*/
public static void writeFile(String path, String fileName, byte[] content)
throws IOException {
try {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write(content);
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 写文件方法
*
* @param path
* @param name
* @param content
* @param encoding
*/
public static void writeFile(String path, String name, String content,
String encoding) {
if (content == null)
content = "";
try {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
//利用org.apache.commons.io.FileUtils快速写文件
org.apache.commons.io.FileUtils.writeStringToFile(new File(path
+ name), content, encoding);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 写文件
*
* @param url
* @param dist
* @return
*/
public static boolean writeFile(String url, File dist) {
try {
writeFile(new URL(url).openStream(), dist);
return true;
} catch (IOException e) {
//无法下载图片,文本等
}
return false;
}
JAVA文件工具类之——文件写入(byte数组、String方式、url写入方式)
最新推荐文章于 2025-03-15 00:00:00 发布