forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
89 lines (88 loc) · 2.34 KB
/
FileUtil.java
File metadata and controls
89 lines (88 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.dcf.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* <h1>FileUtil文件操作工具类</h1><br>
* <p>提供File处理的相关方法</p>
*
*/
public class FileUtil {
/**
* 此方法将file文件转换成文件流的方式,使用时要注意InputStream的释放
* @param file 传入的文件
* @return InputStream 返回流
* @throws FileNotFoundException 抛出文件找不到的异常
*/
public InputStream fileToInputStream(File file) throws FileNotFoundException{
InputStream is=new FileInputStream(file);
return is;
}
/**
* 将文件转换成二进制数组
* @param file 要转换的文件
* @return byte[] 返回的二进制数组
* @throws IOException 抛出IO读写异常
*/
public byte[] fileToByteArr(File file) throws IOException{
InputStream is=new FileInputStream(file);
byte[] buffer=new byte[is.available()];
is.read(buffer);
releaseInputStream(is);
return buffer;
}
/***
* 将二进制数组写入到指定文件中
* @param arr 二进制byte数组
* @param file 要写入的目标文件
* @return 返回指定文件
* @throws IOException 抛出IO异常
*/
public File byteArrToFile(byte[] arr ,File file) throws IOException{
OutputStream os=new FileOutputStream(file);
os.write(arr);
releaseOutputStream(os);
return file;
}
/**
* 将二进制流写进指定的文件中
* @param is 二进制流
* @param file 指定的文件
* @return 返回指定的文件
* @throws IOException 抛出IO异常
*/
public File inputStreamToFile(InputStream is,File file) throws IOException{
if(is!=null){
byte[] buffer=new byte[is.available()];
OutputStream os=new FileOutputStream(file);
os.write(buffer);
releaseOutputStream(os);
}
return file;
}
/**
* 释放InputStream资源
* @param is 要释放的InputStream
* @throws IOException 抛出IO异常
*/
public void releaseInputStream(InputStream is) throws IOException{
if(is!=null){
is.close();
}
}
/**
* 释放OutputStream资源
* @param os 要释放的Outputstream
* @throws IOException 抛出IO异常
*/
public void releaseOutputStream(OutputStream os) throws IOException{
if(os!=null){
os.close();
}
}
}