Results 1 to 8 of 8
  1. #1
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default How to Write to a File Using a Method

    Hi all,
    I Need to write an Function in java which can take a parameter as filename and String Array and Write That Array to the given File. How to I Implement this using Function.

    public void WriteToFile(String f, String []arr)
    {
    f1=new File(this.f);
    f.createNewFile();
    FileWriter fstream = new FileWriter(f,true);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write([]arr+"");
    out.close();

    }



    Please Help Me for doing this I am New to this

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Ever heard of a loop? They come in 4 flavors: for, enhanced-for, while and do-while. You'll need to use one of them.

    db

  3. #3
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks Mr.Darryl.Burke,

    Can you tell me following method is right or wrong????????


    waiting for positive reply....

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Quote Originally Posted by Anagha View Post
    Thanks Mr.Darryl.Burke,

    Can you tell me following method is right or wrong????????


    waiting for positive reply....
    Waiting for following method...

  5. #5
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    public void WriteToFile(String f, String []arr)
    {
    f1=new File(this.f);
    f.createNewFile();
    FileWriter fstream = new FileWriter(f,true);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write([]arr+"");
    out.close();

    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Does that even compile?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You didn't take darryls advice, he suggested a loop. We dont give you the code, instead we help you figure it out. Here is some pseudocode.
    Java Code:
    open streams
    loop
      write to file
    end loop
    finally
      close stream
    end finally

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

Similar Threads

  1. Writing a method to write a method?
    By balla in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 12:05 PM
  2. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  3. Question about the write method in DataOutput
    By shai in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 06:49 AM
  4. how to write method syntax ?
    By Bader in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 05:58 PM
  5. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 02:43 AM

Posting Permissions

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