Tim
Tim

Reputation: 4274

Write to a windows network share from unix

The following code works like a charm in eclipse under windows:

public static void main(String[] args) 
{
    try
    {
        String filePath = "\\\\myserver\\dir";
        String fileName = "myFile.txt";
        FileWriter myFileWriter = new FileWriter(filePath + File.separator + fileName); 
        BufferedWriter myBufferedWriter = new BufferedWriter(myFileWriter);
        myBufferedWriter.write("test");
        myBufferedWriter.close();       
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

Now I want to run this code from a unix machine in the same network. The program runs, but does not write my file or throws an exception. Any ides ?

Cheers

Upvotes: 0

Views: 2170

Answers (2)

Moh-Aw
Moh-Aw

Reputation: 3008

If that destination unix machine has Samba installed you might want to try the following library:

http://jcifs.samba.org/

You would need a username and password though.

try {
        String filePath = "myserver/dir";
        String fileName = "myFile.txt";
        String user = "username";
        String password = "password";
        // URL: smb://user:passwd@host/share/filname
        SmbFileOutputStream out = new SmbFileOutputStream("smb://" + user + ":" + password + "@" + filePath
                + File.separator + fileName);
        out.write("test".getBytes());
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This would also work with a windows machine as the destination if the server is configured as an SMB server.

Upvotes: 1

RaceBase
RaceBase

Reputation: 18848

Because in Unix/Linux this is not the right path

String filePath = "\\\\myserver\\dir";

I suggest to check such path exist, and 99% chances you will not have permission to create them. It would be more or less

String filePath = "/usr/xx/";

Creating folder:

File temp = new File("temp");
boolean test = temp.mkDir();

Upvotes: 0

Related Questions