今天上班接了一个html文件,2000多行js代码有多余的冒号,狠下心替换到400行放弃了,非常累,于是准备用java代码替换一下。
先贴上之前的js文件
再贴上之后的js文件
可以发现是可以拿代码实现的,读取文件,然后用replace方法,再写入文件
public static void main(String[] args) {
String f = "C:\\Users\\zhang\\Desktop\\Email.txt";
File file = new File(f);
String str = read(f);
String replace = str.replace("\"\"","\"");
write(replace,file);
System.out.println(replace);
}
public static String read(String path) {
StringBuffer res = new StringBuffer();
String line = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
while ((line = reader.readLine()) != null) {
if(!line.trim().contains("style=\"\""))
res.append(line + "\n");
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public static boolean write(String cont, File dist) {
try {
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(dist),"UTF-8") );
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
至此,以上效果实现,效率是显而易见的,运行即可,提早下班不是梦!