删除文本文件中的较短字符串

时间:2012-09-21 01:08:55

标签: java file

如何删除文本文件中的较短字符串,如下所示:

987987
9879872938472
987234
234987234987
32423

我希望能够删除少于5个字符的字符串。我想知道如何使用Java。

2 个答案:

答案 0 :(得分:1)

String string="987987 9879872938472 987234 234987234987 32423";
    String[] splitString=string.split(" ");

    for(int i=0;i<splitString.length;i++){
        if(splitString[i].length()<5){
            continue;
        }
        //write to file 
    }

答案 1 :(得分:0)

我认为你想要这样的东西。

// to read in the file    
Scanner sc = new Scanner(new File("file"));

// read in each line and check to see if they are greater
// than 5 characters in length
while sc.hasNextLine(){
    String line = sc.nextLine().trim();
    if(line.length() >= 5) {
        System.out.println(line);
        // or code here to output to some file.
    }
}