Editing text files

Im trying to write a program that opens many files .def and should replace every / with \.
but I'm encountering problem with the method that has to replace the character in the file.
first attempt:
public void changer(String pathFile){
     try{
          RandomAccessFile toEdit = new RandomAccessFile(new File(pathFile), "rw");
          String line = "";
          while(line != null){
               riga = toEdit.readLine();
               String newLine;
               newLine = line.replaceAll("/", "\\");
               toEdit.writeChars(newLine);
          toEdit.close();
     catch(IOException e){
          System.out.println(e.getMessage);
          System.exit(1);
}this throws an exception java.lang.StringIndexOutOfBoundException: String index out of range: 1
exception in thrown by replaceAll method.
second attempt:
public void changer(String pathFile){
     try{
          RandomAccessFile toEdit = new RandomAccessFile(new File(pathFile), "rw");
          String line = "";
          while(line != null){
               riga = toEdit.readLine();
               String newLine;
               for(int i = 0; i<line.length(); i++){
                    if(riga.charAt(i) == '/'){
                         newLine = line.replace('/', '\\')
                         toEdit.writeChars(nuovaRiga)
                    else
                         continue;
          toEdit.close();
     catch(IOException e){
          System.out.println(e.getMessage);
          System.exit(1);
}this throws an exception java.lang.NullPointerException
thrown by length() method.
problem 2: both method don't replace line with newLine but just write newLine after line in the text.
I've read the api documentation for RandomAccessFile but I haven't found a method that really substitute the line or only the char.
any suggestion?

No, you are never reading a second line, you read into riga, which you ignore.oh, sorry, that was an error in copying/translating my code.
here's the correct one.
public void changer(String pathFile){
     try{
          RandomAccessFile toEdit = new RandomAccessFile(new File(pathFile), "rw");
          String line = "";
          while(line != null){
               line = toEdit.readLine();
               String newLine;
               newLine = line.replaceAll("/", "\\");
               toEdit.writeChars(newLine);
          toEdit.close();
     catch(IOException e){
          System.out.println(e.getMessage);
          System.exit(1);
public void changer(String pathFile){
     try{
          RandomAccessFile toEdit = new RandomAccessFile(new File(pathFile), "rw");
          String line = "";
          while(line != null){
               line = toEdit.readLine();
               String newLine;
               for(int i = 0; i<line.length(); i++){
                    if(line.charAt(i) == '/'){
                         newLine = line.replace('/', '\\')
                         toEdit.writeChars(newLine);
                    else
                         continue;
          toEdit.close();
     catch(IOException e){
          System.out.println(e.getMessage);
          System.exit(1);
}

Similar Messages

  • Remotely editing text files in Windows WinRM/PSExec/Powershell/cmd

    Hi!
    How can I remotely edit a text file in Windows?
    I am running and administering a number of Windows 7 workstations and Windows Server 2012 machines. I would like to be able to remotely edit text files on any of these machines. I can connect to any of these machines via psexec or powershell remoting. I
    can also connect via rdp, but for the workstations I would rather not kick the user off unless I absolutely have to.
    For example, we manage state configuration using
    Salt. Local configuration files are stored in C:/salt/conf. I would like to be able to edit these config files on the fly.
    This question on Stack Overflow covers the same topic, but none of the answers are particularly satisfying. Some of the possible solutions
    are:
    Remote Desktop: as I say, I would rather not have to kick a user off their workstation
    Edit a file over a UNC path: this only works for files on a share, and setting up arbitrary shares for quick edits seems like a bad idea.
    Install some manner of ftp or ssh server on all the target computers. This would certainly work, but it is it necessary with WinRM already active? I feel like installing and configuring extra software across all the machines in my organisation would be
    a last resort.
    Edit with a command line editor (e.g. vim, emacs) over a remote powershell session. This doesn't work. I don't understand the technical details, but a powershell remote session isn't interactive in the same way that ssh connections are.
    Edit using powershell's -replace operator, out-file and add-content cmdlets. This works, but is hardly the same as a fully featured text editor.
    Retrieve content from the remote, edit locally, and post back to the remote. This works, and is arguably the best solution I've found. Code sample from SO:
    PS C:\Users\Meredith> Invoke-Command -Session $ps -ScriptBlock {get-content c:/inetpub/myapp/web.config} > web.config
    edit web config
    PS C:\Users\Meredith> get-content web.config | Invoke-Command -Session $ps -ScriptBlock {set-content c:/inetpub/myapp/web.config}
    The last two options are the workflow that I've been using for now. Regex replacement for very simple changes, and copying to local and editing there for more complicated ones. Neither is ideal, but they work. It just feels incredibly old fashioned.
    I'm considering writing a plugin to my editor of choice (vim) to perform the remote fetch and save, to make my workflow a little bit smoother. Before I try that, I just want to know if there's anything that I've missed, or misunderstood.
    Cheers

    What is the "redirector"?
    I posted the same question to Stack Overflow, and was told about administrative shares. I wasn't aware of this, and it's exactly what I wanted. I can open the filetree of a remote machine with the path \\machinename\c$\. Perfectly simple, now that I know
    the answer.

  • Editing text file on application server

    Hello,
    I have created a utility in which I can move files from one folder to another. Now we also need a option by which I can edit text file on the server folders. Is there a function module by which I can edit text from application server ?
    Regards,
    Rajesh.

    With OPEN DATASET and READ you can load them into an internal table. Next put them on screen in lets say the text editor (enjoy SAP Controls). This is one way of doing this, other way would be:
    Upload data with OPEN DATASET into internal table, download file to local drive (cl_gui_frontendservices=>gui_download), open it with notepad (for example) edit it and save. Next upload file into your program (cl_gui_frontendservices=>gui_upload) and download it to server again (OPEN DATASET and TRANSFER).
    If available you can also use transaction CG3Z and CG3Y.

  • Read and Edit Text Files

    I wanted to find out if there is a way to read linux
    configuration files, edit parameters in them and save them using
    coldfusion. I know PHP has the capability to do this, but I don't
    know if CF does and how powerful it is.

    If it's plain text, just write it into a textarea and let
    your users edit it there. When they submit, use cffile to overwrite
    the text file.
    There are other ways to allow users to edit text in a web
    app, such as the "contenteditable" attribute in IE, but the basic
    idea remains the same.
    Or do you mean that the app will alter the files
    automatically? If that's the case, then regular expressions would
    likely be the way to go.

  • EDITING TEXT FILE

    anyone here know how to edit a text file
    i can open the file and i try println it adds to the already content of the file
    i was thinking of EDITING THE FILE
    i mean changing some parts of the files
    example i could always change line #2 if it contains data about AGE
    somehing like that

    i was thinking of EDITING THE FILE
    i mean changing some parts of the files
    example i could always change line #2 if it contains
    data about AGE
    somehing like thatThe easiest way is to read the whole file, line by line, and store the lines in a String array. Then change the lines in the array. Finally, overwrite the file with the lines in the array.
    Unfortunately, there is a slight problem with doing that. Suppose you write the first line to your file. That will delete everything in the file. But then suppose your program crashes. Poof! You will lose all your data except the one line you wrote to the file.
    So to be safe, you need to write to a new file, and when you are done, delete the old file and then change the name of the new file to the old file name. Creating File objects for your files will provide the means for doing that.

  • Using pico or nano to edit text file in ssh connection to remote osx host

    Hi everybody!
    I can not get thru to a point when I'll be able to edit files on a remote osx comuter with pico or nano editors. When I connect thru ssh and start pico it says
    Error opening terminal: network
    I am a newbie to Unix beast. Vi is too complex for me.
    Thanks,
    Maxim
    PowerBook G4 1GHz 12 40GB SD   Mac OS X (10.4.4)  
    PowerBook G4 1GHz 12 40GB SD   Mac OS X (10.4.4)  

    Maxim,
    The problem appears to be the value of your environment variable TERM. It seems to be set to network. It needs to be set to something that pico and nano will understand. Since I do not know what your shell is I need to give you two methods of setting the TERM variable. One of the methods will fail as it is not appropriate for the shell you are using. If it does, don't worry about it just try the other method.
    The first method has two commands. Note: capitalization is significant here. They are:
    TERM=vt100
    export TERM
    If those commands give an error message, use this command:
    setenv TERM vt100
    You should now be able to edit your files.
    I am using a terminal type of vt100 as it is about the simplest terminal type and is supported by just about every unix you might be using to connect to the mac as you do not provide that information.
    Hope this information helps.
    Curt

  • How to edit Text Files?

    Hi,
    I have a text file with sample data like this.
    Nokia|25
    Motorola|30
    LG|20
    Samsung|50
    Sony|25
    I will get an argument like ‘Nokia’. I need to compare it with the data in the file. If I find a matching record in the file, I need to decrement the quantity, i.e 25 in this case(Nokia) and display a message in the screen.
    Can someone tell me how this can be achieved?
    Thanks.

    Thanks for your reply. You have mentioned "When an update happens flush the list of items back to the file. ". If I am going to write it back to the same file, it will get appended right?. I need to update the file. Do you mean to say that i delete the existing file and re-create it and then flush the contents into the file?
    I have just started coding in Java. So if incase you feel my question is absurd, please excuse me.

  • Help about editing text file by QT

    Hi again
    I have faced another problem today >> which is so bad
    see this screenshot to understand the problem : http://qkpic.com/c28fb
    Has anybody an idea about how to solve that ?
    This is a quick review about how to do it : http://www.youtube.com/watch?v=6PS2FOjRCgk
    But it dznt work with me at all !
    i tried it in both ".txt" & "png" files >> it dznt work
    Please if anybody has a solution for this problem ,, i would be grateful a lot
    Thanks in advance
    sc0rpio

    Jon Walker wrote:
    My guess would be, if you tried using a PNG image and this work flow failed, that you neglected to export the PNG file with an alpha channel. You must either create the image file on a transparent background or designate what area of the image is to be made transparent before exporting with the alpha channel.pecifics for doing this depend on the graphic application you use. If using an application like Photoshop, create the image as you normally would and then go to the help menu and follow instructions for exporting a transparent image. Either an PNG or GIF will work.
    @Jon Walker
    Thanks for the reply
    You are right if i am using a picture ,, but let me remind you that i am using a subtitle file ".txt"
    which i opened by Quicktime to merge it with the movie file to get a translated movie.
    Anyway ,, i have converted the subtitle file to a ".mov" file so that i can now open it by photoshop
    it's opened as layers ( about 50 layer ) and i am trying now to edit the first layer and apply then to
    all ,, i hope that it works.
    See this screenshot : http://qkpic.com/32ee6
    i believe that it can be solved directly just by using QT >> i feel there is a simply way

  • Leafpad wipes content of text files on smb when saving

    ( 3.19.3-3-ARCH x86_64 +  Mate desktop )
    Here is the problem:
    Today I used leafpad (0.8.18.1-4) to put some short text on another computer on LAN (smb shares) on existing text file.
    I saved text and exit. But when that text is opened again (with leafpad or any text editor), there is no text at all! Leafpad wiped it out on saving.
    Editing text files on my own computer works ok.
    Then I installed mousepad, and it (mousepad) works ok with creating or editing text files over LAN computers.
    I also have Arch Linux + xfce on virtualbox, last updated 10/2014, so I checked if it have something with update.
    Leafpad there worked (regarding network shared text files) ok.
    When I updated that system today - leafpad starts to misbehave just like on my (real) computer.
    I'm now using mousepad, but I'm curious what is going on.

    OK, i did it, here.

  • How to edit a text file inside jar

    Hi all;
    I have a code that created text file and put this file to other jar archive.
    How can I edit this text file inside jar, add string to this file ?
    Thanks,

    Unpack the jar, edit the file, repack the jar.

  • Editing text in pdf file

    I just bought Adobe Acrobat and was told that I would be able edit text in a pdf file I open. However I can't figure out how to do this?
    I can select the text but can't edit/change it?

    The Adobe Reader for SymbianOS isn't a PDF editor. You'd need Acrobat
    for that, using the Text Touch-up Tool.
    Aandi Inston

  • I can´t edit the text files with SAP BODS 3.2

    Hello
    I can not edit some text files 9-20 megabytes to replace two words in each file, the source is a text file and the same destination but with different name, because with the same name have been unable . My idea is to remove the original and stay with the new performing edition but that if he does , let me file this way:
    SALES_ORDERS_FULL_20140223_150051_006.xml ( ORIGINAL )
    should leave it with me so : SALES_ORDERS_FULL_20140223_150051_006.xml _OLD (AMENDED )
    but strangely leaves me so :
    MICROSOFT_SQL_SERVER_KCC-BI_REPO_KHSA_XI_REPO_KHSA_XI_1543_5434_2_3_Long_INVOICES_INCREMENTAL_20140225_034004_001_xml_dat.txt  (not deseased)
    The type of data you use is long since varchar not allow me because it cuts my data file , use varchar (50000000) but takes the information and short .
    I am using long data type , modify the file and I pass the name of the text files of sap bods for global variables. They are a series of files that are called by a while loop
    Thanks for your answers
    regards

    HIr
    Yes, first I convert long_to_varchar for edit, and convert varchar_to_long.
    I edit before before and later
    Thanks

  • Editing text in Photoshop CS3 file causes crash. File originally created in Photoshop CS6

    Trying to edit text in Photoshop CS3 file causes a crash - Tried on multiple PC's
    File originally created in Photoshop CS6 with "Maximise PSD/PSB File Compatibility" set at "Always".
    Document size A4, using Arial Regular. RGB. 8Bits. 72dpi
    Colour Profile: sRGB IEC61966
    Multi business licence for CS3 and CS6 Design Standard/Premium/Master Collection all on PC
    Please Help!

    Interestingly I have found that if I modify a document in CS6 from an earlier CS3-CS5 document and save it - it works fine. But if I create a new document in CS6 the issue persists.
    Looks like I will be modifying old documents for the time being
    Would be grateful for some advise eon this.

  • I need to edit text in a jpeg file.  How do I do that?

    I need to edit text in a jpeg file.  How do I do that?

    Unfortunately you can't. A JPEG is a flat raster (pixel-based) image file which has no awareness of "text" inside it. You could try to erase the text and graphic under it and then recreate the text, but it's best to do so using a mastering format such as PSD (Photoshop) or PXM (Pixelmator), so that you can export as JPEG but keep your layered master for later editing.
    Matt

  • How Do I Edit a Text File in PowerShell??

    How do i edit the text (or paragraphs) in text files using PowerShell?
    Thak You

    Hi,
    That's a pretty broad question, do you have something specific you're trying to accomplish?
    As one example though, this will create an input file (you could just use one you already have, but this is a fully contained example), read it, replace the word 'text' with the word 'fun', and write an output file:
    $exampleFile = @"
    This is some text.
    This is some more text.
    This is even more text.
    $exampleFile | Out-File .\input.txt
    (Get-Content .\input.txt ).Replace('text','fun') | Out-File .\output.txt
    Don't retire TechNet! -
    (Don't give up yet - 12,950+ strong and growing)

Maybe you are looking for