Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By roofninja

Thread: how do I append to a file

  1. #1
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default how do I append to a file

    What I have works except that it doesn't append to the file if it is already there. How do I do this. I have been reading and searching and keep finding all kinds of stuff, but I haven't seen an answer.

    I do need it to save to the desktop.

    I don't need to read the file, just add to the file.
    Java Code:
    import java.util.*;
    import java.lang.*;
    import java.net.*;
    import java.io.*;
    public class PCname 
    {
    	public static void main(String args[]) throws IOException
            {
    		String pcname=InetAddress.getLocalHost().getHostName();
    		//System.out.println(computername);
    	
    		//open the file
    		FileReader freader=new FileReader();
    
    		String userDesktop = System.getProperty("user.home") + "/Desktop";
    		File tFile = new File(userDesktop,"pcname.txt");
    		FileWriter fw=new FileWriter(tFile);
    		PrintWriter opf=new PrintWriter(fw);
    
    		//write the name to the file
    		opf.println(pcname);
    
    		//close the file
    		opf.close();
    	}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: how do I append to a file

    Quote Originally Posted by roofninja View Post
    I have been reading and searching and keep finding all kinds of stuff, but I haven't seen an answer.
    just look in the api doc ...FileWriter (Java Platform SE 6)
    "Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning."

  3. #3
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default Re: how do I append to a file

    I find the way those pages to be difficult to understand.

    So I changed line 17:
    FileWriter fw=append FileWriter(tFile);

    Ok....I can't figure this out. Where does it go and and what line am I fixing? As you can see, I am having a hard time understanding the syntax of the language. This is frustrating.

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

    Default Re: how do I append to a file

    Quote Originally Posted by roofninja View Post
    I find the way those pages to be difficult to understand.

    So I changed line 17:
    FileWriter fw=append FileWriter(tFile);
    This doesn't look changed to me. eRaaaa told you what you need to do: you need to use the two parameter constructor with the second parameter being a boolean.

    Ok....I can't figure this out. Where does it go and and what line am I fixing? As you can see, I am having a hard time understanding the syntax of the language. This is frustrating.
    Keep studying the API. Just like any skill, it gets better with practice, and being able to understand it is key to being able to code in Java.

  5. #5
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default Re: how do I append to a file

    I see what I did wrong. It took me some time to see what I was missing, but I did figure it out.

    FileWriter fw=new FileWriter(tFile,true);
    Fubarable likes this.

Similar Threads

  1. Append and Renaming of File
    By abhinav01 in forum New To Java
    Replies: 5
    Last Post: 07-22-2012, 02:44 PM
  2. Replies: 2
    Last Post: 04-02-2012, 01:59 PM
  3. Append to file
    By berkeleybross in forum CLDC and MIDP
    Replies: 9
    Last Post: 06-18-2010, 11:42 AM
  4. How to Append in file ?
    By Hippo in forum New To Java
    Replies: 2
    Last Post: 03-19-2010, 01:50 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
  •