JEdi​​t 宏 - 打开和保存文件

发布于 2024-07-09 04:41:59 字数 842 浏览 7 评论 0原文

我有一个 JEdit (BeanShell) 宏,它打开一个特定文件,然后立即将该文件保存到我的 c:\temp 文件夹中(这样我就不会意外更新真实文件)。

这是 bean shell 代码:

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

这给了我以下错误:

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.  

我尝试添加一个 while 循环来等待 buffer.isLoaded() 为 true,但这只是进入无限循环。
似乎有效的是弹出一个消息框(Macros.message)。 不过,我真的不想进行这种不必要的对话。

我不太懂java,所以请告诉我我是否犯了菜鸟错误。

更新:

添加了我自己的答案,以显示 Serhii 的答案指向的代码。

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

Here is the bean shell 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 have tried adding a while loop to wait until buffer.isLoaded() is true, but that just goes into an infinite loop.
What does seem to work is popping up a message box (Macros.message). 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 from Serhii's answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-07-16 04:41:59

您可以尝试此解决方案,调用VFSManager.waitForRequests() ;

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

话少情深 2024-07-16 04:41:59

这是有效的

这是上面 Serhii 的答案 指向的代码。

添加VFSManager.waitForRequests();jEdit.openFile() 命令之后。

完整代码

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's designed for waiting on all 'pending I/O requests'".
*/

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

This Works

This is the code pointed to by Serhii's answer, above.

Add VFSManager.waitForRequests(); after the jEdit.openFile() command.

Full 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's designed for waiting on all 'pending I/O requests'".
*/

_buffer = jEdit.getBuffer(logFilePath);
_buffer.save(view,"c:\\temp\\backup.txt",true);
梦里泪两行 2024-07-16 04:41:59

你也可以不那么大胆。

  1. 使用 jEdit.openFile() 的返回值,这已经是 Buffer,不需要 getBuffer()
  2. 不要调用 VFSManager.waitForRequests() 等待所有请求完成,而只需将 BufferListener 添加到您获得的 Buffer来自 jEdit.openFile() 并在此侦听器的 bufferLoaded 方法中进行保存调用:-)

You can also do it less bold.

  1. use the return value of jEdit.openFile(), this is already the Buffer, no need for getBuffer()
  2. Do not call VFSManager.waitForRequests() which waits for ALL requests to be done, but simply add a BufferListener to the Buffer you got from jEdit.openFile() and do your save call in the bufferLoaded method of this listener :-)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文