Results 1 to 7 of 7
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default FileAppender help

    I'm trying to make a fileAppender, what will take lines from one file and will add those lines what arent in another file to the other file.
    The fileappender uses a for loop to go throw many files.
    The problem is that it only writes the last file, those what where before are empty. Maybe someone can help me with that?

    Java Code:
    private void fileAppender(){
    		
    		File tempFiles = new File(tempPath);
    		String[] tempList = tempFiles.list();
    		
    		BufferedReader brTemp = null;
    		BufferedReader brLocal = null;
    		BufferedWriter bw = null;
    		try{
    			for(int i = 0; i < tempList.length; i++) {
    				ArrayList tempFileContent = new ArrayList();
    				ArrayList localFileContent = new ArrayList();
    				System.out.println("list: " + tempList[i]);
    				
    				/*
    				 * If file doesnt exist, make new file
    				 */
    				File newFile = new File(localPath + "/" + tempList[i]);
    				if(!newFile.exists()){
    					newFile.createNewFile();
    				}
    				brTemp = new BufferedReader(new FileReader(tempPath + "/" + tempList[i]));
    				brLocal = new BufferedReader(new FileReader(localPath + "/" + tempList[i]));
    				String line = null;
    				while((line = brTemp.readLine()) != null){
    					tempFileContent.add(line);
    					System.out.println("Temp list: " + line);
    				}
    				while((line = brLocal.readLine()) != null){
    					localFileContent.add(line);
    					System.out.println("local list: " + line);
    				}
    				/*
    				 * Get new lines from temp file
    				 */
    				for(int k = 0; k < tempFileContent.size(); k++) {
    					if(!localFileContent.contains(tempFileContent.get(k))) {
    						localFileContent.add(tempFileContent.get(k));
    						System.out.println("new list " + tempFileContent.get(k));
    					}
    				}
    				/*
    				 * Write new lines from temp file to task file
    				 */
    				bw = new BufferedWriter(new FileWriter(localPath + "/" + tempList[i]));
    				for(int c = 0; c < localFileContent.size(); c++){
    					bw.write(localFileContent.get(c).toString());
    					bw.newLine();
    				}
    				
    			}
    			/*
    			 * Delete temp files
    			 */
    			for(int g = 0; g < tempList.length; g++){
    				File file = new File(tempPath + "/" + tempList[g]);
    				if(file.exists()){
    					file.delete();
    				}
    			}
    			
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			try{
    				if(brTemp != null){
    					brTemp.close();
    				}
    				if(brLocal != null){
    					brLocal.close();
    				}
    				if(bw != null){
    					bw.flush();
    					bw.close();
    				}
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    		
    		
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by karq View Post
    I'm trying to make a fileAppender, what will take lines from one file and will add those lines what arent in another file to the other file.
    The fileappender uses a for loop to go throw many files.
    The problem is that it only writes the last file, those what where before are empty. Maybe someone can help me with that?
    Please clarify this, especially the second to last sentence.

    Luck!

  3. #3
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please clarify this, especially the second to last sentence.

    Luck!
    basically I have 2 folders
    FOLDER1 contains: car, tree
    FOLDER2 contains:car, tree

    folder1/car contains lines:
    this is a car
    i'm audi
    folder2/car contains lines:
    i'm bmw
    i'm a car
    i'm audi

    folder1/tree contains lines:
    i'm a tree
    this is a tree
    folder2/tree contains lines:
    i'm not a tree
    i'm a tree
    trees are cool


    The fileAppender should do this:
    takes lines from folder1/car and append those lines what arent in folder2/car to folder2/car
    folder2/car should contain after appending
    i'm bmw
    i'm a car
    i'm audi
    this is a car

    and the folder2/tree should contain after appending:
    i'm a tree
    this is a tree
    i'm not a tree
    trees are cool

    And the problem is that after appending the files look like this:
    folder2/car is empty
    folder2/tree contains:
    i'm a tree
    this is a tree
    i'm not a tree
    trees are cool

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    You have a problem with your logic. You need to see what the code is doing and compare that with what you want it to do.
    Have you tried debugging your code by adding println() statements to show the values of variables as they change, to show execution flow such as when you are adding or not adding contents to the lists.

  5. #5
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You have a problem with your logic. You need to see what the code is doing and compare that with what you want it to do.
    Have you tried debugging your code by adding println() statements to show the values of variables as they change, to show execution flow such as when you are adding or not adding contents to the lists.
    Yes, I have and the problem is around here
    Java Code:
    /*
    				 * Write new lines from temp file to task file
    				 */
    				bw = new BufferedWriter(new FileWriter(localPath + "/" + tempList[i]));
    				for(int c = 0; c < localFileContent.size(); c++){
    					bw.write(localFileContent.get(c).toString());
    					bw.newLine();
    					
    				}
    Everything else works fine.
    Maybe I should use something else to write files?
    Last edited by karq; 09-06-2010 at 07:06 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    I don't see any debug code in the code you posted.
    For example what is the value of localFileContent.size()?

  7. #7
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I don't see any debug code in the code you posted.
    For example what is the value of localFileContent.size()?
    Found the problem, I didnt close the bufferedWriter in the right way. I closed it after all files where proccessed, but the right way was that I had to close the bufferedWriter everytime when i wrote a file

    Here's the working code:
    Java Code:
                          /*
    				 * Write new lines from temp file to task file
    				 */
    				bw = new BufferedWriter(new FileWriter(localPath + "/" + tempList[i]));
    				for(int c = 0; c < localFileContent.size(); c++){
    					bw.write(localFileContent.get(c).toString());
    					bw.newLine();
    					
    				}
    				bw.flush();
    				bw.close();

Posting Permissions

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