Respected members,

I have this code chunk that should read multiple text files from a folder. I am later calculating their probability. For one file it is working fine and methods are also showing output. The first code is for ONE FILE named as English.txt.

BufferedReader reader = new BufferedReader(new FileReader("D:\\MsThesis\\from csv files to text files\\English.txt"));

    String line;
    try {
        while((line = reader.readLine()) != null)
        {
            try {
                detector.append(reader);  //reader object instance
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ArrayList<Language> langlist = detector.getProbabilities(); //Call Method to calculate probabilities
    String lang = detector.detect();
    System.out.println("Language Detected for input file is" +" " +lang); 
    System.out.println("Probability of language is: " +" " +langlist); 




}

Now I want it to iterate over all the text files in the folder and I want these methods to be called for everyone, so that it calculates probability of every file separately. I have tried a chunk but it shows "Access denied error for the folder". Help will be highly appreciated. Kindly do guide what should be done to achieve this output. Below code shows the chunk I tried.

String currentLine="";
    String path= "D:\\MsThesis\\from csv files to text files";
    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 

    for (int i = 0; i < listOfFiles.length; i++) 
    {
        if (listOfFiles[i].isFile()) 
        {
            files = listOfFiles[i].getName();
            if (files.endsWith(".txt") || files.endsWith(".TXT"))
            {
                //File textFile = new File(files); 
                try {
                    BufferedReader br = new BufferedReader(new FileReader(path)); 
                    while ((currentLine = br.readLine()) != null) { 
                    }
                    br.close(); 
                } catch (FileNotFoundException e) {
                    System.out.println(e.getMessage());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }
    ArrayList<Language> langlist = detector.getProbabilities();
    String lang = detector.detect();
    System.out.println("Language Detected for input file is" +" " +lang); 
    System.out.println("Probability of language is: " +" " +langlist);




        }
}
    }

On which line did it throw the exception?
What was the file/path that you were processing at the time?

commented: It throws exception on second one. The first one works for one text file, i want to iterate it over the same folder for all file. Same path +0

What was the exact file/path that you were processing for the second one?

Respected James, the path for the second code chunk is the folder path that is being given to String Variable "Path".
String path= "D:\MsThesis\from csv files to text files";
"from csv file to text files" is name of that directory in which there are text files.
I hope that's what you are asking.

Not quite. When you say "the second one" are you referring to listOfFiles[1] ?
What exactly are the values in listOfFiles when the exception is thrown.

You still didn't say which line in the source code is the one where the exception is thrown

line 16. By second one I mean the second chunk of code I shared. I made bit changes but the "access is denied" File not found exception. kindly check this 3rd edited code below. Using SequenceInputStream will be beneficial ?

File filedir= new File ("D:\\MsThesis\\from csv files to text files");
    for (String file: filedir.list()){
        FileInputStream fIstream = new FileInputStream(filedir); //causes exception here. file not found
        DataInputStream in = new DataInputStream(fIstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        try {
            while((line = br.readLine()) != null)
            {

                    detector.append(br);
                    ArrayList<Language> langlist = detector.getProbabilities();
                    String lang = detector.detect();
                    System.out.println("Language Detected for input file is" +" " +lang); 
                    System.out.println("Probability of language is: " +" " +langlist);

                //System.out.println(line);
            }
            br.close();
        }

            catch (FileNotFoundException e)

            {

                e.printStackTrace();

            }

            catch (IOException e)

            {

                e.printStackTrace();

}
    }

Line 16 is a blank line. Exactly which line of code is throwing the exception and what exactly are the contents of the filelist?

Respected James,

Accept my apologies. It is line 3. It is mentioned in comment that this line throws exception of "Access is Denied" file not found exception,for the folder. I have checked access rights for the folder also.

You are trying to open an input stream on the directory filedir itself, which cannot work.
I suspect you wanted to open the stream on each file in that loop.

ps: You shoild also check that file is a file and not a sub-directory.

Here is the check also. File is "file"

String target_dir = "./testdataset";
int i = 0;
BufferedReader inputStream = null;
File dir = new File(target_dir);
File[] files = dir.listFiles();

for (File f : files) {
    if(f.isFile()) {
        System.out.println("File name in directory is: " + f);  
        inputStream = new BufferedReader(new FileReader(f));

    }
    String line;
    try {
        while((line = inputStream.readLine()) != null) {

             detector.append(inputStream);  
        }

        String lang = detector.detect();
        ArrayList<Language> langlist = detector.getProbabilities();
        System.out.println("Language Detected for input file is" + " " + lang); 
        System.out.println("Probability of language is: " + " " + langlist); 
        inputStream.close();      
    }
    catch(Exception e) {}
}

in while loop it only reads first file from folder.

catch(Exception e) {}
Never do that.
Maybe there's an exception being thrown, but you have supressed it.
Change it to
catch(Exception e) {e.printStackTrace();}
and try again

Also. think about what will happen if f is sub-directory!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.