0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
m d
  • 1
  • 1

1 Answers1

1

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.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99