Results 1 to 3 of 3
  1. #1
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

    Default help me to store data in a class

    hi,

    i have a project in which there is my classes, i want to create a class which will store the data in to a file. data are manipulation in different class.

    please tell me how to create that class which store the data.

  2. #2
    georgexu316 is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default

    You use the FileWriter and BufferedWriter class:
    Class FileWriter
    The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

    BufferedWriter

    The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.


    Example
    Java Code:
    import java.io.*;
    class FileWrite 
    {
       public static void main(String args[])
      {
          
        FileWriter fstream = new FileWriter("name.txt");        //create a file called name.txt
        BufferedWriter out = new BufferedWriter(fstream);    // create a writing stream
        out.write("Hello Java");                                        //write whatever you want
        out.close();                                                       // stops writing
        
      }
    }
    Last edited by georgexu316; 02-26-2009 at 02:44 PM.

  3. #3
    chrisjake is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    --------------------Configuration: <Default>--------------------
    C:\FileWrite.java:7: unreported exception java.io.IOException; must be caught or declared to be thrown
    FileWriter fstream = new FileWriter("name.txt"); //create a file called name.txt
    ^
    C:\FileWrite.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
    out.write("Hello Java"); //write whatever you want
    ^
    C:\FileWrite.java:10: unreported exception java.io.IOException; must be caught or declared to be thrown
    out.close(); // stops writing
    ^
    3 errors

    Process completed.

    hehe i've just got this when I copy paste your code

    ohh I forgot to right the throws IOException beside public static void main (String []args) __(>throws IOException<)__
    Last edited by chrisjake; 08-06-2009 at 08:33 AM. Reason: i forgot to write this sorry...

Similar Threads

  1. Store textfile data in an array
    By mokonji in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 05:28 PM
  2. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 04:25 AM
  3. How to read data from excel and store it in db
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-03-2008, 10:19 AM
  4. How to create folder(s) and store data?
    By Grom in forum New To Java
    Replies: 2
    Last Post: 08-20-2008, 09:01 AM
  5. how to use btree to store data
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05: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
  •