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()); } }
。。