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; }
相关文章推荐
- 读取Java文件到byte数组的三种方式及Java文件操作大全(包括文件加密,String加密)
- java读取文件读取工具类-将文件到byte数组的几种方式
- 工具类,下载文件,参数给个URL,返回一个Byte数组,byte数组当参数写入指定的路径中
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- java 16进制数据格式化处理工具类,16进制byte数组转String
- java中的文件操作:读取写入byte[]字节流、string字符串、list列表
- 读取Java文件到byte数组的三种方式
- java字节流的方式复制一个文件,按数组读取和写入
- 读取Java文件到byte数组的三种方式(转载)
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式
- 读取Java文件到byte数组的三种方式