如何在 OBEX 中使用 FTP 通过蓝牙 java 代码删除文件并将其复制到目标设备?

发布于 12-21 16:44 字数 113 浏览 6 评论 0原文

我需要通过蓝牙在目标设备中将文件替换为旧版本。 我知道为此需要使用 OBEX(FTP 和 OPP)配置文件。 但我不知道如何删除旧版本并在目标目录中复制新版本的文件(java代码)。

你能帮我吗?

I need to replace a file with old version of it in destination device via bluetooth.
I know that OBEX(FTP and OPP) profiles is necessary to use for this.
But I don't know How can delete old version and copy new version of file in destination directory (java code).

Can you help me please?

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

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

发布评论

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

评论(1

梦情居士2024-12-28 16:44:40

要对文件执行操作,您首先需要更改到文件所在的目录。
例如,如果您需要访问 /root/directory/subdir/
你应该调用setPath 3次

    setPath(""); // to get to /root/
    setPath("directory") // get to /root/directory/
    setPath("subdir") // get to root/directory/subdir/

下面所有的代码都是针对J2ME的
我使用此方法来设置带分隔符的路径(例如/root/dir/)。

    private void moveToDirectory(String dir) throws IOException {
        RE r = new RE("/"); // where RE is me.regexp.RE
        setDir("");
        String[] dirs = r.split(dir);
        for (int i = 1; i < dirs.length; i++) setDir(dirs[i]);
    }

要删除文件,您应该在其上打开 PUT 操作并关闭它,或者使用 ClientSession 中的删除方法。

    public void delete() throws IOException {
        HeaderSet hs = cs.createHeaderSet(); // where cs is an opened ClientSession
        hs.setHeader(HeaderSet.NAME, file); // file - is a filename String, no slashes should be used
        cs.delete(hs);
    }

如果您需要替换文件,您可能不需要调用删除方法,只需打开 OutputStream 并向其写入一个新文件,

    public OutputStream openOutputStream() throws IOException {
        HeaderSet hs = cs.createHeaderSet();
        hs.setHeader(HeaderSet.NAME, file);
        Operation op = cs.put(hs); // Operation should be global, so you can close it after you done
        return op.openOutputStream();
    }

记住在完成流后关闭操作。

To perform operations on files you whould firstly change to the directory where the file is.
For exampe, if you need to get to /root/directory/subdir/
you should call setPath three times

    setPath(""); // to get to /root/
    setPath("directory") // get to /root/directory/
    setPath("subdir") // get to root/directory/subdir/

All the code written below is for J2ME
I use this method to set the path with separators (e.g. /root/dir/)

    private void moveToDirectory(String dir) throws IOException {
        RE r = new RE("/"); // where RE is me.regexp.RE
        setDir("");
        String[] dirs = r.split(dir);
        for (int i = 1; i < dirs.length; i++) setDir(dirs[i]);
    }

To delete a file you should open a PUT operation on it and close it, or use the delete method in ClientSession.

    public void delete() throws IOException {
        HeaderSet hs = cs.createHeaderSet(); // where cs is an opened ClientSession
        hs.setHeader(HeaderSet.NAME, file); // file - is a filename String, no slashes should be used
        cs.delete(hs);
    }

If you need to replace a file you probably don't need to call delete method, just open OutputStream and write to it a new one

    public OutputStream openOutputStream() throws IOException {
        HeaderSet hs = cs.createHeaderSet();
        hs.setHeader(HeaderSet.NAME, file);
        Operation op = cs.put(hs); // Operation should be global, so you can close it after you done
        return op.openOutputStream();
    }

remember to close Operation after you done with the streams.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文