0

I'm using the following code to search specific files in my computer and write the absolute path in a text file. My problem is that every time I run this code it add duplicate lines into text file, i want to add only those lines(file path) which are not written in the text file at that time (no duplicates).. Thank you

public static void walkin(File dir) {
    String pattern = ".mp4";
    try {

        PrintWriter out = new PrintWriter(new BufferedWriter(
                new FileWriter("D:\\nawaaaaaa.txt", true)));
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {
                    walkin(listFile[i]);
                } else if (listFile[i].getName().endsWith(pattern)
                        && listFile[i].isFile()) {
                    System.out.println(listFile[i].getPath());
                    out.write(listFile[i].toString());
                    out.write("\r\n");
                    // out.close();
                } else {
                    walkin(listFile[i]);
                }
            }
        }
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

4

5 回答 5

0

如果您不希望文件中有重复项,则需要跟踪已写入的文件名。AHashSet<String>正在为此而努力。但是我很惊讶上面的代码完全可以工作,因为你一直在顶部打开文件walkin()并且walkin()它本身是递归的。您需要重新考虑一下您的代码。可能将PrintWriterintowalkin()作为参数传递。

于 2013-12-11T07:52:04.387 回答
0

问题已解决(顺便说一句,我不确定它是否是最有效的解决方案)......

public static void main(String[] args) {

    try {
        File dir = new File("D:\\To Do");
        BufferedWriter out = new BufferedWriter(new FileWriter(
                "D:\\path.txt", true));

        walkin(dir, out);
        out.close();
        readfile();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // Replace this with a suitable directory
        // walkin(new File("D:/to Do"));
}

public static void walkin(File dir, BufferedWriter out) throws IOException {
    String pattern = ".mp4";

    // BufferedWriter out = new BufferedWriter(
    // new FileWriter("D:\\path.txt",true));
    File listFile[] = dir.listFiles();
    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].getName().endsWith(pattern)
                    && listFile[i].isFile()) {
                if (filez.add(listFile[i].getPath())) {
                    // System.out.println(listFile[i].getPath());
                    out.write(listFile[i].toString());
                    out.write("\r\n");
                    // System.out.println(filez);

                }
            } else {
                walkin(listFile[i], out);
            }
        }
    }
}

public static void readfile() {

    BufferedReader br = null;
    String str;
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(
                "D:\\duplicate_free.txt"));
        br = new BufferedReader(new FileReader("D:\\path.txt"));
        while ((str = br.readLine()) != null) {
            if (files.contains(str)) {

            } else {
                files.add(str);
            }
        }
        for (String uniq : files) {
            out.write(uniq);
            System.out.println(uniq);
        }
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

于 2013-12-12T16:44:19.367 回答
0

您需要将您的方法扩展为执行此类任务的类。

您有两个主要问题,您为每个目录打开一个编写器,然后将walkin, 用于不适用于您的逻辑的事物(并再次打开编写器)。

您应该尝试设计一个能够为您创建索引的类。

public static void main(String[] args) throws IOException {


    File createTempFile = File.createTempFile("mp4", ".idx");

    FileIndexer fi = new  FileIndexer(createTempFile.getAbsolutePath());

    fi.index("C:\\", "mp4");

    System.out.println(createTempFile);

}


public static class FileIndexer {

    private static final String END_OF_LINE = "\r\n";

    private final String outputPath;
    private final Set<String> index = new HashSet<String>();

    public FileIndexer(String outputPath) {
        this.outputPath = outputPath;
    }

    private boolean isValidPath(String path) {

        return outputPath != null && outputPath.trim().length() > 0; 

    }

    private boolean isValidIndexFile(File file) {

        return file.isFile() && file.canRead() && file.canWrite();

    }

    private void createIndexFile(File file) throws IOException {

        if(file.createNewFile() == false) {
            throw new IOException("Could not create index file");
        }

        this.index.clear();

    }

    private void readIndexFile(File file) throws IOException {

        isValidIndexFile(file);

        index.clear();

        BufferedReader bufferedReader = null;
        try {
             bufferedReader = new BufferedReader(new FileReader(file));

            String line;
            while((line = bufferedReader.readLine()) != null) {
                addToIndex(line);
            }
        } finally {
            if(bufferedReader != null) {
                bufferedReader.close();
            }
        }
    }

    private void addToIndex(String line) {
        index.add(line);
    }

    private PrintWriter openIndex() throws IOException {

        if(isValidPath(outputPath) == false) {
            throw new IOException(String.format("The outputPath is not valid: [%s]",outputPath));
        }

        File indexFile = new File(outputPath);

        if(indexFile.exists()) {
            readIndexFile(indexFile);
        } else {
            createIndexFile(indexFile);
        }

        return new PrintWriter(new BufferedWriter(new FileWriter(this.outputPath, true)));

    }

    public synchronized void index(String pathToIndex, String pattern) throws IOException {

        isValidPath(pathToIndex);

        PrintWriter out = openIndex();

        try {

            File elementToIndex = new File(pathToIndex);
            index(elementToIndex,pathToIndex, out);

        } finally {
            if(out != null) {
                out.close();
            }
        }
    }


    private void index(File elementToIndex, String pattern, PrintWriter out) {


        if(elementToIndex == null) {
            return;
        }


        if(elementToIndex.isDirectory()) {
            for(File file : elementToIndex.listFiles()) {
                index(file,pattern, out);
            }
        }

        if(elementToIndex.isFile() && elementToIndex.getAbsolutePath().endsWith(pattern)) {
            writeToIndex(elementToIndex, out);
        }
    }

    private void writeToIndex(File elementToIndex, PrintWriter out) {

        out.write(elementToIndex.getAbsolutePath());
        out.write(END_OF_LINE);

    }

}
于 2013-12-11T11:01:31.107 回答
0

你的代码对我有用,不知道你那边的问题是什么,你是怎么称呼它的;但是您可以稍微优化您的代码,如下所示(只是非常快速的代码,代码会变得更好,但给您一个想法):

public class SomeTest {

    private static HashSet<String> filez = new  HashSet<String> (); 

    public static void walkin(File dir, PrintWriter out) {
        String pattern = ".mp4";
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].getName().endsWith(pattern) && listFile[i].isFile()) {
                    //System.out.println(listFile[i].getPath());
                    if (filez.add(listFile[i].getPath())) {
                        out.write(listFile[i].toString());
                        out.write("\r\n");
                    }
                } else {
                    walkin(listFile[i], out);
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            File dir = new File("C:\\mydir");
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new FileWriter("D:\\nawaaaaaa.txt", true)));
            walkin(dir, out);
            out.close();
        } catch (IOException e) {
           //
        }
    }
}

您可以使用 filez 哈希集来打印内容,或者在解析过程结束时写入文件......您的选择。

于 2013-12-11T08:03:18.093 回答
0

由于您多次运行代码(“每次运行此代码时,它都会将重复的行添加到文本文件中”),因此一旦完成对文件的写入,您就可以读取每一行并将其存储在HashSet<String>. 并使用另一个作家将其写入文件。

BufferedWriter writer = new BufferedWriter(new FileWriter("filename"));
for (String eachUniqueLine: `Your hash set`) {
    writer.write(eachUniqueLine);
    writer.newLine();
}

(这很昂贵,因为您必须进行更多的 i/o 操作)

于 2013-12-11T08:00:04.900 回答