去除文本中的括号内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class FilterBracket {

public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("输入要处理的文件路径及文件名:");
String proFile = scanner.nextLine();
List<String> list = readFileByLine(proFile);

String finalFileLoc = proFile.substring(0, proFile.lastIndexOf("\\")+1);
String proFileName = proFile.substring(proFile.lastIndexOf("\\")+1);
File fileFile = new File(finalFileLoc+"new"+proFileName.replaceFirst(proFileName.substring(0, 1),proFileName.substring(0, 1).toUpperCase()));
fileFile.createNewFile();

FileWriter fw = new FileWriter(fileFile);
String str="";
for (String s : list) {
str = s;
if(s.indexOf("(")!=-1 && s.indexOf(")")!=-1){
str = s.replace(s.substring(s.indexOf("("),s.lastIndexOf(")")+1), "");
}else if(s.indexOf("(")!=-1 && s.indexOf(")")!=-1){
str = s.replace(s.substring(s.indexOf("("),s.lastIndexOf(")")+1), "");
}else if(s.indexOf("(")!=-1){
str = s.replace(s.substring(s.indexOf("(")), "");
}else if(s.indexOf("(")!=-1){
str = s.replace(s.substring(s.indexOf("(")), "");
}
fw.write(str+System.getProperty("line.separator"));
}

fw.close();

}

public static List<String> readFileByLine(String fileName) throws Exception{
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName),"UTF-8");
BufferedReader br = new BufferedReader(isr);
String s = "";
List<String> existEnterprise = new ArrayList<String>();
while((s=br.readLine())!=null){
existEnterprise.add(s);
}
isr.close();
br.close();
return existEnterprise;
}


}
阅读更多
个人分类: 工具类
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

去除文本中的括号内容

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