Results 1 to 5 of 5

Thread: Open text file and edit it in java

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

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

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

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

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

Similar Threads

  1. Need permission to edit even a text file
    By VAIJAYI in forum Operating Systems
    Replies: 4
    Last Post: 07-01-2011, 05:33 AM
  2. How to append data to a text file in java?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 04:44 PM
  3. How to edit Webpage Text Areas in Text Editor?
    By roodiii in forum Technology & Internet
    Replies: 5
    Last Post: 27-01-2010, 07:48 PM
  4. How to edit text in PDF file?
    By Don_Lightfoot in forum Windows Software
    Replies: 8
    Last Post: 09-01-2010, 12:10 AM
  5. Does java parse large text file
    By Zeverto in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 01:26 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,918,147.21754 seconds with 17 queries