Reading multiple text files from same folder

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2570 Views - Last Post: 29 July 2015 - 02:00 AM Rate Topic: -----

#1 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Reading multiple text files from same folder

Posted 27 July 2015 - 04:09 AM

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

        
                   
   
	    }
}
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Reading multiple text files from same folder

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 27 July 2015 - 05:04 AM

I'd probably use http://docs.oracle.c...nputStream.html for this
Was This Post Helpful? 0
  • +
  • -

#3 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 01:30 AM

View Postg00se, on 27 July 2015 - 05:04 AM, said:

I'd probably use http://docs.oracle.c...nputStream.html for this


Respected member:

I changed it a bit it is still giving access denied error for files. Kindly check; help will be highly appreciated. Though the permission rights on folder are granted. Its still not reading files.
File filedir= new File ("D:\\MsThesis\\from csv files to text files");
	for (String file: filedir.list()){
		FileInputStream fIstream = new FileInputStream(filedir);
		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();
	
	        }
	
	    }
	




View PostRoshanayKhan, on 28 July 2015 - 01:29 AM, said:

View Postg00se, on 27 July 2015 - 05:04 AM, said:

I'd probably use http://docs.oracle.c...nputStream.html for this


Respected member:

I changed it a bit it is still giving access denied error for files. Kindly check; help will be highly appreciated. Though the permission rights on folder are granted. Its still not reading files. From csv files to text files is name of folder.
File filedir= new File ("D:\\MsThesis\\from csv files to text files");
	for (String file: filedir.list()){
		FileInputStream fIstream = new FileInputStream(filedir);
		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();
	
	        }
	
	    }
	



Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 01:51 AM

Try

for (File file: filedir.listFiles()) {
	BufferedReader br = new BufferedReader(new FileReader(file));
...
}

Was This Post Helpful? 0
  • +
  • -

#5 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:05 AM

View Postg00se, on 28 July 2015 - 01:51 AM, said:

Try

for (File file: filedir.listFiles()) {
	BufferedReader br = new BufferedReader(new FileReader(file));
...
}



Respected member:

I tried this too :
File filedir= new File ("D:\\MsThesis\\from csv files to text files");
for (File file: filedir.listFiles()){

BufferedReader br = new BufferedReader(new FileReader(file));

rest of code is same; it is reading only one file from directory and calculating probability for it. Kindly check the output in attachment it is shown. There are about 21 text files in folders each with a different language. "from csv files to text files" is name of folder. I want it to read all files and calculate probability for each text file.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:09 AM

Quote

Kindly check the output in attachment it is shown.

There's no attachment. But anyway - please just post the new code you're using
Was This Post Helpful? 0
  • +
  • -

#7 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:21 AM

View Postg00se, on 28 July 2015 - 02:09 AM, said:

Quote

Kindly check the output in attachment it is shown.

There's no attachment. But anyway - please just post the new code you're using

File filedir= new File ("D:\\MsThesis\\from csv files to text files");
	for (File file: filedir.listFiles()){
		System.out.print("file name is" +file);
		BufferedReader br = new BufferedReader(new FileReader(file));
		
		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();


}
	}
	
	    
}
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:43 AM

dir "D:\MsThesis\from csv files to text files"


Please post the output of the above command
Was This Post Helpful? 0
  • +
  • -

#9 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:55 AM

View Postg00se, on 28 July 2015 - 02:43 AM, said:

dir "D:\MsThesis\from csv files to text files"


Please post the output of the above command


Respected member, kindly check the output. It is reading cs.txt file only from directory named "from csv files to text files". Thank you for your kind concern.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 02:58 AM

Quote

kindly check the output.

I can't really - you've cut most of it off ;)

Quote

It is reading cs.txt file only


That won't help ...
Was This Post Helpful? 0
  • +
  • -

#11 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 03:11 AM

View Postg00se, on 28 July 2015 - 02:58 AM, said:

Quote

kindly check the output.

I can't really - you've cut most of it off ;)/>

Quote

It is reading cs.txt file only


That won't help ...


Accept my apologies,
I just simply printed the directory path ; Lemme show full pic,

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 03:14 AM

I'm not interested in the output of the app. I'm interested in the output of the command i gave you
Was This Post Helpful? 0
  • +
  • -

#13 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 03:21 AM

View Postg00se, on 28 July 2015 - 03:14 AM, said:

I'm not interested in the output of the app. I'm interested in the output of the command i gave you


Respected Member,

Yes I just printed same directory path.
                                        ArrayList<Language> langlist = detector.getProbabilities();
					String lang = detector.detect();
					System.out.println("Directory name is" +"" +filedir);
					System.out.println("Language Detected for input file is" +" " +lang); 



I used filedir variable.
As you mentioned dir "D:\MsThesis\from csv files to text files"

please mention what should be type of dir? As it is having a folder path. I should simply write dir= "path" ?
Was This Post Helpful? 0
  • +
  • -

#14 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 03:32 AM

That's a command at the command line (cmd.exe)
Was This Post Helpful? 0
  • +
  • -

#15 RoshanayKhan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-July 15

Re: Reading multiple text files from same folder

Posted 28 July 2015 - 03:50 AM

View Postg00se, on 28 July 2015 - 03:32 AM, said:

That's a command at the command line (cmd.exe)


Respected member,

I am running algorithms through eclipse. Kindly check this code chunk, it is showing exception in it path (Line 05) . Same exception access denied. Is there something wrong with folder access rights? i might be making very small mistake in the logic or is there folder rights issue. The code is only executing while loop. For loop does not works. Thank you sir for your continuous concern
File path= new File ("D:\\MsThesis\\from csv files to text files");
	for (File file: path.listFiles()){
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(path));
			//System.out.println("Directory name is" +path.getAbsolutePath()); Checked ; works
			String line;
			while((line = br.readLine()) != null)
				
			{
				
				   detector.append(br);
					ArrayList<Language> langlist = detector.getProbabilities();
					String lang = detector.detect();
				//System.out.println("Directory name is" +"" +filedir);
					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();


}
	}
	
	    
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2