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 29-03-2010
Member
 
Join Date: Mar 2010
Posts: 203
Open text file and edit it in java

Hello,
I connect to file and open it with FileWriter class and write normally, i.e add a line to the file, but now I want to read the file and return a variable ip, a line like ip = 182.15 .. and change the IP. How can I perform this task, if you have any solution or any idea about then please let me know. Thanks in advance.
Reply With Quote
  #2  
Old 29-03-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Open text file and edit it in java

Hello,
Well instead of FileWriter you use a FileReader. Then you use a BufferedReader to read your file line by line (method "readLine ()). Finally, you analyze your line to determine if it interests you or not. If you need more information on the API which are used by java rather the java.io package which you are interested in then you can find then on the sun's official site.
Reply With Quote
  #3  
Old 29-03-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Open text file and edit it in java

Hello,
Even I do a same kind of a problem with my java code. I am trying it but nothing is working for me. I actually used FileReader and buffered with the method readline and I can access the line that I want but how can I now edit. I have searched a lot of help on internet but non of them is useful for me, rather it defines a high profile guide or tutorials, which are out of my scope. So, a simple explanation will be appreciated. Thanks in advance.
Reply With Quote
  #4  
Old 29-03-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: Open text file and edit it in java

Hello,
I think you can do the following. If the lines in your file are of the form "key = value", then it would be better to use a property file.
For example:
Here is the code for it
Code:
Properties p = new Properties();
d.load(new FileInputStream ("ton_fichier"));
/ / Mails to take the IP address
d.getProperty("IP");
//...
Just try the following code and see if it works.
Reply With Quote
  #5  
Old 29-03-2010
Member
 
Join Date: Nov 2009
Posts: 347
Re: Open text file and edit it in java

Hello,
You can not edit a file directly like this or else you'll have to use class RandomAccessFile.

With the method I gave you, you need:
- A StringBuilder
- To read each line, you add content to this last (append ())
- Reading the line that you want change, you add the amendment to the StringBuilder
- You add the lines remaining at end of file

Once done, you close your actual buffer reading and you open a write buffer to overwrite the file with the correct content contained in your StringBuilder.
Reply With Quote
Reply

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Open text file and edit it in java"
Thread Thread Starter Forum Replies Last Post
Need permission to edit even a text file VAIJAYI Operating Systems 4 07-01-2011 05:33 AM
How to append data to a text file in java? KALANI84 Software Development 4 05-02-2010 04:44 PM
How to edit Webpage Text Areas in Text Editor? roodiii Technology & Internet 5 27-01-2010 07:48 PM
How to edit text in PDF file? Don_Lightfoot Windows Software 8 09-01-2010 12:10 AM
Does java parse large text file Zeverto Software Development 3 30-07-2009 01:26 PM


All times are GMT +5.5. The time now is 04:16 AM.