1
public void removeLine() {
        try {
            File dir = new File("chars");
            if(dir.exists()) {
                String read;
                File files[] = dir.listFiles(); 
                for (int j = 0; j < files.length; j++) {
                    File loaded = files[j];
                    if (loaded.getName().endsWith(".txt")) {
                        Scanner s = new Scanner (loaded);
                        while (s.hasNextLine()) {
                            read = s.nextLine();
                            if (read.contains("char-15")) {
                                read.replace(read, "");
                                System.out.println(loaded.getName() +" - Data: "+read);
                                break;                          
                            }                       
                        }                   
                    }           
                }
            }
        } catch (Exception e) {

        }

    }

これが行うべきことは、「char-15」を含む各行を空の文字列に置き換えることです。

ただし、これを実行すると、すべてのファイルの行が削除されません。5000をはるかに超えるファイルがあるため、これを手動で行うことはできません。

すべてのファイルでこの特定の行を削除するにはどうすればよいですか?

4

4 に答える 4

2

を使用してこれを簡単に行うことができますApache Common IO API

ここに完全な作業例があります

import java.io.File;
import java.io.IOException;    
import org.apache.commons.io.FileUtils;

public class FileUpdater {

    public static void main(String[] args) throws IOException {
        File dir = new File("D:\\dummy");
        if (dir.exists() && dir.isDirectory()) {
            File[] listFiles = dir.listFiles();
            for (File file : listFiles) {
                if (file.getName().contains(".txt")) {
                    String fileString = FileUtils.readFileToString(file);
                    String finalString = fileString.replaceAll("char-15", "");
                    FileUtils.writeStringToFile(file, finalString);
                }

            }
        }
    }

}

上記のコードはreplace char-15すべてemptyのファイルに

}

于 2013-09-01T10:36:48.637 に答える
0

最初にファイルを文字列としてロードします。

public static String readAllText(InputStream is) {
    StringBuilder sb = new StringBuilder();
    try {
        Reader r = new InputStreamReader(is);
        int c;
        while ((c = r.read()) != -1) sb.append(char.class.cast(c));
    } catch (Exception e) {
        //Handle Exception
    }
    return sb.toString();
}

次に削除します:

public static String removeUntil(String str, String c, int st)
{
    StringBuilder sb = new StringBuilder(str);
    str = sb.reverse().toString();
    for(int i = 0;i<st;i++)
        str = str.substring(0,str.lastIndexOf(c));
    sb = new StringBuilder(str);
    str = sb.reverse().toString();
    return str;
}

例: removeUntil("I.want.remove.this.until.third.dot",".",3); 結果 -> this.until.third.dot

于 2014-08-25T10:22:47.447 に答える
0

Java 7 では、次のようなことができます。

if (loaded.getName().endsWith(".txt")) {
   Path path = Paths.get(loaded.getName());
   String content = new String(Files.readAllBytes(path));
   content = content.replaceAll("char-15", "");
   Files.write(path, content.getBytes());
}
于 2013-09-01T10:32:23.250 に答える
0

Java sting is immutable and final, whichever operation you made in String, it will create new instance only. So read.replace(read, ""); is not help to replace word in your file.

于 2013-09-01T10:39:21.520 に答える