Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-07-2010
Member
 
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.
Reply With Quote
  #2  
Old 28-07-2010
Member
 
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.
Reply With Quote
  #3  
Old 28-07-2010
Member
 
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 ();)
Reply With Quote
  #4  
Old 28-07-2010
Member
 
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 ();)
Reply With Quote
  #5  
Old 28-07-2010
Member
 
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 ...
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Read and Write a .txt file with Java"
Thread Thread Starter Forum Replies Last Post
Need to read input text and write it to output file Tramba-keshwar Software Development 6 26-06-2011 10:38 PM
Is it possible to use one file handle to read and write in different files in c++? KALANI84 Software Development 5 02-02-2010 04:20 PM
File read-write mode in java ISAIAH Software Development 5 20-01-2010 10:02 AM
How to read and write files in Java BansiJ Software Development 3 02-09-2009 08:52 PM
How to create read write lock in java AlienKing Software Development 3 05-05-2009 08:18 PM


All times are GMT +5.5. The time now is 02:47 PM.