TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Read and Write a .txt file with Java (http://forums.techarena.in/software-development/1354745.htm)

Mulan 28-07-2010 01:49 AM

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.:notworthy Also I want to know how I can open a file .txt and retrieve data.?? Expecting some sooner solution with proper details.

Shell 28-07-2010 01:53 AM

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 ... :no: 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.

Khushal 28-07-2010 01:58 AM

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 ();)


Mecurtis 28-07-2010 02:00 AM

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 ();)


RichiRich 28-07-2010 02:03 AM

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 ...


All times are GMT +5.5. The time now is 01:40 AM.