Вопрос

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.

Это было полезно?

Решение

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

                }
            }
        }
    }

Другие советы

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

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

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top