Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Reading text files from a folder


  • Please log in to reply

#1
bainne

bainne

    Member

  • Member
  • PipPip
  • 11 posts
This is probably a simple matter of a for loop but if it is i cant figure it out. If i have a folder containing say four text files, how would i read them in one after the other without program termination? But allowing access to all of them at the same time so that they can be compared.
Any help on this would be greatly appreciated
Thanks
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
hello again bainne,

in your jan 7 topic, you were comparing 2 ArrayLists. use the same code to fill 4 ArrayLists, instead of just 2.

now the files are closed and you can compare ArrayLists. let's say the name of the method for comparing lists is compareList. there are 6 possible combinations to compare - list1,list2 list1,list3 list1,list4 list2, list3 list2,list4 list3,list4

here's how a loop might look:

ArrayList[] lists = new ArrayList[4];
lists[0] = list1;
lists[1] = list2;
lists[2] = list3;
lists[3] = list4;
for( int i = 0;i < 4;i++ ) {
for( int j = i + 1;j < 4;j++ ) {
compareList( lists[i], lists[j] );
}
}

without using a loop:

compareList( list1, list2 );
compareList( list1, list3 );
compareList( list1, list4 );
compareList( list2, list3 );
compareList( list3, list4 );
compareList( list4, list4 );

bdlt
  • 0

#3
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Thanks! I tried this and but i got error that cant convert from list to arrayList. I have defined the lists as List list1 = new ArrayList();
However the two lists are made up of the words that i am reading in from the text files once they have been tokenized. The main problem is actually reading in the text files so that each list will contain one of the four text files in the folder. I need to do this so that the folder will continually read in the next file in the folder until the folder is empty. The reason being that in some cases a folder may contain 30 text files and so it wont be feasible to read them in manually one at a time.
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
instead of
ArrayList[] lists = new ArrayList[4];

try
List[] lists = new List[4];

do you want this to work for 4 files, or any number of files?

are the filenames similar( like file1.txt, file2.txt, ... )?
  • 0

#5
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Its will be any number of files. The example im working on at the moment is four, but that can change.
  • 0

#6
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
and yes sorry, the file names are all the same except for the last digit
  • 0

#7
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
here's one way to read the files using ArrayList and a method readFile() which returns an ArrayList object. if you want to use List objects, have your readFile() method return a List object.
note: this is for filenames file0.txt, file1.txt, file2.txt, file2.txt
and readFile() throws IOException


  private void fileLoop() {
	int	count = 0;
	String name = "file";
	String filename = name + count + ".txt";
	boolean done = false;
	ArrayList[] lists = new ArrayList[4];
	while( !done ) {	   
	  try {	
		lists[ count ] = readFile( filename );
	  }
	  catch( IOException ioe ) {
		System.out.println( " error reading " + filename );
		done = true;
	  }
	  count++;
	  filename = name + count + ".txt";
	  if( count >= lists.length ) {
		done = true;
	  }
	}

  }

  • 0

#8
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Yeah thats just what i was i trying to do, thanks!
  • 0

#9
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
you're welcome.

remember to change the declaration below if the number of files differs from 4
ArrayList[] lists = new ArrayList[4];
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP