简体   繁体   English

Java:如何清除和排序 .txt 文件中的所有内容?

[英]Java : How can I clear and sorting all contents in .txt file?

  1. How can i sort all of my string content in .txt file based on alphabetical?如何根据字母顺序对 .txt 文件中的所有字符串内容进行排序? For example : here's what inside the txt file :例如:这是txt文件中的内容:

    1. Jhon约翰
    2. Anna安娜
    3. Alice爱丽丝

    After sorting :排序后:

    1. Alice爱丽丝
    2. Anna安娜
    3. Jhon约翰
  2. How would i clear all of the contents and leave only the empty txt file?我如何清除所有内容并只留下空的 txt 文件?
public static void main(String[] args){
    try {
        File file = new File("input.txt")
        List<String> allLines = Files.readAllLines(file.toPath(), Charset.defaultCharset());
        Collections.sort(allLines);

        //For deleting
        file.delete();
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

For example例如

File file = new File("filepathname.txt");
file.delete();
file.creatFile();


File myFile = null;文件 myFile = null;
BufferedReader br = null; BufferedReader br = null;
BufferedWriter bw = null; BufferedWriter bw = null;
String [] name = {字符串 [] 名称 = {
"John"," AnNa","1","AliCe", “约翰”、“安娜”、“1”、“爱丽丝”、
"Ammy"," JOey","Alex","0"}; "艾米","乔伊","亚历克斯","0"};

try {试试{
myFile = new File("unsorted strings.txt"); myFile = new File("unsorted strings.txt");
myFile.createNewFile(); myFile.createNewFile();
bw = new BufferedWriter(new FileWriter(myFile)); bw = new BufferedWriter(new FileWriter(myFile));

for ( int i = 0; i < name.length; i++ ){ for ( int i = 0; i < name.length; i++ ){
bw.write(name[i]); bw.write(name[i]);
bw.newLine(); bw.newLine();
} }

bw.close(); bw.close();
br = new BufferedReader(new FileReader(myFile)); br = new BufferedReader(new FileReader(myFile));
String temp = "";字符串温度 = "";
int counter = 0;整数计数器 = 0;

while((temp = br.readLine()) != null){ while((temp = br.readLine()) != null){
//temp = temp.trim(); //temp = temp.trim();
//char fChar = temp.toUpperCase().charAt(0); //char fChar = temp.toUpperCase().charAt(0);
//temp = temp.substring(1,temp.length()).toLowerCase(); //temp = temp.substring(1,temp.length()).toLowerCase();
//name[counter] = fChar + temp; //名称[计数器] = fChar + temp;
//counter++; //计数器++;

name[counter] = temp;名称[计数器] = 温度;
counter++;计数器++;
} }

br.close(); br.close();

//java.util.Arrays.sort(name); //java.util.Arrays.sort(name);

java.util.Arrays.sort(name,new java.util.Comparator< String >(){ java.util.Arrays.sort(name,new java.util.Comparator<String>(){
public int compare(String o1, String o2) {公共 int 比较(字符串 o1,字符串 o2){
return o1.trim().toLowerCase().compareTo(o2.trim().toLowerCase());返回 o1.trim().toLowerCase().compareTo(o2.trim().toLowerCase());
}}); }});


myFile = new File("sorted strings.txt"); myFile = new File("排序字符串.txt");
myFile.createNewFile(); myFile.createNewFile();
bw = new BufferedWriter(new FileWriter(myFile)); bw = new BufferedWriter(new FileWriter(myFile));


for ( int i = 0; i < name.length; i++ ){ for ( int i = 0; i < name.length; i++ ){
bw.write(name[i]); bw.write(name[i]);
bw.newLine(); bw.newLine();

} }


bw.close(); bw.close();


myFile = new File("empty file.txt"); myFile = new File("空文件.txt");
myFile.createNewFile(); myFile.createNewFile();
bw = new BufferedWriter(new FileWriter(myFile)); bw = new BufferedWriter(new FileWriter(myFile));
bw.write(""); bw.write("");
bw.close(); bw.close();

}catch (Exception ex) { }catch(异常前){
ex.printStackTrace(); ex.printStackTrace();
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM