日期:2014-05-20 浏览次数:20930 次
public static String readString(String fileName, Boolean bEn) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); if (bEn) { while (content != null) { content = bf.readLine(); String str = ""; for (int i = 0; i < content.length(); i++) { str += content.charAt(i) + 3; } content = str; if (content == null) { break; } content = str; sb.append(content);// .trim()); } } else { while (content != null) { content = bf.readLine(); String str = ""; for (int i = 0; i < content.length(); i++) { str += content.charAt(i) - 3; } if (content == null) { break; } sb.append(content);// .trim()); } } bf.close(); fileOperation.writeBytes(fileName, sb.toString()); return sb.toString(); } public static int writeBytes(String file, String string) { java.io.ObjectOutputStream out; try { out = new java.io.ObjectOutputStream(new java.io.FileOutputStream( file)); out.writeObject(string); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return -1; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return -2; } return 0; }
while ((content = bf.readLine()) != null) { for (int i = 0; i < content.length(); i++) { char c = content.charAt(i) + 3; sb.append(c); // 既然用了StringBuffer/Builder,就绝对绝对不要str += xxx + xxx + xxx; sb.append(str); } }
------解决方案--------------------
貌似你这里,对换行有遗漏
------解决方案--------------------
你都读到内存上操作,要考虑大文件的时候的性能问题
建议还是读一行写一行,写到一个临时文件,最后再把临时文件改名
感觉你的代码要很多累赘的处理,还有,如果要对字符处理,最好转成字符数组来操作,charAt的性能比较低
public static String readString(String fileName, Boolean bEn) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); if (bEn) { while (content != null) { content = bf.readLine(); //String str = ""; //for (int i = 0; i < content.length(); i++) { // str += content.charAt(i) + 3; //} for (char c : content.toCharArray()) { sb.append(c+3); } //content = str; //if (content == null) { // break; //} //content = str; //sb.append(content);// .trim()); } } else { while (content != null) { content = bf.readLine(); //String str = ""; //for (int i = 0; i < content.length(); i++) { // str += content.charAt(i) - 3; //} for (char c : content.toCharArray()) { sb.append(c-3); } //if (content == null) { // break; //} //sb.append(content);// .trim()); } } bf.close(); fileOperation.writeBytes(fileName, sb.toString()); return sb.toString(); } public static int writeBytes(String file, String string) { java.io.ObjectOutputStream out; try { out = new java.io.ObjectOutputStream(new java.io.FileOutputStream( file)); out.writeObject(string); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return -1; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return -2; } return 0; }