Question

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();
    }
}

}

Was it helpful?

Solution

Your code works for me, no idea what is the problem on your side, how you are calling it; but you can optimize your code a bit, something as follows (just very quick code, code be made nicer, but to give you an idea):

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) {
           //
        }
    }
}

You can use the filez hashset to print stuff, or write your file at the end of the parsing process as well.. your choice.

OTHER TIPS

If you don't want duplicates in the file, you will need to keep track of the file names you have already written. A HashSet<String> is going for this. But I'm surprised the above code works at all given that you keep opening the file at the top of walkin() and walkin() itself is recursive. You need to rethink your code a bit. Possibly passing the PrintWriter into walkin() as a parameter.

Since you are running the code multiple times ("every time I run this code it add duplicate lines into text file"), so once you finish writing to the file, you read each line and store it in a HashSet<String>. And use another writer to write it to the file.

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

(It is costly as in you have to do more i/o operation)

You need to expand your method into a class that perform this kind of tasks.

You have two main problem you open a writer for each directory and you call the walkin, for things that do not apply to your logic (and open writer again).

You should try to design a class that will be able to create an index for you.

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);

    }

}

Problem Solved (BTW i'm not sure if it is most efficient solution or not ).......

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();
    }

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top