Results 1 to 3 of 3
- 02-25-2010, 06:49 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
Comparing contents of a file with String[]
Hi,
I am trying to compare the contents of an array to what ever there is in a file...so that
if - whatever i have in array[i] is in file
DO SOMETHING
else
DO SOMETHING
as of now i take the first element of my array and check it agains my file, however, when i find a match in my file i dunno how to reset my Buffer reader to start reading from the top of the file again and to check the next element in my array...here is the code
JUST SO U HAVE AN IDEA.....Java Code:FileInputStream fstream = new FileInputStream(pathOfDSKFile + "DSK_record.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine, str; boolean a; // Read File Line By Line while ((strLine = br.readLine()) != null) { for (int i = 0; i < filesForBAT.length; i++) { do { str = filesForBAT[i].substring(0, (filesForBAT[i].length()) - 4); //debugging //getting rid of the .txt at the end of the string and comparing to string in file a = filesForBAT[i].substring(0, (filesForBAT[i].length()) - 4).equals(strLine); //debugging if (a == true) { // create SDK OR NOT System.out.println("SDK" + filesForBAT[i].substring(0, (filesForBAT[i].length()) - 4) + " was created already"); //HERE IS WHERE I NEED TO RESET OR START READING AGAIN FROM THE BEGINNING OF THE FILE AND MOVE TO i = 1 in my for loop in.close(); } } while ((strLine = br.readLine()) != null); }//end of for }//end of while
Data in File ----------------- Data in filesForBAT[]
crs_SP2010 ......................... crs_SP2013.txt
usr_SP2010 ......................... usr_SP2013.txt
crs_SP2011
usr_SP2011
crs_SP2012
usr_SP2012
crs_SP2013
usr_SP2012
- 02-25-2010, 08:46 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
You don't want to open and close File n times or to move through stream n times, how about converting File to String and work with String?
Each line you read from file can be appended to StringBuffer(or StringBuilder maybe it's lil faster),
add line feed after each, so you end up with String that represents a file.
Think about which loop to put in which and separate code for comparing in separate method and optimize it through some test with large files/arrays.
cheers
- 02-25-2010, 09:10 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
Concatinating contents of a string array based on condition
By gangsterooseven in forum Advanced JavaReplies: 2Last Post: 10-07-2009, 07:35 AM -
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks