Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method to locally access files with JCIFS is as follows:

  1. Create a new SmbFile object with the file path as the parameter.

  2. Use the SmbFile object to perform file operations such as read, write or delete.

  3. Handle any exceptions that may be thrown by the operation.

Example:

import jcifs.smb.*;

public class SmbFileExample {

    public static void main(String[] args) {

        try {
            // Create a new SmbFile object for the file path
            SmbFile file = new SmbFile("smb://localhost/share/file.txt");

            // Read the contents of the file
            byte[] buffer = new byte[(int) file.length()];
            SmbFileInputStream in = new SmbFileInputStream(file);
            in.read(buffer);
            in.close();
            String contents = new String(buffer, "UTF-8");
            System.out.println("File contents: " + contents);

            // Write to the file
            SmbFileOutputStream out = new SmbFileOutputStream(file);
            String data = "New file contents";
            out.write(data.getBytes("UTF-8"));
            out.close();

            // Delete the file
            file.delete();

        } catch (SmbException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}