3 import java.io.*;
4 import java.util.StringTokenizer;
5
6 public class FileOperate {
7 private String message;
8 public FileOperate() {
9 }
10
11 /**
12 * 读取文本文件内容
13 * @param filePathAndName 带有完整绝对路径的文件名
14 * @param encoding 文本文件打开的编码方式
15 * @return 返回文本文件的内容
16 */
17 public String readTxt(String filePathAndName,String encoding) throws IOException{
18 encoding = encoding.trim();
19 StringBuffer str = new StringBuffer("");
20 String st = "";
21 try{
22 FileInputStream fs = new FileInputStream(filePathAndName);
23 InputStreamReader isr;
24 if(encoding.equals("")){
25 isr = new InputStreamReader(fs);
26 }else{
27 isr = new InputStreamReader(fs,encoding);
28 }
29 BufferedReader br = new BufferedReader(isr);
30 try{
31 String data = "";
32 while((data = br.readLine())!=null){
33 str.append(data+" ");
34 }
35 }catch(Exception e){
36 str.append(e.toString());
37 }
38 st = str.toString();
39 }catch(IOException es){
40 st = "";
41 }
42 return st;
43 }
44
45 /**
46 * 新建目录
47 * @param folderPath 目录
48 * @return 返回目录创建后的路径
49 */
50 public String createFolder(String folderPath) {
51 String txt = folderPath;
52 try {
53 java.io.File myFilePath = new java.io.File(txt);
54 txt = folderPath;
55 if (!myFilePath.exists()) {
56 myFilePath.mkdir();
57 }
58 }
59 catch (Exception e) {
60 message = "创建目录操作出错";
61 }
62 return txt;
63 }
64
65 /**
66 * 多级目录创建
67 * @param folderPath 准备要在本级目录下创建新目录的目录路径 例如 c:myf
68 * @param paths 无限级目录参数,各级目录以单数线区分 例如 a|b|c
69 * @return 返回创建文件后的路径 例如 c:myfac
70 */
71 public String createFolders(String folderPath, String paths){
72 String txts = folderPath;
73 try{
74 String txt;
75 txts = folderPath;
76 StringTokenizer st = new StringTokenizer(paths,"|");
77 for(int i=0; st.hasMoreTokens(); i++){
78 txt = st.nextToken().trim();
79 if(txts.lastIndexOf("/")!=-1){
80 txts = createFolder(txts+txt);
81 }else{
82 txts = createFolder(txts+txt+"/");
83 }
84 }
85 }catch(Exception e){
86 message = "创建目录操作出错!";
87 }
88 return txts;
89 }
90
91
92 /**
93 * 新建文件
94 * @param filePathAndName 文本文件完整绝对路径及文件名
95 * @param fileContent 文本文件内容
96 * @return
97 */
98 public void createFile(String filePathAndName, String fileContent) {
99
100 try {
101 String filePath = filePathAndName;
102 filePath = filePath.toString();
103 File myFilePath = new File(filePath);
104 if (!myFilePath.exists()) {
105 myFilePath.createNewFile();
106 }
107 FileWriter resultFile = new FileWriter(myFilePath);
108 PrintWriter myFile = new PrintWriter(resultFile);
109 String strContent = fileContent;
110 myFile.println(strContent);
111 myFile.close();
112 resultFile.close();
113 }
114 catch (Exception e) {
115 message = "创建文件操作出错";
116 }
117 }
118
119
120 /**
121 * 有编码方式的文件创建
122 * @param filePathAndName 文本文件完整绝对路径及文件名
123 * @param fileContent 文本文件内容
124 * @param encoding 编码方式 例如 GBK 或者 UTF-8
125 * @return
126 */
127 public void createFile(String filePathAndName, String fileContent, String encoding) {
128
129 try {
130 String filePath = filePathAndName;
131 filePath = filePath.toString();
132 File myFilePath = new File(filePath);
133 if (!myFilePath.exists()) {
134 myFilePath.createNewFile();
135 }
136 PrintWriter myFile = new PrintWriter(myFilePath,encoding);
137 String strContent = fileContent;
138 myFile.println(strContent);
139 myFile.close();
140 }
141 catch (Exception e) {
142 message = "创建文件操作出错";
143 }
144 }
145
146
147 /**
148 * 删除文件
149 * @param filePathAndName 文本文件完整绝对路径及文件名
150 * @return Boolean 成功删除返回true遭遇异常返回false
151 */
152 public boolean delFile(String filePathAndName) {
153 boolean bea = false;
154 try {
155 String filePath = filePathAndName;
156 File myDelFile = new File(filePath);
157 if(myDelFile.exists()){
158 myDelFile.delete();
159 bea = true;
160 }else{
161 bea = false;
162 message = (filePathAndName+"删除文件操作出错");
163 }
164 }
165 catch (Exception e) {
166 message = e.toString();
167 }
168 return bea;
169 }
170
171
172
173 /**
174 * 删除文件夹
175 * @param folderPath 文件夹完整绝对路径
176 * @return
177 */
178 public void delFolder(String folderPath) {
179 try {
180 delAllFile(folderPath); //删除完里面所有内容
181 String filePath = folderPath;
182 filePath = filePath.toString();
183 java.io.File myFilePath = new java.io.File(filePath);
184 myFilePath.delete(); //删除空文件夹
185 }
186 catch (Exception e) {
187 message = ("删除文件夹操作出错");
188 }
189 }
190
191
192 /**
193 * 删除指定文件夹下所有文件
194 * @param path 文件夹完整绝对路径
195 * @return
196 * @return
197 */
198 public boolean delAllFile(String path) {
199 boolean bea = false;
200 File file = new File(path);
201 if (!file.exists()) {
202 return bea;
203 }
204 if (!file.isDirectory()) {
205 return bea;
206 }
207 String[] tempList = file.list();
208 File temp = null;
209 for (int i = 0; i < tempList.length; i++) {
210 if (path.endsWith(File.separator)) {
211 temp = new File(path + tempList[i]);
212 }else{
213 temp = new File(path + File.separator + tempList[i]);
214 }
215 if (temp.isFile()) {
216 temp.delete();
217 }
218 if (temp.isDirectory()) {
219 delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
220 delFolder(path+"/"+ tempList[i]);//再删除空文件夹
221 bea = true;
222 }
223 }
224 return bea;
225 }
226
227
228 /**
229 * 复制单个文件
230 * @param oldPathFile 准备复制的文件源
231 * @param newPathFile 拷贝到新绝对路径带文件名
232 * @return
233 */
234 public void copyFile(String oldPathFile, String newPathFile) {
235 try {
236 int bytesum = 0;
237 int byteread = 0;
238 File oldfile = new File(oldPathFile);
239 if (oldfile.exists()) { //文件存在时
240 InputStream inStream = new FileInputStream(oldPathFile); //读入原文件
241 FileOutputStream fs = new FileOutputStream(newPathFile);
242 byte[] buffer = new byte[1444];
243 while((byteread = inStream.read(buffer)) != -1){
244 bytesum += byteread; //字节数 文件大小
245 System.out.println(bytesum);
246 fs.write(buffer, 0, byteread);
247 }
248 inStream.close();
249 }
250 }catch (Exception e) {
251 message = ("复制单个文件操作出错");
252 }
253 }
254
255
256 /**
257 * 复制整个文件夹的内容
258 * @param oldPath 准备拷贝的目录
259 * @param newPath 指定绝对路径的新目录
260 * @return
261 */
262 public void copyFolder(String oldPath, String newPath) {
263 try {
264 new File(newPath).mkdirs(); //如果文件夹不存在 则建立新文件夹
265 File a=new File(oldPath);
266 String[] file=a.list();
267 File temp=null;
268 for (int i = 0; i < file.length; i++) {
269 if(oldPath.endsWith(File.separator)){
270 temp=new File(oldPath+file[i]);
271 }else{
272 temp=new File(oldPath+File.separator+file[i]);
273 }
274 if(temp.isFile()){
275 FileInputStream input = new FileInputStream(temp);
276 FileOutputStream output = new FileOutputStream(newPath + "/" +
277 (temp.getName()).toString());
278 byte[] b = new byte[1024 * 5];
279 int len;
280 while ((len = input.read(b)) != -1) {
281 output.write(b, 0, len);
282 }
283 output.flush();
284 output.close();
285 input.close();
286 }
287 if(temp.isDirectory()){//如果是子文件夹
288 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
289 }
290 }
291 }catch (Exception e) {
292 message = "复制整个文件夹内容操作出错";
293 }
294 }
295
296
297 /**
298 * 移动文件
299 * @param oldPath
300 * @param newPath
301 * @return
302 */
303 public void moveFile(String oldPath, String newPath) {
304 copyFile(oldPath, newPath);
305 delFile(oldPath);
306 }
307
308
309 /**
310 * 移动目录
311 * @param oldPath
312 * @param newPath
313 * @return
314 */
315 public void moveFolder(String oldPath, String newPath) {
316 copyFolder(oldPath, newPath);
317 delFolder(oldPath);
318 }
319 public String getMessage(){
320 return this.message;
321 }
322 }
IO 写的文件操作类
最新推荐文章于 2023-10-09 19:42:24 发布