Is it possible to modify a text file whithout copying it in a new one?

Hi all,
In fact I'm trying to use a kind of ini file to configure an application. So I have first to get information in a file using the name of the file, the name of the section and the keyname of the value I want to get. For this no problem. But I just can't change the value. I've read a lot of threads, but whithout finding a solution.
Here is an example of the ini file:
[SECTION1]
KEYWORD1=VALUE1
KEYWORD2=VALUE2
[SECTIONk]
KEYWORD1=VALUE1
KEYWORD2=VALUE2
[SECTIONn]
KEYWORD1=VALUE1
KEYWORD2=VALUE2
And here what I want to have next
[SECTION1]
KEYWORD1=VALUE1
KEYWORD2=VALUE2
[SECTIONk]
KEYWORD1=VALUE1
KEYWORD2=ANOTHERVALUE
[SECTIONn]
KEYWORD1=VALUE1
KEYWORD2=VALUE2
I read the file like this:
buffer = new BufferedReader (new FileReader(sourceFile));
Then I can modify buffer like this:
-I read a line (with buffer.readLine()) to obtain a string called oldString
-I convert it into a StringBuffer
-Next I use the replace method of StringBuffer class
-I can next have a String, I'll call it modifiedString
Now how can I place modifiedString in sourceFile at oldString's place. The only way I found to do this is to create a new file and to copy line after line all the lines from sourceFile, then to delete sourceFile and next to rename the new file in sourceFile. But I think that it's not really good and I'm sure that one (or more) of you has (have) already solve this problem with a better solution.
Thanks a lot.
     S�bastien

