6 Replies - 29612 Views - Last Post: 29 March 2009 - 04:37 PM Rate Topic: -----

#1 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

open and read files from folder

Post icon  Posted 29 March 2009 - 04:22 AM

i have few txt files in one folder. i need a program that will open that folder and read all files one by one to find a string and give me output.

so far i got code to do this from only one particular txt file. but i cant make it from many files in a folder.

can anybody help me pls.................
FileReader fr;				BufferedReader br;
String result="";
String word= new String();
String target = "friend";

try{ //read one text file 
fr = new FileReader ("C:/Users/user/Desktop/java/test.txt");
br = new BufferedReader(fr);
Scanner scan = new Scanner(br); 

while(scan.hasNext()){  
result = scan.findWithinHorizon(target,0); 

if(result!=null) {
word = (scan.next() + scan.findWithinHorizon("", 0));

ArrayList<String> names = new ArrayList<String>();
names.add(word);
for (int i=0; i< names.size(); i++) {
System.out.println(names.get(i));}



Is This A Good Question/Topic? 0
  • +

Replies To: open and read files from folder

#2 saumya9888  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 08-August 08

Re: open and read files from folder

Posted 29 March 2009 - 05:13 AM

Just make a File object of that folder and get all its files into a file array using f.listFiles and use them with the function you provided...
Was This Post Helpful? 0
  • +
  • -

#3 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

Re: open and read files from folder

Posted 29 March 2009 - 07:44 AM

View Postsaumya9888, on 29 Mar, 2009 - 04:13 AM, said:

Just make a File object of that folder and get all its files into a file array using f.listFiles and use them with the function you provided...


i just did it... it's not showing any error but no output
can u pls tell me where need to edit more ??

File directory = new File("C:/Users/user/Desktop/java/test");
String filename[] = directory.list();
	   
		 for (int i = 0; i < filename.length; i++) {
		listFilenames = filename[i];}
		
	//	System.out.print(listFilenames);

fr = new FileReader (listFilenames);
br = new BufferedReader(fr);
Scanner scan = new Scanner(br); 

This post has been edited by doha786: 29 March 2009 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: open and read files from folder

Posted 29 March 2009 - 10:05 AM

ArrayList<String> listNames = new ArrayList<String>();
String out;
JFileChooser chooser = new JFileChooser();//creates a file chooser to avoid magic names
   		File infile = null;// instanties files and file reader
   		FileReader reader = null;
  if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) // if the file is what its supposed to be, then accepts it and assigns values
   		{
   			infile = chooser.getSelectedFile();
   			reader = new FileReader(infile);
   		}
Scanner input = new Scanner(infile);//Creates a scanner to scan the file
while (input.hasNextLine())// while it has another line will keep scanning
{
out = input.nextLine();
listnames.add(out);
}


Then just perform any of the functions that you need to do to the array. This is a generic read in line by line Code.
Hopes This Helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

Re: open and read files from folder

Posted 29 March 2009 - 10:22 AM

thanks 4 ur reply...

actually i dont want to give any option for user to choose the folder, it will be fixed from me. i already given my full code in 1st post...

i m very new in java... can u pls suggest the easiest way..................
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: open and read files from folder

Posted 29 March 2009 - 02:40 PM

Well, if thats how you want it to be, then just assign the file like you did before
File directory = new File("C:/Users/user/Desktop/java/test");
then switch the appropriate names
reader = new FileReader(directory);
Scanner input = new Scanner(directory);

That would be how to do it with a set file path. I like to use JFileChooser for I take into consideration they may want to load a different file. Say they save 2 things? You will only be able to load that One item. But i guess since your just loading and not saving, that will work, but I dont like it ;)

Goodluck! :)
Was This Post Helpful? 1
  • +
  • -

#7 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

Re: open and read files from folder

Posted 29 March 2009 - 04:37 PM

thanks once again...

i just did it but still i cant get my desired output. not showing any error but dont know where i m doing wrong....

here is my full code:
import java.util.*; 
import java.io.*; 
import java.lang.*;

public class ReadFolderfFiles{ 

public static void main (String args[]){ 

FileReader reader;
String result="";
String word= new String();
String target = "friend";

		try {
 File directory = new File("C:/Users/user/Desktop/java/test");
reader = new FileReader (directory);		
Scanner scan = new Scanner(reader); 

while(scan.hasNext()){  
result = scan.findWithinHorizon(target,0); 

if(result!=null) {
word = (scan.next() + scan.findWithinHorizon("", 0));

ArrayList<String> names = new ArrayList<String>();
names.add(word);
for (int j=0; j< names.size(); j++) {
System.out.println(names.get(j));}

//System.out.println(word);

  }
  }
 
scan.close();
 }

catch(Exception e){ System.err.println(e.getMessage()); }
 } } 



pls help me..........
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1