FileUtils - 每天进步一点点! - ITeye博客

1、方法writeByteArrayToFile(File file, byte[] data)

Java代码  收藏代码 "收藏这段代码")

  1. publicstaticvoid writeByteArrayToFile(File file, byte[] data) throws IOException {  
  2.        OutputStream out = null;  
  3.        try {  
  4.            out = openOutputStream(file);  
  5.            out.write(data);  
  6.        } finally {  
  7.            IOUtils.closeQuietly(out);  
  8.        }  
  9.    }  
  10. publicstatic FileOutputStream openOutputStream(File file) throws IOException {  
  11.        if (file.exists()) {  
  12.            if (file.isDirectory()) {  
  13.                thrownew IOException("File '" + file + "' exists but is a directory");  
  14.            }  
  15.            if (file.canWrite() == false) {  
  16.                thrownew IOException("File '" + file + "' cannot be written to");  
  17.            }  
  18.        } else {  
  19.            File parent = file.getParentFile();  
  20.            if (parent != null && parent.exists() == false) {  
  21.                if (parent.mkdirs() == false) {  
  22.                    thrownew IOException("File '" + file + "' could not be created");  
  23.                }  
  24.            }  
  25.        }  
  26.        returnnew FileOutputStream(file);  
  27.    }  

    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)

Java代码  收藏代码 "收藏这段代码")

  1. publicstatic String readFileToString(File file) throws IOException {  
  2.         return readFileToString(file, null);  
  3.     }  
  4.     publicstatic String readFileToString(File file, String encoding) throws IOException {  
  5.         InputStream in = null;  
  6.         try {  
  7.             in = openInputStream(file);  
  8.             return IOUtils.toString(in, encoding);  
  9.         } finally {  
  10.             IOUtils.closeQuietly(in);  
  11.         }  
  12.     }  
  13.     publicstatic FileInputStream openInputStream(File file) throws IOException {  
  14.         if (file.exists()) {  
  15.             if (file.isDirectory()) {  
  16.                 thrownew IOException("File '" + file + "' exists but is a directory");  
  17.             }  
  18.             if (file.canRead() == false) {  
  19.                 thrownew IOException("File '" + file + "' cannot be read");  
  20.             }  
  21.         } else {  
  22.             thrownew FileNotFoundException("File '" + file + "' does not exist");  
  23.         }  
  24.         returnnew FileInputStream(file);  
  25.     }  

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

Java代码  收藏代码 "收藏这段代码")

  1. //IOUtils:
  2.     publicstatic String toString(InputStream input, String encoding)  
  3.             throws IOException {  
  4.         StringWriter sw = new StringWriter();  
  5.         copy(input, sw, encoding);  
  6.         return sw.toString();   //通过StringWriter输出内容
  7.     }  
  8.     publicstaticvoid copy(InputStream input, Writer output, String encoding)  
  9.             throws IOException {  
  10.         if (encoding == null) {  
  11.             copy(input, output);  
  12.         } else {  
  13.             InputStreamReader in = new InputStreamReader(input, encoding);  
  14.             copy(in, output);  
  15.         }  
  16.     }  
  17.     publicstaticvoid copy(InputStream input, Writer output)  
  18.             throws IOException {  
  19.         InputStreamReader in = new InputStreamReader(input);  
  20.         copy(in, output);  
  21.     }  
  22.     publicstaticint copy(Reader input, Writer output) throws IOException {  
  23.         long count = copyLarge(input, output);  
  24.         if (count > Integer.MAX_VALUE) {  
  25.             return -1;  
  26.         }  
  27.         return (int) count;  
  28.     }  
  29.     publicstaticlong copyLarge(Reader input, Writer output) throws IOException {  
  30.         char[] buffer = newchar[DEFAULT_BUFFER_SIZE];  
  31.         long count = 0;  
  32.         int n = 0;  
  33.         while (-1 != (n = input.read(buffer))) {  
  34.             output.write(buffer, 0, n);  
  35.             count += n;  
  36.         }  
  37.         return count;  
  38.     }  

//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

  1. StringWriter sw = new StringWriter();  
  2. return sw.toString();   //通过StringWriter

3、方法writeStringToFile(File , Content , encoding)

Java代码  收藏代码 "收藏这段代码")

  1. publicstaticvoid writeStringToFile(File file, String data, String encoding) throws IOException {  
  2.         OutputStream out = null;  
  3.         try {  
  4.             out = openOutputStream(file);  
  5.             IOUtils.write(data, out, encoding);  
  6.         } finally {  
  7.             IOUtils.closeQuietly(out);  
  8.         }  
  9.     }     
  10.     publicstatic FileOutputStream openOutputStream(File file) throws IOException {  
  11.         if (file.exists()) {  
  12.             if (file.isDirectory()) {  
  13.                 thrownew IOException("File '" + file + "' exists but is a directory");  
  14.             }  
  15.             if (file.canWrite() == false) {  
  16.                 thrownew IOException("File '" + file + "' cannot be written to");  
  17.             }  
  18.         } else {  
  19.             File parent = file.getParentFile();  
  20.             if (parent != null && parent.exists() == false) {  
  21.                 if (parent.mkdirs() == false) {  
  22.                     thrownew IOException("File '" + file + "' could not be created");  
  23.                 }  
  24.             }  
  25.         }  
  26.         returnnew FileOutputStream(file);  
  27.     }  
  28.     //IOUtils
  29.     publicstaticvoid write(String data, OutputStream output, String encoding)  
  30.             throws IOException {  
  31.         if (data != null) {  
  32.             if (encoding == null) {  
  33.                 write(data, output);  
  34.             } else {  
  35.                 output.write(data.getBytes(encoding));  
  36.             }  
  37.         }  
  38.     }  
  39.     publicstaticvoid write(String data, OutputStream output)  
  40.             throws IOException {  
  41.         if (data != null) {  
  42.             output.write(data.getBytes());  
  43.         }  
  44.     }  

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

Java代码  收藏代码 "收藏这段代码")

  1. publicstaticvoid write(StringBuffer data, Writer output)  
  2.             throws IOException {  
  3.         if (data != null) {  
  4.             output.write(data.toString());  
  5.         }  
  6.     }  
  7.     publicstaticvoid write(StringBuffer data, OutputStream output)  
  8.             throws IOException {  
  9.         if (data != null) {  
  10.             output.write(data.toString().getBytes());  
  11.         }  
  12.     }  

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

 。。


Original url: Access
Created at: 2019-03-15 11:38:40
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论