The class below remove some words from a file. I suppose you can change it so that it modify a word instead.
import java.io.*;
import java.util.*;
class RemoveWord
     public static void main(String [] arg)
          Vector word=new Vector();
          for(int i=1;i<arg.length;i++)
               word.add(arg);
          removeWord(arg[0],word);
     /*This function removes all the words beeing in the vector "word" from the file fileName     fichier fileName
     public static void removeWord(String fileName,Vector word)
          RandomAccessFile rf =null;
          try
               rf = new RandomAccessFile(fileName, "rw");
               int pos=-1;
               long posCurseur=rf.getFilePointer();
               String ligne=rf.readLine();
               while(ligne!=null)
                    for(int i=0;i<word.size();i++)
                         String wordToRemove=(String)word.get(i);
                         pos=ligne.indexOf(wordToRemove);
                         if(pos!=-1)
                              ligne=ligne.substring(0,pos)+ligne.substring(pos+wordToRemove.length(),ligne.length());
                              for(int j=0;j<wordToRemove.length();j++)
                                   ligne=ligne+" ";     
                              rf.seek(posCurseur);
                              rf.writeBytes(ligne);
                    posCurseur=rf.getFilePointer();
                    ligne = rf.readLine();
          catch(EOFException e){e.printStackTrace();}
          catch(FileNotFoundException e){e.printStackTrace();}
          catch(IOException e){e.printStackTrace();}
          catch(Exception e){e.printStackTrace();}
          finally
               try
                    rf.close();
               catch(IOException e){e.printStackTrace();}

Similar Messages

  • Is it possible to move my iTunes from the old laptop to the new one (including all the music files and playlists) without having to start all over again with a blank iTunes?

    Is it possible to move my iTunes from the old laptop to the new one (including all the music files and playlists) without having to start all over again with a blank iTunes?

    yes
    You will need to copy the whole iTunes folder which is in your user profile on the old computer in Music folder.
    Before you do it make sure iTunes are updates on the old computer and it works without any problems.
    This will copy all your media, music, movies (provided you did not change the default location).
    After the migration you will need to sing in to Apple store again with your Apple ID
    and also authorize your new computer (both done from the tom menu bar in Store.
    Once you are done please deauthorize your old computer (the same menu path) if you are not planning to use it anymore.
    Any problems post here.

  • Best way to transfer files from old Macbook Pro to new one?

    I just got a new Macbook Pro from Apple...YEEEEAAAHHHH! 
    So....
    What's the best way to transfer files from old Macbook Pro to new one?  I know Migration assistant, but is there a recommended way?  I don't want to put do a system restore or transfer everything from the old laptop, but I do want to move important things like photos, music, documents and such over.  Any suggestions on best way to do this?
    Another question, perhaps subquestion.  If I have, say, an older version of iPhoto but a newer version of one on my new Macbook Pro, will it still transfer photos no problem?
    Just looking for hints from the experts so this puppy is set up in the best way possible.

    Just one comment on this:
    ‘If I have, say, an older version of iPhoto but a newer version of one on my new Macbook Pro, will it still transfer photos no problem?’
    the problems are ALWAYS in the reverse way, when you have the newest (or a newer) version of a given app, and wish to transfer its library to an older version.
    Otherwise, see the link to Pondini.
    I have been using Martin Jahn’s iBackup, with which I have become accustomed and works fine. It also makes daily backups. Its advantage over other apps is that you may add whatever you wish to save, beside its default settings (which you may delete or cancel, of course, even if not recommended); it also has a friendly interface and easily customizable. Of course, this is a personal view, you may try other methods as well, or other backup apps. All are good if you are satisfied and correspond to your needs.

  • Is it possible to transfer some games from an old iPad to a new one without loosing all the data?

    Is it possible to transfer some gMes from an old iPad to a new one without losing all my game's data?

    yes
    You will need to copy the whole iTunes folder which is in your user profile on the old computer in Music folder.
    Before you do it make sure iTunes are updates on the old computer and it works without any problems.
    This will copy all your media, music, movies (provided you did not change the default location).
    After the migration you will need to sing in to Apple store again with your Apple ID
    and also authorize your new computer (both done from the tom menu bar in Store.
    Once you are done please deauthorize your old computer (the same menu path) if you are not planning to use it anymore.
    Any problems post here.

  • I transferred settings and files from old MacBook air to new one.  New one now gives popup about failing to connect to server (old iMac).  the new Mac air and old iMac have never met and are 2000 miles apart. How do I stop the popups?

    I transferred settings and files from old MacBook air to new one.  New one now gives popup about failing to connect to server (old iMac).  the new Mac air and old iMac have never met and are 2000 miles apart. How do I stop the popups?

    Change your settings in Sharing preferences, for one, and remove the server out of the Finder's sidebar. Obviously, this is the result of transfering all your other computer's preferences to your new computer.

  • Is it possible to read a text file into an applet?

    I have a text file that contains data I want to use in my applet. Is there a way to read in the data?
    Thanks,
    Brad

    With a signed applet you can read a file from the local hard disk. There is a forum for signed applets. The source code of your signed applet will require some additional code (before opening and reading the file, like // create a permission object
    FilePermission perm = new FilePermission("path/file", read);
    // check the permission object
    AccessController.checkPermission(perm);

  • HT2548 how do i copy the domain file from OS 10.4 to a lion OS computer?  I made a copy of the domain file and copied it to my new computer, but it doesn't open in iweb when I double-click on it.

    How do I import an iweb domain file from an ibook (OS 10.4) to an imac (OS 10.73)?
    I copied the domain file from my ibook to my imac, but when I double-click on the file, it doesn't open in iweb. 

    In Lion the Library folder is now invisible. To make it permanently visible enter the following in the Terminal application window: chflags nohidden ~/Library and hit the Enter button - 10.7: Un-hide the User Library folder.  Then place the domain file in your Users/Home/Library/Application Support/iWeb folder.
    To open your domain file in Lion or to switch between multiple domain files Cyclosaurus has provided us with the following script that you can make into an Applescript application with Script Editor. Open Script Editor, copy and paste the script below into Script Editor's window and save as an application.
    do shell script "/usr/bin/defaults write com.apple.iWeb iWebDefaultsDocumentPath -boolean no"delay 1
    tell application "iWeb" to activate
    You can download an already compiled version with this link: iWeb Switch Domain.
    Just launch the application, find and select the domain file you want to open and it will open with iWeb. It modifies the iWeb preference file each time it's launched so one can switch between domain files.
    WARNING: iWeb Switch Domain will overwrite an existing Domain.sites2 file if you select to create a new domain in the same folder.  So rename your domain files once they've been created to something other than the default name.
    OT

  • How do I add data to an existing file, without having to create a new one?

    Hi,
    I made a servlet which is supposed to write the word "new" in a file everytime the server creates an instance:
    File chatInfos = new File("LOCATION_OF_THE_FILE/infos.dat");
       if(chatInfos.exists()) {
        try {
        FileOutputStream fos = new FileOutputStream(chatInfos);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject("new");
        os.close();
        fos.close();
        } catch(IOException e){};
       }But it doesnt work. It seems like that every time the old file is destroyed and a new one is created. But I dont want that. I simply want to add a new object to the file.
    thanx

    http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File, boolean)
    Kind regards,
    Levi

  • How can I extract part of a PDF file and copy it to a new PDF file?

    How can I extract part of a PDF file and copy it to another PDF file?

    You will need Adobe Acrobat for this.

  • How to transfer text files on my MacBookAir to my new iPad mini

    I have managed to transfer pictures over to the iPad mini using usb cable but is it possible to transfer files from my documents?

    I have managed to transfer pictures over to the iPad mini using usb cable but is it possible to transfer files from my documents?

  • How to transfert notepad text files from my computer to my new ipod touch 64g

    see first question

    snarfman123 wrote:
    Ok..got it thanks! But, that leads me to my next question. I have over 140GB of music on an external hard drive that I want in my library. I know I can set the media folder to that external drive, but when I add the songs to my library, it would copy the files into that folder and not just move them meaning I'd have double the files on my drive? Is there a way to set it so it just transfers the files directly to the media folder and keeps them there?
    Drag the file into the "Automatically Add to iTunes" folder, which can be found in your iTunes Media folder.  iTunes will add the file to your library and will immediately move it to an Artist/Album folder and you will end up with only a single copy.  Try it now with one file so you can see how it works, before doing larger batches.

  • Incoming texts going to old phone instead of new one.

    I upgraded from iphone 4 to Nokia Lumia 928. I can call out, receive calls, and send texts on the Nokia, but all incoming text messages are still sent to my iphone. Any ideas on how to fix this? It's not real handy having to carry two phones around.

        Hi there TGuinn!
    I definitely don't want you to have to carry around multiple devices! As Suzy Q mentioned it seems as though your iMessage may still be activated for your phone number. The great news is it's easy to remove. Visit this link: http://vz.to/1aTAJuM sign in with your Apple ID and remove your number from iMessages.
    Please test and let me know if you have any other questions!
    Thanks!
    MelissaM_VZW
    Please follow us on Twitter @VZWSupport

  • How do I transfer files from old computer to the new one

    I have bought the desktop and used migration asiitant to transfer files but nothing came through, how do i transfer files

    Your best option of migrating was when your turned on the new computer for the first time using Setup Assistant. Part of SA is it asks if you are migrating from another Mac, you apparently passed that and missed that opportunity. You then must have used Migration Assistant which is similar to SA but with one critical difference. Migration Assistant migrates everything to a new user account whereas SA would not. Log out of your existing account and into the new account and you will find your stuff. If you want to start over (highly recommended) you can. You will need to carefully read Lion Setup Assistant tips beginning where he discusses Second Chance.... This will take some time but in the long run make your migration more seamless.

  • Is it possible to transfer songs from a separate iTunes account into a new one?

    I have a lot of songs saved on my Dad's mac under his apple ID.  I'd like to transfer the songs I've purchased/imported on his computer onto mine.  Is this possible through possibly the iCloud?  I'd like to avoid having to burn them all to a disc or what have you.

    If the use the same Apple ID/iTunes account they can share the iTunes purchases.
    If the Rahapsody songs are regular, non-DRM/Copy protected the just import them into your iTunes library and then sync

  • Do I need to uninstall old Download manager exe (Akamai?) file from past trial to install new one?

    I just purchased CS5.5 Design Premium (student edition) at Amazon. Everything went smoothly while validating that I am a student. I installed the 1st of 2 disks. Second one won't install.
    The PDF help sheet says to look for DesignPremium_CS5_5_LSx.exe in my program files to extract the files from the second disk, but its not in there.
    Since I purchased this at Amazon, even though I have the correct serial number, I can't re-download the manger.exe file from Adobe because it is not in MyOrders at the Adobe store.
    Perhaps it could be because last semester I downloaded different trials, I think Master Suite 5.5 and maybe Flash 5, but I can't remember for sure.
    When this problem surfaced over the weekend, I went back and tried to uninstall all the trial stuff (they don't work anymore anyway), but still no luck.
    Thanks in advance for any help you can offer

    You can always download the trial and input your serial. Other than that I'm sure there is a way to get your downloads from Amazon, you will probably just have to contact their customer support to unlock it again so it's available from your account there...
    Mylenium

Maybe you are looking for