1、方法writeByteArrayToFile(File file, byte[] data):
public static void writeByteArrayToFile(File file, byte[] data) throws IOException { OutputStream out = null; try { out = openOutputStream(file); out.write(data); } finally { IOUtils.closeQuietly(out); } } public static FileOutputStream openOutputStream(File file) throws IOException { if (file.exists()) { if (file.isDirectory()) { throw new IOException("File '" + file + "' exists but is a directory"); } if (file.canWrite() == false) { throw new IOException("File '" + file + "' cannot be written to"); } } else { File parent = file.getParentFile(); if (parent != null && parent.exists() == false) { if (parent.mkdirs() == false) { throw new IOException("File '" + file + "' could not be created"); } } } return new FileOutputStream(file); }
知识点:
获得一个文件名路径的上一层路径
File file = new File("D:\\almanac\\Ex1.java");
String parentPath = file.getParent(); // D:\almanac
File parentDir = file.getParentFile(); // D:\almanac
为什么new FileOutPutStream和new File创建不了文件?java.io.FileNotFoundException 系统找不到指定的路径
Java FileOutputStream Create File if not exists
结论:new FileOutputStream(file)创建文件时需要确保文件夹存在。(1级目录不需要)
2.方法readFileToString(File file)
public static String readFileToString(File file) throws IOException { return readFileToString(file, null); } public static String readFileToString(File file, String encoding) throws IOException { InputStream in = null; try { in = openInputStream(file); return IOUtils.toString(in, encoding); } finally { IOUtils.closeQuietly(in); } } public static FileInputStream openInputStream(File file) throws IOException { if (file.exists()) { if (file.isDirectory()) { throw new IOException("File '" + file + "' exists but is a directory"); } if (file.canRead() == false) { throw new IOException("File '" + file + "' cannot be read"); } } else { throw new FileNotFoundException("File '" + file + "' does not exist"); } return new FileInputStream(file); }
//IOUtils: public static String toString(InputStream input, String encoding) throws IOException { StringWriter sw = new StringWriter(); copy(input, sw, encoding); return sw.toString(); //通过StringWriter输出内容 } public static void copy(InputStream input, Writer output, String encoding) throws IOException { if (encoding == null) { copy(input, output); } else { InputStreamReader in = new InputStreamReader(input, encoding); copy(in, output); } } public static void copy(InputStream input, Writer output) throws IOException { InputStreamReader in = new InputStreamReader(input); copy(in, output); } public static int copy(Reader input, Writer output) throws IOException { long count = copyLarge(input, output); if (count > Integer.MAX_VALUE) { return -1; } return (int) count; } public static long copyLarge(Reader input, Writer output) throws IOException { char[] buffer = new char[DEFAULT_BUFFER_SIZE]; long count = 0; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; }
总结:
Writer.write(char[] cbuf, int off, int len) throws IOException
//Writes a portion of an array of characters.
Parameters:
cbuf - Array of characters
off - Offset from which to start writing characters
len - Number of characters to write
- StringWriter sw = new StringWriter();
- return sw.toString(); //通过StringWriter
3、方法writeStringToFile(File , Content , encoding)
public static void writeStringToFile(File file, String data, String encoding) throws IOException { OutputStream out = null; try { out = openOutputStream(file); IOUtils.write(data, out, encoding); } finally { IOUtils.closeQuietly(out); } } public static FileOutputStream openOutputStream(File file) throws IOException { if (file.exists()) { if (file.isDirectory()) { throw new IOException("File '" + file + "' exists but is a directory"); } if (file.canWrite() == false) { throw new IOException("File '" + file + "' cannot be written to"); } } else { File parent = file.getParentFile(); if (parent != null && parent.exists() == false) { if (parent.mkdirs() == false) { throw new IOException("File '" + file + "' could not be created"); } } } return new FileOutputStream(file); } //IOUtils public static void write(String data, OutputStream output, String encoding) throws IOException { if (data != null) { if (encoding == null) { write(data, output); } else { output.write(data.getBytes(encoding)); } } } public static void write(String data, OutputStream output) throws IOException { if (data != null) { output.write(data.getBytes()); } }
public static void write(StringBuffer data, Writer output) throws IOException { if (data != null) { output.write(data.toString()); } } public static void write(StringBuffer data, OutputStream output) throws IOException { if (data != null) { output.write(data.toString().getBytes()); } }
。。
相关推荐
FileUtils实现文件下载,下载的文件会显示真是的文件名,下载的文件无论什么格式都不会在页面直接打开
用于快速读取File和写入File,org.apache.commons.io.FileUtils包含各种读取的方法,比传统的读写速度快,占用内存小
FileUtils.java 文件处理工具类
fileutils.zip,fileutils-一个简单的filewatcher实用程序一个简单的filewatcher实用程序
【Java基础】-- FileUtils工具类常用方法(csdn)————程序
文件工具类FileUtils,对文件中内容行数lines的总数统计
FileUtils.cpp pdal c++
关于文件操作工具类相关方法介绍,手工打造描述,请多多指教
,复制单个文件到指定路径,复制整个文件夹到指定路径,复制文件夹下所有文件到指定路径,删除单个文件,删除文件夹下所有文件,删除文件夹以及文件下下所有文件。。。等
实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
支持多线程上传下载,支持断点续传功能的一个工具类。
java web 上传下载工具类,压缩包内包含src和WebRoot,直接新建项目,然后复制这两个目录内的文件,覆盖新建项目中的文件即可。用法参见test用例
fileutils-maven-插件fileutils-maven-plugin 是一个构建插件,用于对文本文件执行一些基本操作。目标概述fileutils-maven-plugin 提供了几个目标。 fileutils-maven-plugin:concatFileList 将文件内容连接到输出...
android File操作工具类 提供了常用的File操作方法
java组件开发(12) IOUtils、FileUtils
fileutils fileutils具有实用程序功能,可以读取,写入和同步文件。用例写文件: write("/tmp/myfile.txt", std::string_view{"Hello, world!"});将文件同步到存储: sync("/tmp/myfile.txt");读取文件: std::...
利用java语言写的文件夹压缩和解压的代码,主要针对.7z格式和.zip格式。其中引入了相应maven依赖,可以完美实现多个文件的压缩和解压。