Results 1 to 9 of 9
  1. #1
    yuminstall is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Question Update Text File

    Hi,

    I have been making a banking system in Java, at the moment I have been trying to get it to recorded certain values to a text document which I have done. I cant seem however to get it to update the text file instead of recreating it each time.

    Src:

    Java Code:
    import java.io.*;
    public class storage {
    
    	public static void main(String[] args) throws IOException{
    			
    			//Record Details
    			Writer output = null;
    		    String accountnumber = "number";
    		    String accountname = "name";
    		    String accounttype = "Normal";
    		    String accountbalance = "39,000";
    		    String accountstatus = "enabled";
    		    File info = new File("accounts.txt");
    		    output = new BufferedWriter(new FileWriter(info));
    		    
    		    //Layout
    		    output.write(" |");
    		    output.write(" ");
    		    output.write(accountnumber);
    		    output.write(" |");
    		    output.write(" ");
    		    output.write(accountname);
    		    output.write(" |");
    		    output.write(" ");
    		    output.write(accounttype);
    		    output.write(" |");
    		    output.write(" ");
    		    output.write(accountbalance);
    		    output.write(" |");
    		    output.write(" ");
    		    output.write(accountstatus);
    		    output.write(" |");
    		    output.close();
    		    System.out.println("File Written.");        
    		    
    
    	}
    
    }

  2. #2
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    If I have understood you, then you should look at java.nio package
    java.nio package
    But I think you don't need that sophysticated feature....

  3. #3
    yuminstall is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    all I need is some code to make it add a new line each time in the text document so every time it compiles it wont start a new file but just keep adding a line to the existing one.

  4. #4
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    OK so all you need is a Reader to read your file in your program. THEN manipulate it and write it to disk with a Writer like you did

  5. #5
    yuminstall is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    im not sure on the code for that, any chance you could give me an example?
    Last edited by yuminstall; 01-09-2009 at 01:58 PM.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    This sort of thing is perfect for RandomAccessFile (although I believe that is, probably, far too advanced for you).

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default append?

    Check to see if the file exists and if it does, use the append method instead of the write method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    yuminstall is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    yeah the file exists and has the information in it, how would i change the code so it would use the append method instead?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs. Namely the constuctors for FileOutputStream (and other similar classes). Note, that is only useful for appending to the file, not for modifying the existing contents of the file.

Similar Threads

  1. using java dom to update file
    By newbieal in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 11:16 PM
  2. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  3. [SOLVED] Update an XML file
    By Eranga in forum Advanced Java
    Replies: 4
    Last Post: 07-15-2008, 02:03 PM
  4. can i update the tag value of XMl file by other value(string
    By pankaj_salwan in forum Advanced Java
    Replies: 7
    Last Post: 07-04-2008, 08:12 AM
  5. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 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
  •