免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2053 | 回复: 2
打印 上一主题 下一主题

java io流 file操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 17:40 |只看该作者 |倒序浏览
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. * 读取文本文件内容
  21. *
  22. * @param filePathAndName
  23. *            带有完整绝对路径的文件名
  24. * @param encoding
  25. *            文本文件打开的编码方式
  26. * @return 返回文本文件的内容
  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 返回目录创建后的路径
  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 = "创建目录操作出错";
  74. }
  75. return txt;
  76. }

  77. /**
  78. * 多级目录创建
  79. *
  80. * @param folderPath
  81. *            准备要在本级目录下创建新目录的目录路径 例如 c:myf
  82. * @param paths
  83. *            无限级目录参数,各级目录以单数线区分 例如 a|b|c
  84. * @return 返回创建文件后的路径 例如 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 = "创建目录操作出错!";
  102. }
  103. return txts;
  104. }

  105. /**
  106. * 新建文件
  107. *
  108. * @param filePathAndName
  109. *            文本文件完整绝对路径及文件名
  110. * @param fileContent
  111. *            文本文件内容
  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 = "创建文件操作出错";
  130. }
  131. }

  132. /**
  133. * 有编码方式的文件创建
  134. *
  135. * @param filePathAndName
  136. *            文本文件完整绝对路径及文件名
  137. * @param fileContent
  138. *            文本文件内容
  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 = "创建文件操作出错";
  158. }
  159. }

  160. /**
  161. * 删除文件
  162. *
  163. * @param filePathAndName
  164. *            文本文件完整绝对路径及文件名
  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 + "删除文件操作出错");
  178. }
  179. } catch (Exception e) {
  180. message = e.toString();
  181. }
  182. return bea;
  183. }

  184. /**
  185. * 删除文件夹
  186. *
  187. * @param folderPath
  188. *            文件夹完整绝对路径
  189. * @return
  190. */
  191. public static void delFolder(String folderPath) {
  192. try {
  193. delAllFile(folderPath); // 删除完里面所有内容
  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 = ("删除文件夹操作出错");
  200. }
  201. }

  202. /**
  203. * 删除指定文件夹下所有文件
  204. *
  205. * @param path
  206. *            文件夹完整绝对路径
  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. * 复制单个文件
  240. *
  241. * @param oldPathFile
  242. *            准备复制的文件源
  243. * @param newPathFile
  244. *            拷贝到新绝对路径带文件名
  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()) { // 文件存在时
  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; // 字节数 文件大小
  258. fs.write(buffer, 0, byteread);
  259. }
  260. inStream.close();
  261. }
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. message = ("复制单个文件操作出错");
  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()) { // 文件存在时
  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; // 字节数 文件大小
  285. fs.write(buffer, 0, byteread);
  286. }
  287. inStream.close();
  288. }
  289. } catch (Exception e) {
  290. e.printStackTrace();
  291. message = ("复制单个文件操作出错");
  292. }
  293. }
  294. /**
  295. * 复制整个文件夹的内容
  296. *
  297. * @param oldPath
  298. *            准备拷贝的目录
  299. * @param newPath
  300. *            指定绝对路径的新目录
  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 = "复制整个文件夹内容操作出错";
  335. }
  336. }

  337. /**
  338. * 移动文件
  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. * 移动目录
  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. * 根据id来确定图片放置路径
  364. *
  365. * @param infoId - 数字编号
  366. * @return 分隔目录
  367. */
  368. public static String getPathById(int infoId) {
  369. return getPathById(infoId+"");
  370. }

  371. /**
  372. * 根据id来确定图片放置路径
  373. *
  374. * @param infoId - 字符编号
  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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-19 20:10 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-03-31 09:06 |只看该作者
收下代码,有空实践一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP