JEdit Macro - open and save file

I have a JEdit (BeanShell) macro that opens a specific file and then saves the file to the c: \ temp folder (so I don't accidentally update the real file).

Here is the wrapper bean code:

logFilePath = "c:\\temp\\aj.txt";
jEdit.openFile( view , logFilePath );
_buffer = jEdit.getBuffer(logFilePath);
_buffer.save(view,"c:\\temp\\backup.txt",true);

      

This gives me the following error:

I/O Error
Each buffer can only execute one input/output operation at a time.  
Please wait until the current operation finishes 
(or abort it in the I/O progress monitor) before starting another one.  

      

I tried adding a while loop to wait for buffer.isLoaded()

true, but that just goes into an infinite loop.
Seems to work, a message box ( Macros.message

) appears . However, I really don't want to have this unnecessary dialogue.

I don't know much java, so please tell me if I'm making a rookie mistake.

Update:

Added my own answer to show the code pointed to by Sergius' answer .

+1


source to share


3 answers


You can try this solution by calling VFSManager.waitForRequests();

.



+4


source


Works

This is the code pointed to by Sergius' answer above.

Add VFSManager.waitForRequests();

after the command jEdit.openFile()

.



Complete code

logFilePath = "c:\\temp\\aj.txt";
jEdit.openFile( view , logFilePath );

VFSManager.waitForRequests();

/* 
    VFSManager.waitForRequests();

    jEdit waits then for the file to be completely loaded before continuing 
    ... It designed for waiting on all 'pending I/O requests'".
*/

_buffer = jEdit.getBuffer(logFilePath);
_buffer.save(view,"c:\\temp\\backup.txt",true);

      

+3


source


You can also make it less greasy.

  • use the return value jEdit.openFile (), it's already Buffer, no need for getBuffer ()
  • Don't call VFSManager.waitForRequests (), which waits for ALL requests to complete, but just add the BufferListener to the buffer you received from jEdit.openFile () and make your save call in the bufferLoaded method of that listener :)
0


source







All Articles