Results 1 to 5 of 5

Thread: Read and Write a .txt file with Java

  1. #1
    Join Date
    Apr 2010
    Posts
    80

    Read and Write a .txt file with Java

    How I can save data to a .txt file with java..?? I have recently started doing the Java programming language, so I am not having much idea about the topic. I am trying to make a sample of coding in which the user enter data and when it gets on record after having to return the name of the file data is stored in a file .txt. I think that you should have understand my query.!! So please help me in solving my problem. Also I want to know how I can open a file .txt and retrieve data.?? Expecting some sooner solution with proper details.

  2. #2
    Join Date
    Mar 2008
    Posts
    232

    Re: Read and Write a .txt file with Java

    Even I am new user of Java, and I want to say that "FileOutputStream and FileInputStream" is not really clear ... To register a data in a file. Txt, you must first ask you if you register an object or a primitive type (integer, double, float ...). If you must store items, you must import the package java.io.Serializable example: we have a class "Book", you would save an object when a book ... and well it should return this line of code at the top of your class:
    • import java.io.Serializable;

    and declare your class this way:
    • public class Book implements Serializable

    You should know that your items will not be saved. The serialization is the name given to action to save an object.

  3. #3
    Join Date
    Mar 2008
    Posts
    258

    Re: Read and Write a .txt file with Java

    If you want to save an object, type this code :
    Code:
    Paper myBook; 
    ObjectOutputStream oos; 
    try ( 
    oos = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream (new File ("TextFile.txt" )))); 
    oos.writeObject (myBook); 
    oos.close (); 
    ) 
    catch (java.io.IOException e) (e.printStackTrace ();)

  4. #4
    Join Date
    Apr 2008
    Posts
    193

    Re: Read and Write a .txt file with Java

    I think that you want to read the object, so I have tried to explain you how to read the object by providing the following code :
    Code:
    ObjectInputStream ois; 
    try 
    ( 
    ois = new ObjectInputStream (new BufferedInputStream (new FileInputStream (new File ("TextFile.txt" )))); 
    myBook = (Book) ois.readObject (); 
    ois.close (); 
    ) 
    catch (IOException e) (e.printStackTrace ();) 
    catch (ClassNotFoundException e2) (e2.printStackTrace ();)

  5. #5
    Join Date
    Jan 2009
    Posts
    140

    Re: Read and Write a .txt file with Java

    If you just need to read and / or write primitive types, you do not need to import the package java.io.Serializable or implement "Serializable" to your class book. To save a primitive type in a text file type this code:
    Code:
    int count; 
    try ( 
    dos = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (new File ("TextFile.txt" )))); 
    dos.writeInt (number); 
    dos.close (); 
    ) 
    catch (java.io.IOException e) (e.printStackTrace ();)
    If you must record a double instead of an int for example, replaces the function writeInt by writeDouble ...

Similar Threads

  1. Need to read input text and write it to output file
    By Tramba-keshwar in forum Software Development
    Replies: 6
    Last Post: 26-06-2011, 10:38 PM
  2. Replies: 5
    Last Post: 02-02-2010, 04:20 PM
  3. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  4. How to read and write files in Java
    By BansiJ in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 08:52 PM
  5. How to create read write lock in java
    By AlienKing in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 08:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,993,510.06537 seconds with 17 queries