Results 1 to 3 of 3
  1. #1
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default 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

    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
    JUST SO U HAVE AN IDEA.....

    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

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    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

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eponcedeleon View Post
    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
    Put all the lines mentioned in the second column in a Set<String> (without the .txt suffix) and check each line from the file against that Set; if found do something, else do something else. That way you only have to read the file once.

    kind regards,

    Jos

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  3. Concatinating contents of a string array based on condition
    By gangsterooseven in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 07:35 AM
  4. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •