|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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. ![]() |
#2
| |||
| |||
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 ... ![]()
and declare your class this way:
You should know that your items will not be saved. The serialization is the name given to action to save an object. |
#3
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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 ();) |
![]() |
|
Tags: buffer, ioexception, java, serializable, stacktrace, txt file |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |