亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2124 | 回復(fù): 2
打印 上一主題 下一主題

java io流 file操作 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-03-19 17:40 |只看該作者 |倒序?yàn)g覽
java io流 file操作
  1. /**
  2. * all rights reserved by ming, 2005
  3. */
  4. package com.koubei.util;

  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.util.StringTokenizer;

  15. public class FileOperate {

  16. static String message;

  17. public FileOperate() {
  18. }

  19. /**
  20. * 讀取文本文件內(nèi)容
  21. *
  22. * @param filePathAndName
  23. *            帶有完整絕對(duì)路徑的文件名
  24. * @param encoding
  25. *            文本文件打開的編碼方式
  26. * @return 返回文本文件的內(nèi)容
  27. */
  28. public static String readTxt(String filePathAndName, String encoding)
  29. throws IOException {
  30. encoding = encoding.trim();
  31. StringBuffer str = new StringBuffer("");
  32. String st = "";
  33. try {
  34. FileInputStream fs = new FileInputStream(filePathAndName);
  35. InputStreamReader isr;
  36. if (encoding.equals("")) {
  37. isr = new InputStreamReader(fs);
  38. } else {
  39. isr = new InputStreamReader(fs, encoding);
  40. }
  41. BufferedReader br = new BufferedReader(isr);
  42. try {
  43. String data = "";
  44. while ((data = br.readLine()) != null) {
  45. str.append(data + " ");
  46. }
  47. } catch (Exception e) {
  48. str.append(e.toString());
  49. }
  50. st = str.toString();
  51. } catch (IOException es) {
  52. st = "";
  53. }
  54. return st;
  55. }

  56. /**
  57. * 新建目錄
  58. *
  59. * @param folderPath
  60. *            目錄
  61. * @return 返回目錄創(chuàng)建后的路徑
  62. */
  63. public static String createFolder(String folderPath) {
  64. String txt = folderPath;
  65. try {
  66. java.io.File myFilePath = new java.io.File(txt);
  67. txt = folderPath;
  68. if (!myFilePath.exists()) {
  69. myFilePath.mkdirs();
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. message = "創(chuàng)建目錄操作出錯(cuò)";
  74. }
  75. return txt;
  76. }

  77. /**
  78. * 多級(jí)目錄創(chuàng)建
  79. *
  80. * @param folderPath
  81. *            準(zhǔn)備要在本級(jí)目錄下創(chuàng)建新目錄的目錄路徑 例如 c:myf
  82. * @param paths
  83. *            無限級(jí)目錄參數(shù),各級(jí)目錄以單數(shù)線區(qū)分 例如 a|b|c
  84. * @return 返回創(chuàng)建文件后的路徑 例如 c:myfac
  85. */
  86. public static String createFolders(String folderPath, String paths) {
  87. String txts = folderPath;
  88. try {
  89. String txt;
  90. txts = folderPath;
  91. StringTokenizer st = new StringTokenizer(paths, "|");
  92. for (int i = 0; st.hasMoreTokens(); i++) {
  93. txt = st.nextToken().trim();
  94. if (txts.lastIndexOf("/") != -1) {
  95. txts = createFolder(txts + txt);
  96. } else {
  97. txts = createFolder(txts + txt + "/");
  98. }
  99. }
  100. } catch (Exception e) {
  101. message = "創(chuàng)建目錄操作出錯(cuò)!";
  102. }
  103. return txts;
  104. }

  105. /**
  106. * 新建文件
  107. *
  108. * @param filePathAndName
  109. *            文本文件完整絕對(duì)路徑及文件名
  110. * @param fileContent
  111. *            文本文件內(nèi)容
  112. * @return
  113. */
  114. public static void createFile(String filePathAndName, String fileContent) {

  115. try {
  116. String filePath = filePathAndName;
  117. filePath = filePath.toString();
  118. File myFilePath = new File(filePath);
  119. if (!myFilePath.exists()) {
  120. myFilePath.createNewFile();
  121. }
  122. FileWriter resultFile = new FileWriter(myFilePath);
  123. PrintWriter myFile = new PrintWriter(resultFile);
  124. String strContent = fileContent;
  125. myFile.println(strContent);
  126. myFile.close();
  127. resultFile.close();
  128. } catch (Exception e) {
  129. message = "創(chuàng)建文件操作出錯(cuò)";
  130. }
  131. }

  132. /**
  133. * 有編碼方式的文件創(chuàng)建
  134. *
  135. * @param filePathAndName
  136. *            文本文件完整絕對(duì)路徑及文件名
  137. * @param fileContent
  138. *            文本文件內(nèi)容
  139. * @param encoding
  140. *            編碼方式 例如 GBK 或者 UTF-8
  141. * @return
  142. */
  143. public static void createFile(String filePathAndName, String fileContent,
  144. String encoding) {

  145. try {
  146. String filePath = filePathAndName;
  147. filePath = filePath.toString();
  148. File myFilePath = new File(filePath);
  149. if (!myFilePath.exists()) {
  150. myFilePath.createNewFile();
  151. }
  152. PrintWriter myFile = new PrintWriter(myFilePath, encoding);
  153. String strContent = fileContent;
  154. myFile.println(strContent);
  155. myFile.close();
  156. } catch (Exception e) {
  157. message = "創(chuàng)建文件操作出錯(cuò)";
  158. }
  159. }

  160. /**
  161. * 刪除文件
  162. *
  163. * @param filePathAndName
  164. *            文本文件完整絕對(duì)路徑及文件名
  165. * @return Boolean 成功刪除返回true遭遇異常返回false
  166. */
  167. public static boolean delFile(String filePathAndName) {
  168. boolean bea = false;
  169. try {
  170. String filePath = filePathAndName;
  171. File myDelFile = new File(filePath);
  172. if (myDelFile.exists()) {
  173. myDelFile.delete();
  174. bea = true;
  175. } else {
  176. bea = false;
  177. message = (filePathAndName + "刪除文件操作出錯(cuò)");
  178. }
  179. } catch (Exception e) {
  180. message = e.toString();
  181. }
  182. return bea;
  183. }

  184. /**
  185. * 刪除文件夾
  186. *
  187. * @param folderPath
  188. *            文件夾完整絕對(duì)路徑
  189. * @return
  190. */
  191. public static void delFolder(String folderPath) {
  192. try {
  193. delAllFile(folderPath); // 刪除完里面所有內(nèi)容
  194. String filePath = folderPath;
  195. filePath = filePath.toString();
  196. java.io.File myFilePath = new java.io.File(filePath);
  197. myFilePath.delete(); // 刪除空文件夾
  198. } catch (Exception e) {
  199. message = ("刪除文件夾操作出錯(cuò)");
  200. }
  201. }

  202. /**
  203. * 刪除指定文件夾下所有文件
  204. *
  205. * @param path
  206. *            文件夾完整絕對(duì)路徑
  207. * @return
  208. * @return
  209. */
  210. public static boolean delAllFile(String path) {
  211. boolean bea = false;
  212. File file = new File(path);
  213. if (!file.exists()) {
  214. return bea;
  215. }
  216. if (!file.isDirectory()) {
  217. return bea;
  218. }
  219. String[] tempList = file.list();
  220. File temp = null;
  221. for (int i = 0; i < tempList.length; i++) {
  222. if (path.endsWith(File.separator)) {
  223. temp = new File(path + tempList[i]);
  224. } else {
  225. temp = new File(path + File.separator + tempList[i]);
  226. }
  227. if (temp.isFile()) {
  228. temp.delete();
  229. }
  230. if (temp.isDirectory()) {
  231. delAllFile(path + "/" + tempList[i]);// 先刪除文件夾里面的文件
  232. delFolder(path + "/" + tempList[i]);// 再刪除空文件夾
  233. bea = true;
  234. }
  235. }
  236. return bea;
  237. }

  238. /**
  239. * 復(fù)制單個(gè)文件
  240. *
  241. * @param oldPathFile
  242. *            準(zhǔn)備復(fù)制的文件源
  243. * @param newPathFile
  244. *            拷貝到新絕對(duì)路徑帶文件名
  245. * @return
  246. */
  247. public static void copyFile(String oldPathFile, String newPathFile) {
  248. try {
  249. int bytesum = 0;
  250. int byteread = 0;
  251. File oldfile = new File(oldPathFile);
  252. if (oldfile.exists()) { // 文件存在時(shí)
  253. InputStream inStream = new FileInputStream(oldPathFile); // 讀入原文件
  254. FileOutputStream fs = new FileOutputStream(newPathFile);
  255. byte[] buffer = new byte[1444];
  256. while ((byteread = inStream.read(buffer)) != -1) {
  257. bytesum += byteread; // 字節(jié)數(shù) 文件大小
  258. fs.write(buffer, 0, byteread);
  259. }
  260. inStream.close();
  261. }
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. message = ("復(fù)制單個(gè)文件操作出錯(cuò)");
  265. }
  266. }
  267. /******
  268. *
  269. * @param oldPathFile
  270. * @param newPathFile
  271. * @param filename
  272. */
  273. public static void copyFile(String oldPathFile, String newPathFile,String filename) {
  274. try {
  275. int bytesum = 0;
  276. int byteread = 0;
  277. File oldfile = new File(oldPathFile);
  278. newPathFile+=filename;
  279. if (oldfile.exists()) { // 文件存在時(shí)
  280. InputStream inStream = new FileInputStream(oldPathFile); // 讀入原文件
  281. FileOutputStream fs = new FileOutputStream(newPathFile);
  282. byte[] buffer = new byte[1444];
  283. while ((byteread = inStream.read(buffer)) != -1) {
  284. bytesum += byteread; // 字節(jié)數(shù) 文件大小
  285. fs.write(buffer, 0, byteread);
  286. }
  287. inStream.close();
  288. }
  289. } catch (Exception e) {
  290. e.printStackTrace();
  291. message = ("復(fù)制單個(gè)文件操作出錯(cuò)");
  292. }
  293. }
  294. /**
  295. * 復(fù)制整個(gè)文件夾的內(nèi)容
  296. *
  297. * @param oldPath
  298. *            準(zhǔn)備拷貝的目錄
  299. * @param newPath
  300. *            指定絕對(duì)路徑的新目錄
  301. * @return
  302. */
  303. public static void copyFolder(String oldPath, String newPath) {
  304. try {
  305. new File(newPath).mkdirs(); // 如果文件夾不存在 則建立新文件夾
  306. File a = new File(oldPath);
  307. String[] file = a.list();
  308. File temp = null;
  309. for (int i = 0; i < file.length; i++) {
  310. if (oldPath.endsWith(File.separator)) {
  311. temp = new File(oldPath + file[i]);
  312. } else {
  313. temp = new File(oldPath + File.separator + file[i]);
  314. }
  315. if (temp.isFile()) {
  316. FileInputStream input = new FileInputStream(temp);
  317. FileOutputStream output = new FileOutputStream(newPath
  318. + "/" + (temp.getName()).toString());
  319. byte[] b = new byte[1024 * 5];
  320. int len;
  321. while ((len = input.read(b)) != -1) {
  322. output.write(b, 0, len);
  323. }
  324. output.flush();
  325. output.close();
  326. input.close();
  327. }
  328. if (temp.isDirectory()) {// 如果是子文件夾
  329. copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  330. }
  331. }
  332. } catch (Exception e) {
  333. e.printStackTrace();
  334. message = "復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)";
  335. }
  336. }

  337. /**
  338. * 移動(dòng)文件
  339. *
  340. * @param oldPath
  341. * @param newPath
  342. * @return
  343. */
  344. public static void moveFile(String oldPath, String newPath) {
  345. copyFile(oldPath, newPath);
  346. delFile(oldPath);
  347. }

  348. /**
  349. * 移動(dòng)目錄
  350. *
  351. * @param oldPath
  352. * @param newPath
  353. * @return
  354. */
  355. public static void moveFolder(String oldPath, String newPath) {
  356. copyFolder(oldPath, newPath);
  357. delFolder(oldPath);
  358. }

  359. public static String getMessage() {
  360. return message;
  361. }

  362. /**
  363. * 根據(jù)id來確定圖片放置路徑
  364. *
  365. * @param infoId - 數(shù)字編號(hào)
  366. * @return 分隔目錄
  367. */
  368. public static String getPathById(int infoId) {
  369. return getPathById(infoId+"");
  370. }

  371. /**
  372. * 根據(jù)id來確定圖片放置路徑
  373. *
  374. * @param infoId - 字符編號(hào)
  375. * @return 分隔目錄
  376. */
  377. public static String getPathById(String infoId) {
  378. StringBuffer path = new StringBuffer();
  379. while (infoId.length() > 1) {
  380. path.append(infoId.substring(0, 2)).append("/");
  381. infoId = infoId.substring(2);
  382. }
  383. if (infoId.length() > 0)
  384. path.append(infoId).append("/");
  385. return path.toString();
  386. }

  387. }
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-03-19 20:10 |只看該作者
謝謝分享

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2012-03-31 09:06 |只看該作者
收下代碼,有空實(shí)踐一下
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP