JAVA IO修改文本内容
生活随笔
收集整理的這篇文章主要介紹了
JAVA IO修改文本内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
使用JD-GUI反編譯Jar源碼的時候,點擊Save all source,保存為zip包,而包中的源碼每行都多出了注釋/** */ 標記,所以下面的代碼則為去除每行的注釋標記;
如下:
package com.htjc.netpay;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;public class FileTest {/*** * 刪除文本中所有的 \* *\ 注釋標記* @param oldStr * 查找內容 * @param replaceStr * 替換內容 */ public static void replaceTxtBy(File file) {System.out.println("掃描文件:"+file.getPath()+" 所有注釋");String temp = ""; try {FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis,"utf-8"); BufferedReader br = new BufferedReader(isr); StringBuffer buf = new StringBuffer();int b = -1;int a = -1;// 保存該行前面的內容boolean falg =false;while ((temp = br.readLine()) != null) {a = temp.indexOf("/*");if(a!=-1){falg = true;}b = temp.indexOf("*/");if(falg && b!=-1){temp = temp.substring(b+2);falg = false;b = -1;}if(!falg){buf.append(temp).append(System.getProperty("line.separator"));}}br.close();FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos); pw.write(buf.toString().toCharArray());pw.flush(); pw.close(); } catch (IOException e) {e.printStackTrace(); }}public static void scanFile(String path){File f = new File(path);if(f.isDirectory()){File[] files = f.listFiles();for(File fis :files){scanFile(fis.getPath()); }}else{if(f.getName().indexOf(".java")!=-1){replaceTxtBy(f);}}}/*** main方法測試 * @param s * @throws IOException */ public static void main(String[] s) throws IOException { scanFile("D:\\fdfs");} }轉載于:https://my.oschina.net/woter/blog/883512
總結
以上是生活随笔為你收集整理的JAVA IO修改文本内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css - Position定位属性与层
- 下一篇: js 算法排序总结