您的位置:首页 > 编程语言 > Java开发

JAVA文件工具类之——文件写入(byte数组、String方式、url写入方式)

2017-09-26 19:04 513 查看
/**
* 将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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: