java 读写文件-java读写文件

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import org.apache.commons.io.FileUtils;
  10. /**
  11. * 文件工具类
  12. */
  13. public class FileUtil {
  14. /**
  15. * 创建文件(文件存在,则删除)
  16. *
  17. * @param fileName
  18. * @return
  19. */
  20. public static File makeNewFile(String fileName) {
  21. try {
  22. File file = new File(fileName);
  23. if (file.exists()) {
  24. //文件存在,删除
  25. file.delete();
  26. } else {
  27. //文件不存在,先创建路径
  28. String filePath = fileName.substring(0, fileName.lastIndexOf(System.getProperty("file.separator")));
  29. file = new File(filePath);
  30. if (!file.exists()) {
  31. file.mkdirs();
  32. }
  33. }
  34. //创建文件
  35. file = new File(fileName);
  36. if (!file.exists()) {
  37. file.createNewFile();
  38. }
  39. return file;
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. return null;
  43. }
  44. }
  45. /**
  46. * 创建文件(文件存在,则删除)
  47. *
  48. * @param fileName
  49. * @param content
  50. * @return
  51. */
  52. public static int makeNewFile(String fileName, String content) {
  53. try {
  54. File file = new File(fileName);
  55. if (file.exists()) {
  56. //文件存在,删除
  57. file.delete();
  58. } else {
  59. //文件不存在,先创建路径
  60. String filePath = fileName.substring(0, fileName.lastIndexOf(System.getProperty("file.separator")));
  61. file = new File(filePath);
  62. if (!file.exists()) {
  63. file.mkdirs();
  64. }
  65. }
  66. //创建文件
  67. file = new File(fileName);
  68. if (!file.exists()) {
  69. file.createNewFile();
  70. }
  71. FileUtils.writeStringToFile(file, content, "UTF-8");
  72. return 0;
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. return -1;
  76. }
  77. }
  78. /**
  79. * 文件追加内容
  80. *
  81. * @param fileName
  82. * @param content
  83. * @return
  84. */
  85. public static int fileAppend(String fileName, String content) {
  86. BufferedWriter writer = null;
  87. try {
  88. //创建路径
  89. String filePath = fileName.substring(0, fileName.lastIndexOf(System.getProperty("file.separator")));
  90. File file = new File(filePath);
  91. if (!file.exists()) {
  92. file.mkdirs();
  93. }
  94. //创建文件
  95. file = new File(fileName);
  96. if (!file.exists()) {
  97. file.createNewFile();
  98. }
  99. //追加日志
  100. writer = new BufferedWriter(new OutputStreamWriter(
  101. new FileOutputStream(file, true), "UTF-8"));
  102. if (file.length() != 0) {//空文件
  103. writer.write("\r\n");
  104. }
  105. writer.write(content);
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. return -1;
  109. } finally {
  110. if (writer != null) {
  111. try {
  112. writer.close();
  113. } catch (IOException ex) {
  114. ex.printStackTrace();
  115. }
  116. }
  117. }
  118. return 0;
  119. }
  120. /**
  121. * 文件追加内容
  122. *
  123. * @param fileName
  124. * @param content
  125. * @return
  126. */
  127. public static int fileAppendGBK(String fileName, String content) {
  128. BufferedWriter writer = null;
  129. try {
  130. //创建路径
  131. String filePath = fileName.substring(0, fileName.lastIndexOf(System.getProperty("file.separator")));
  132. File file = new File(filePath);
  133. if (!file.exists()) {
  134. file.mkdirs();
  135. }
  136. //创建文件
  137. file = new File(fileName);
  138. if (!file.exists()) {
  139. file.createNewFile();
  140. }
  141. //追加日志
  142. writer = new BufferedWriter(new OutputStreamWriter(
  143. new FileOutputStream(file, true), "GBK"));
  144. if (file.length() != 0) {//空文件
  145. writer.write("\r\n");
  146. }
  147. writer.write(content);
  148. } catch (IOException e) {
  149. e.printStackTrace();
  150. return -1;
  151. } finally {
  152. if (writer != null) {
  153. try {
  154. writer.close();
  155. } catch (IOException ex) {
  156. ex.printStackTrace();
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. /**
  163. * 把串内容写到文件指定行上,如果插入行与该行内容相同则覆盖,不同则插入到该行<br>
  164. * 如果插入行号<=0,则在文件最后追加一行
  165. *
  166. * @param fileName 文件名称,全路径
  167. * @param content 插入的行内容
  168. * @param insertLineNum 要插入到文件的行号
  169. * @return
  170. */
  171. public static int insertAssignedLineWithCompare(String fileName, String content,
  172. int insertLineNum) {
  173. BufferedWriter writer = null;
  174. BufferedReader reader = null;
  175. try {
  176. // 创建路径
  177. String filePath = fileName.substring(0, fileName.lastIndexOf(System
  178. .getProperty("file.separator")));
  179. File file = new File(filePath);
  180. if (!file.exists()) {
  181. file.mkdirs();
  182. }
  183. file = new File(fileName);
  184. if (!file.exists()) {
  185. // 文件不存在,新建文件,直接写入内容为一行
  186. file.createNewFile();
  187. writer = new BufferedWriter(new OutputStreamWriter(
  188. new FileOutputStream(file, true), "UTF-8"));
  189. writer.write(content);
  190. } else {
  191. // 文件已存在
  192. if (insertLineNum <= 0) {
  193. // 把内容追加到文件最后
  194. writer = new BufferedWriter(new OutputStreamWriter(
  195. new FileOutputStream(file, true), "UTF-8"));
  196. if (file.length() != 0) {// 非空文件
  197. writer.write("\r\n");//换行
  198. }
  199. writer.write(content);
  200. } else {
  201. // 把内容插入到指定行
  202. reader = new BufferedReader(new InputStreamReader(
  203. new FileInputStream(file), "UTF-8"));
  204. StringBuilder front = new StringBuilder();// 第insertLineNum行前面的内容
  205. StringBuilder back = new StringBuilder();// 第insertLineNum行后面的内容
  206. String insertLineStr = "";// 第insertLineNum行的内容
  207. String currentLineStr = reader.readLine();// 每一行内容
  208. int currentLineNum = 0;// 当前行号
  209. while (currentLineStr != null && !"".equals(currentLineStr)) {
  210. currentLineNum++;
  211. if (currentLineNum < insertLineNum) {
  212. front.append(currentLineStr).append("\r\n");
  213. } else if (currentLineNum == insertLineNum) {
  214. insertLineStr = currentLineStr;
  215. } else {
  216. back.append(currentLineStr).append("\r\n");
  217. }
  218. currentLineStr = reader.readLine();
  219. }
  220. //currentLineNum目前为文件最大行号
  221. if (currentLineNum < insertLineNum) {
  222. // 写入行号大于文件最大行号,把内容追加到文件最后
  223. front.append(content);
  224. } else {
  225. // 把内容追加到文件指定行
  226. if (content.indexOf(insertLineStr) < 0) {
  227. // 没有插入过此行,插入
  228. front.append(content).append("\r\n").append(insertLineStr);
  229. if (back.toString() != null && !"".equals(back.toString())) {
  230. front.append("\r\n").append(back.substring(0, back.lastIndexOf("\r\n")));
  231. }
  232. } else {
  233. // 插入过此行,覆盖
  234. front.append(content);
  235. if (back.toString() != null && !"".equals(back.toString())) {
  236. front.append("\r\n").append(back.substring(0, back.lastIndexOf("\r\n")));
  237. }
  238. }
  239. }
  240. // 重新创建文件
  241. try {
  242. reader.close();
  243. } catch (IOException ex) {
  244. ex.printStackTrace();
  245. return -1;
  246. }
  247. file.delete();
  248. file.createNewFile();
  249. writer = new BufferedWriter(new OutputStreamWriter(
  250. new FileOutputStream(file, true), "UTF-8"));
  251. writer.write(front.toString());
  252. }
  253. }
  254. } catch (Exception e) {
  255. e.printStackTrace();
  256. return -2;
  257. } finally {
  258. if (writer != null) {
  259. try {
  260. writer.close();
  261. } catch (IOException ex) {
  262. ex.printStackTrace();
  263. }
  264. }
  265. }
  266. return 0;
  267. }
  268. /**
  269. * 获取文件
  270. *
  271. * @param filePath
  272. * @return
  273. */
  274. public static File getFile(String filePath) {
  275. try {
  276. File file = new File(filePath);
  277. if (!file.exists()) {
  278. file.getParentFile().mkdirs();
  279. file.createNewFile();
  280. }
  281. return file;
  282. } catch (Exception ex) {
  283. ex.printStackTrace();
  284. return null;
  285. }
  286. }
  287. /**
  288. * 读取文件内容
  289. *
  290. * @param file
  291. * @return
  292. */
  293. public static String getFileContent(File file) {
  294. StringBuilder fileContent = new StringBuilder();
  295. try {
  296. if (file.isFile() && file.exists()) {
  297. InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
  298. BufferedReader reader = new BufferedReader(read);
  299. String line;
  300. while ((line = reader.readLine()) != null) {
  301. fileContent.append(line);
  302. }
  303. read.close();
  304. }
  305. } catch (Exception ex) {
  306. ex.printStackTrace();
  307. }
  308. return fileContent.toString();
  309. }
  310. }

java 读写文件

推荐阅读