1.将文本 a,b,aaaa,fdsfdsf,"fdsfdsafdsf,fafas,&&fafas, ",sf//:fdsfa 中的逗号改为用(:ptth)分隔,其中双引号内的逗号不变
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Substitute { public static void parse(File file, File fileOut) { BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader(file)); bw = new BufferedWriter(new FileWriter(fileOut)); String line = ""; String subStr1 = ""; String subStr2 = ""; String lineAfter = ""; while((line=br.readLine()) != null) { line = line.trim(); int index1 = line.indexOf("\""); subStr1 = line.substring(0,index1); int index2 = line.lastIndexOf("\""); subStr2 = line.substring(index2+1, line.length()); subStr1 = subStr1.replace("," ,"(:ptth)"); subStr2 = subStr2.replace("," ,"(:ptth)"); System.out.println(subStr1 + line.substring(index1,index2+1) + subStr2); lineAfter = subStr1 + line.substring(index1,index2+1) + subStr2; bw.write(lineAfter); bw.newLine(); bw.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); bw = null; } catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) { File fileIn = new File("d:\\testSub.txt"); File fileOut = new File("d:\\out.txt"); parse(fileIn, fileOut); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Substitute2 { public static void parse(File file, File fileOut) { BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader(file)); bw = new BufferedWriter(new FileWriter(fileOut)); String line = ""; String subStr1 = ""; String subStr2 = ""; String subStr3 = ""; String subStr4 = ""; String subStr5 = ""; String lineAfter = ""; while((line=br.readLine()) != null) { line = line.trim(); int index1 = line.indexOf("\""); int index2 = line.indexOf("\"", index1+1); int index3 = line.indexOf("\"", index2+1); int index4 = line.indexOf("\"", index3+1); subStr1 = line.substring(0,index1); subStr2 = line.substring(index1, index2+1); subStr3 = line.substring(index2+1, index3); subStr4 = line.substring(index3, index4+1); subStr5 = line.substring(index4+1, line.length()); // subStr1 = subStr1.replace("," ,"(:ptth)"); // subStr2 = subStr2.replace("," ,"(:ptth)"); subStr1 = subStr1.replace("," ,"(:ptth)"); subStr3 = subStr3.replace("," ,"(:ptth)"); subStr5 = subStr5.replace("," ,"(:ptth)"); System.out.println(subStr1 + subStr2 + subStr3 + subStr4 + subStr5); lineAfter = subStr1 + subStr2 + subStr3 + subStr4 + subStr5; bw.write(lineAfter); bw.newLine(); bw.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); bw = null; } catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) { File fileIn = new File("d:\\testSql.txt"); File fileOut = new File("d:\\out3.txt"); parse(fileIn, fileOut); } }