java文件操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值