相关类
IOUtils:输入输出流操作
public class IOUtils {
*********************
静态方法
****************
使用缓冲读取数据
public static InputStream toBufferedInputStream(InputStream input) throws IOException {
public static InputStream toBufferedInputStream(InputStream input, int size) throws IOException {
public static BufferedInputStream buffer(InputStream inputStream) {
public static BufferedInputStream buffer(InputStream inputStream, int size) {
public static BufferedReader toBufferedReader(Reader reader) {
public static BufferedReader toBufferedReader(Reader reader, int size) {
public static BufferedReader buffer(Reader reader) {
public static BufferedReader buffer(Reader reader, int size) {
****************
使用缓冲输出数据
public static BufferedWriter buffer(Writer writer) {
public static BufferedWriter buffer(Writer writer, int size) {
public static BufferedOutputStream buffer(OutputStream outputStream) {
public static BufferedOutputStream buffer(OutputStream outputStream, int size) {
***************
将数据读入内存
public static byte[] toByteArray(InputStream input) throws IOException {
public static byte[] toByteArray(InputStream input, long size) throws IOException {
public static byte[] toByteArray(InputStream input, int size) throws IOException {
public static byte[] toByteArray(Reader input, Charset encoding) throws IOException {
public static byte[] toByteArray(Reader input, String encoding) throws IOException {
public static byte[] toByteArray(URI uri) throws IOException {
public static byte[] toByteArray(URL url) throws IOException {
public static byte[] toByteArray(URLConnection urlConn) throws IOException {
****************
将数据转换为字符数组
public static char[] toCharArray(InputStream is, Charset encoding) throws IOException {
public static char[] toCharArray(InputStream is, String encoding) throws IOException {
public static char[] toCharArray(Reader input) throws IOException {
*******************
将数据转换为字符串
public static String toString(InputStream input, Charset encoding) throws IOException {
public static String toString(InputStream input, String encoding) throws IOException {
public static String toString(Reader input) throws IOException {
public static String toString(URI uri, Charset encoding) throws IOException {
public static String toString(URI uri, String encoding) throws IOException {
public static String toString(URL url, Charset encoding) throws IOException {
public static String toString(URL url, String encoding) throws IOException {
public static String toString(byte[] input, String encoding) throws IOException {
****************
resource转换为string、byte[]、URL
public static String resourceToString(String name, Charset encoding) throws IOException {
public static String resourceToString(String name, Charset encoding, ClassLoader classLoader) throws IOException {
public static byte[] resourceToByteArray(String name) throws IOException {
public static byte[] resourceToByteArray(String name, ClassLoader classLoader) throws IOException {
public static URL resourceToURL(String name) throws IOException {
public static URL resourceToURL(String name, ClassLoader classLoader) throws IOException {
*******************
读取行数据转换为字符串
public static List readLines(InputStream input, Charset encoding) throws IOException {
public static List readLines(InputStream input, String encoding) throws IOException {
public static List readLines(Reader input) throws IOException {
public static LineIterator lineIterator(Reader reader) {
public static LineIterator lineIterator(InputStream input, Charset encoding) throws IOException {
public static LineIterator lineIterator(InputStream input, String encoding) throws IOException {
*******************
转换为输入流
public static InputStream toInputStream(CharSequence input, Charset encoding) {
public static InputStream toInputStream(CharSequence input, String encoding) throws IOException {
public static InputStream toInputStream(String input, Charset encoding) {
public static InputStream toInputStream(String input, String encoding) throws IOException {
******************
将数据写入到输出流
public static void write(byte[] data, OutputStream output) throws IOException {
public static void writeChunked(byte[] data, OutputStream output) throws IOException {
public static void write(byte[] data, Writer output, Charset encoding) throws IOException {
public static void write(byte[] data, Writer output, String encoding) throws IOException {
public static void write(char[] data, Writer output) throws IOException {
public static void writeChunked(char[] data, Writer output) throws IOException {
public static void write(char[] data, OutputStream output, Charset encoding) throws IOException {
public static void write(char[] data, OutputStream output, String encoding) throws IOException {
public static void write(CharSequence data, Writer output) throws IOException {
public static void write(CharSequence data, OutputStream output, Charset encoding) throws IOException {
public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {
public static void write(String data, Writer output) throws IOException {
public static void write(String data, OutputStream output, Charset encoding) throws IOException {
public static void write(String data, OutputStream output, String encoding) throws IOException {
public static void writeLines(Collection> lines, String lineEnding, OutputStream output, Charset encoding) throws IOException {
public static void writeLines(Collection> lines, String lineEnding, OutputStream output, String encoding) throws IOException {
public static void writeLines(Collection> lines, String lineEnding, Writer writer) throws IOException {
***********************
输入流复制到输出流
public static int copy(InputStream input, OutputStream output) throws IOException {
public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length) throws IOException {
public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length, byte[] buffer) throws IOException {
public static void copy(InputStream input, Writer output, Charset inputEncoding) throws IOException {
public static void copy(InputStream input, Writer output, String inputEncoding) throws IOException {
public static int copy(Reader input, Writer output) throws IOException {
public static long copyLarge(Reader input, Writer output) throws IOException {
public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException {
public static long copyLarge(Reader input, Writer output, long inputOffset, long length) throws IOException {
public static long copyLarge(Reader input, Writer output, long inputOffset, long length, char[] buffer) throws IOException {
public static void copy(Reader input, OutputStream output, Charset outputEncoding) throws IOException {
public static void copy(Reader input, OutputStream output, String outputEncoding) throws IOException {
******************
比较流是否相等
public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IOException {
******************
跳过流
public static long skip(InputStream input, long toSkip) throws IOException {
public static long skip(ReadableByteChannel input, long toSkip) throws IOException {
public static long skip(Reader input, long toSkip) throws IOException {
public static void skipFully(InputStream input, long toSkip) throws IOException {
public static void skipFully(ReadableByteChannel input, long toSkip) throws IOException {
public static void skipFully(Reader input, long toSkip) throws IOException {
********************
读取流数据
public static int read(Reader input, char[] buffer, int offset, int length) throws IOException {
public static int read(Reader input, char[] buffer) throws IOException {
public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException {
public static int read(InputStream input, byte[] buffer) throws IOException {
public static int read(ReadableByteChannel input, ByteBuffer buffer) throws IOException {
public static void readFully(Reader input, char[] buffer, int offset, int length) throws IOException {
public static void readFully(Reader input, char[] buffer) throws IOException {
public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException {
public static void readFully(InputStream input, byte[] buffer) throws IOException {
public static byte[] readFully(InputStream input, int length) throws IOException {
public static void readFully(ReadableByteChannel input, ByteBuffer buffer) throws IOException {
FileUtils:文件操作
public class FileUtils {
*******************
静态方法
**************
获取文件、目录
public static File getFile(File directory, String... names) {
public static File getFile(String... names) {
public static String getTempDirectoryPath() {
public static File getTempDirectory() {
public static String getUserDirectoryPath() {
public static File getUserDirectory() {
***************
获取文件输入流
public static FileInputStream openInputStream(File file) throws IOException {
public static FileOutputStream openOutputStream(File file) throws IOException {
public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
****************
列出文件、目录
public static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
public static Collection listFiles(File directory, String[] extensions, boolean recursive) {
public static Collection listFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
public static Iterator iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive) {
public static Iterator iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
*******************
判断文件是否相等
public static boolean contentEquals(File file1, File file2) throws IOException {
public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) throws IOException {
******************
url、文件转换
public static File toFile(URL url) {
public static File[] toFiles(URL[] urls) {
public static URL[] toURLs(File[] files) throws IOException {
static String decodeUrl(String url) {
****************
文件、目录复制操作
public static void copyFile(File srcFile, File destFile) throws IOException {
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
public static long copyFile(File input, OutputStream output) throws IOException {
public static void copyFileToDirectory(File srcFile, File destDir) throws IOException {
public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) throws IOException {
public static void copyDirectory(File srcDir, File destDir) throws IOException {
public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException {
public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException {
public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException {
public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException {
public static void copyToDirectory(File src, File destDir) throws IOException {
public static void copyToDirectory(Iterable srcs, File destDir) throws IOException {
*****************
url、字节流、文件操作
public static void copyURLToFile(URL source, File destination) throws IOException {
public static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout) throws IOException {
public static void copyInputStreamToFile(InputStream source, File destination) throws IOException {
public static void copyToFile(InputStream source, File destination) throws IOException {
******************
删除目录
public static void deleteDirectory(File directory) throws IOException {
public static boolean deleteQuietly(File file) {
******************
目录判断
public static boolean directoryContains(File directory, File child) throws IOException {
public static void cleanDirectory(File directory) throws IOException {
*******************
读取文件操作
public static String readFileToString(File file, Charset encoding) throws IOException {
public static String readFileToString(File file, String encoding) throws IOException {
public static byte[] readFileToByteArray(File file) throws IOException {
public static List readLines(File file, Charset encoding) throws IOException {
public static List readLines(File file, String encoding) throws IOException {
public static LineIterator lineIterator(File file, String encoding) throws IOException {
public static LineIterator lineIterator(File file) throws IOException {
***************
输出到文件
public static void writeStringToFile(File file, String data, Charset encoding) throws IOException {
public static void writeStringToFile(File file, String data, String encoding) throws IOException {
public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException {
public static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException {
public static void write(File file, CharSequence data, Charset encoding) throws IOException {
public static void write(File file, CharSequence data, String encoding) throws IOException {
public static void write(File file, CharSequence data, Charset encoding, boolean append) throws IOException {
public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
public static void writeByteArrayToFile(File file, byte[] data) throws IOException {
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
public static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws IOException {
public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException {
public static void writeLines(File file, String encoding, Collection> lines) throws IOException {
public static void writeLines(File file, String encoding, Collection> lines, boolean append) throws IOException {
public static void writeLines(File file, Collection> lines) throws IOException {
public static void writeLines(File file, Collection> lines, boolean append) throws IOException {
public static void writeLines(File file, String encoding, Collection> lines, String lineEnding) throws IOException {
public static void writeLines(File file, String encoding, Collection> lines, String lineEnding, boolean append) throws IOException {
public static void writeLines(File file, Collection> lines, String lineEnding) throws IOException {
public static void writeLines(File file, Collection> lines, String lineEnding, boolean append) throws IOException {
****************
强制删除创建文件、目录
public static void forceDelete(File file) throws IOException {
public static void forceDeleteOnExit(File file) throws IOException {
public static void forceMkdir(File directory) throws IOException {
public static void forceMkdirParent(File file) throws IOException {
****************
移动文件目录
public static void moveDirectory(File srcDir, File destDir) throws IOException {
public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException {
public static void moveFile(File srcFile, File destFile) throws IOException {
public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException {
public static void moveToDirectory(File src, File destDir, boolean createDestDir) throws IOException {
****************
文件目录大小
public static long sizeOf(File file) {
public static BigInteger sizeOfAsBigInteger(File file) {
public static long sizeOfDirectory(File directory) {
public static BigInteger sizeOfDirectoryAsBigInteger(File directory) {
*****************
文件新旧
public static boolean isFileNewer(File file, File reference) {
public static boolean isFileNewer(File file, Date date) {
public static boolean isFileNewer(File file, long timeMillis) {
public static boolean isFileOlder(File file, File reference) {
public static boolean isFileOlder(File file, Date date) {
public static boolean isFileOlder(File file, long timeMillis) {
******************
其他方法
public static void touch(File file) throws IOException {
public static File[] convertFileCollectionToFileArray(Collection files) {
public static boolean waitFor(File file, int seconds) {
public static String byteCountToDisplaySize(BigInteger size) {
public static String byteCountToDisplaySize(long size) {
public static long checksumCRC32(File file) throws IOException {
public static Checksum checksum(File file, Checksum checksum) throws IOException {
示例
*********************
controller 层
@RestController
public class HelloController {
@Value("classpath:/static/file/hello.txt")
private Resource resource;
@Value("classpath:/static/file/hello.txt")
private File file;
@Value("http://www.baidu.com")
private Resource resource2;
@Value("http://www.baidu.com")
private URL url;
@RequestMapping("/hello2")
public String hello2() throws Exception{
System.out.println("hello2 输出:");
System.out.println(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8)+"\n");
return "success 2";
}
@RequestMapping("/hello3")
public String hello3() throws Exception{
System.out.println("hello3 输出:");
System.out.println(FileUtils.readFileToString(file,StandardCharsets.UTF_8)+"\n");
return "success 3";
}
@RequestMapping("/hello4")
public String hello4() throws Exception{
System.out.println("hello4 输出:");
System.out.println(IOUtils.toString(resource2.getInputStream(), StandardCharsets.UTF_8));
System.out.println(IOUtils.toString(url, StandardCharsets.UTF_8));
return "success 4";
}
}
*************************
控制台输出
hello2 输出:
hello world
hello world 2
hello world 3
hello world 4
hello3 输出:
hello world
hello world 2
hello world 3
hello world 4
hello4 输出:
百度一下,你就知道
百度一下,你就知道