saher saher - 1 year ago 71
Java Question

How to use '//' in java while creating file

  1. String testpath1 = "smb://host_address/File_Folder";

  2. File testFile1 = new File(testpath1 + fi.getName());

  3. fi.write(testFile1);

This code in Java to read and write file in Linux server. However, after creating file(at line 2), file path becomes

I want the file path as

Please help!

Answer Source

Java File class converts the pathname to an abstract pathname depending on the environment it is running it. . An abstract pathname has two components:

  1. An optional system dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or"\\" for a Microsoft Windows UNC path
  2. A sequence of zero or more string names

I guess Java might be using the abstract pathname of Unix based system in your case resulting in the lose of one /

You need to use SmbFile class to open a SmbFile SmbFile file = new SmbFile(testpath1 + fi.getName()) You can see SmbFile doc and Java File for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download