-3

How do I print out every text file in a particular directory with BufferedReader? Because I have a method to create file in a particular directory, and at times I want to read out every text file I've created in that directory to know what I have created.

Winter Leong
  • 95
  • 1
  • 1
  • 7
  • 7
    Ok.start coding.Good luck :) – Suresh Atta Jul 13 '13 at 06:30
  • We won't create OP code for you... Get to work. – Dummy Code Jul 13 '13 at 06:33
  • 1
    You need to try something first and then provide what you tried. Please refer to the [about](http://stackoverflow.com/about) page for information about how to ask a question on this forum. Good luck. – Qben Jul 13 '13 at 06:36
  • chill guys, I've already did some on my own, but I don't know why it won't work. I'm just asking for some examples..I'm sorry if I didn't provide my code. – Winter Leong Jul 13 '13 at 07:42

4 Answers4

1

first list all files

public File[] listf(String directoryName) {

// .............list file
File directory = new File(directoryName);

// get all the files from a directory
File[] fList = directory.listFiles();

for (File file : fList) {
    if (file.isFile()) {
        System.out.println(file.getAbsolutePath());
    } else if (file.isDirectory()) {
        listf(file.getAbsolutePath());
    }
}
System.out.println(fList);
return fList;
}      

and after that pass that list into the print(File[]) function

in print function you must print each file of list

Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54
1

I hope this code to help you:

    // Directory path here
    String path = ".";

    String files;
    File folder = new File(path);
    // Returns an array of the files in the directory denoted.
    File[] listOfFiles = folder.listFiles();


    for (int i = 0; i < listOfFiles.length; i++) {

        if (listOfFiles[i].isFile()) {

            //Checks if the type of the file is a text file.
            files = listOfFiles[i].getName();
            if (files.endsWith(".txt") || files.endsWith(".TXT")) {

                // Reads the file and show every line on the screen.
                File file = listOfFiles[i];
                BufferedReader reader;
                try {
                    reader = new BufferedReader(new FileReader(
                            file.getAbsolutePath()));

                    String line = null;
                    while ((line = reader.readLine()) != null) {

                        System.out.println(line);

                    }
                } catch (IOException e) {

                }
            }
        }
    }
amatellanes
  • 3,645
  • 2
  • 17
  • 19
0

1) First Google your self for solution after that try yourself to write something and test it ... still you have any issues come to Stackoverflow to Write a question Try this one.. Not tested but it helps you i think

 BufferedReader listReader = new BufferedReader(
                new FileReader("c:/File_list.dat"));
        String fileName;
        while((fileName = listReader.readLine()) != null) {
            BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
            String line;
            while((line = fileReader.readLine()) != null) {
                System.out.println(line);
            }
            fileReader.close();
        }
        listReader.close();
Devendar
  • 323
  • 1
  • 4
  • 20
0

Do you have a list of names of the files you want to read, or do you want ever last file in a folder that is readable to be read, you say "I want to read out every text file I've created in that directory to know what I have created." so it sounds like the first one to me,

And also what kind of code have you tried already, Here are some key phrases to google.

  • "java get all files in a directory"
  • "java how to read files"

There is already a ton of info out there on these subjects but just for a quick search on the first one I find a similar question here.

Community
  • 1
  • 1
Sean_A91
  • 333
  • 4
  • 15