总的来说,分为三个模块,读文件模块、处理数据、写入文件。(中间有如何创建文件)
public class EG_Reader {
//主方法
public static void main(String[] args) {
String filePath = "C:\\Users\\lenovo\\Desktop\\testRead.txt";//文件路径;
String fileContexts = readerTxtFile(filePath);
String fileContext = dealTxtFile(fileContexts);//删除相同数字;
writeTxtFile(fileContext);
}
//文件写入
private static void writeTxtFile(String content) {
// TODO Auto-generated method stub
//System.out.println(content);
BufferedWriter bw = null;
//以下代码是在D:/testWrite/write下创建名为filename.txt的文件
creatFile();
try {
File file = new File("D:/testWrite/write/filename.txt");//制定路径
//如果没有文件,自动创建文件;
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("succeed");
} catch (IOException e) {
e.printStackTrace();
}
}
以下代码是在D:/testWrite/write下创建名为filename.txt的文件
private static void creatFile() {
// TODO Auto-generated method stub
// 根据系统的实际情况选择目录分隔符(windows下是,linux下是/)
String separator = File.separator;
String directory = "D:" + separator + "testWrite" + separator + "write";
// 以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的
// linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用\来表示反斜杠)
// String directory = "myDir1/myDir2";
String fileName = "filename.txt";
// 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
File f = new File(directory, fileName);
if (f.exists()) {
// 文件已经存在,输出文件的相关信息
// System.out.println(f.getAbsolutePath());
// System.out.println(f.getName());
//System.out.println(f.length());
} else {
// 先创建文件所在的目录
f.getParentFile().mkdirs();
try {
// 创建新文件
f.createNewFile();
} catch (IOException e) {
System.out.println("创建新文件时出现了错误。。。");
e.printStackTrace();
}
}
}
//处理文件,删除相同数字;
private static String dealTxtFile(String fileContext) {
// TODO Auto-generated method stub
//System.out.println(fileContext+"*****");
StringBuffer str = new StringBuffer("");
String[] strs = fileContext.split("\\r\\n");
//String dd = String.join(",", strs);
ArrayList list = new ArrayList();
for(int j=0;j<strs.length;j++) {
if(!list.contains(strs[j])) {
list.add(strs[j]);
}
}
list.sort(null);//排序
for(int i=0;i<list.size();i++) {
//System.out.println(list.get(i));
str.append(list.get(i));
str.append("\r\n");
}
return str.toString();
}
//文件读取
private static String readerTxtFile(String filePath) {
// TODO Auto-generated method stub
StringBuilder content = new StringBuilder("");
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
String[] result = getNamePhone(lineTxt);
content.append(result[0]);
//System.out.println(result[0] + "----" + result[1]);
content.append("\r\n");// txt换行
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return content.toString();
}
//处理文件中每一行字符
private static String[] getNamePhone(String lineTxt) {
// TODO Auto-generated method stub
String[] str = new String[1];
//处理数据类型多的情况下
/*int index = 0;
for(int i=0;i<lineTxt.length();i++) {
if(lineTxt.charAt(i)>='0'&&lineTxt.charAt(i)<='9') {
index =i;
break;
}
}
str[0]=lineTxt.substring(0, index);*/
str[0]=lineTxt;
//System.out.println(lineTxt.toString());
return str;
}
}