-1
public static void main(String[] args) throws IOException, URISyntaxException {

    String ipaddress = "10.20.220.187";
    String folderpath = "C:\\Users\\Administrator\\Desktop\\KS_testData\\";
    String fileName = "test.txt";

    String inputFileName = "file:\\\\"+ipaddress+"\\"+folderpath+fileName;

    System.out.println(" inputFileName "+inputFileName);

    File file = new File(inputFileName);
    FileReader inputFileReader = new FileReader(file);
    BufferedReader inputStream = new BufferedReader(inputFileReader);

    String inLine = null;
    while ((inLine = inputStream.readLine()) != null) {
        System.out.println(inLine);
    }
}

Here Im trying to connect to provided ipaddress and read file on it, The specified folder name is a Shared Folder and Im getting FileNotFoundException. Can anyone suggest what Im doing wrong.

Output received is :

    inputFileName file:\\10.20.220.187\C:\Users\Administrator\Desktop\KS_testData\test.txt
Exception in thread "main" java.io.FileNotFoundException: file:\10.20.220.187\C:\Users\Administrator\Desktop\KS_testData\test.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileReader.<init>(FileReader.java:72)
    at com.sv.ppsdemos.remote.RemoteReadAndWrite.main(RemoteReadAndWrite.java:22)
KDS
  • 43
  • 2
  • 7
  • If you paste `file:\10.20.220.187\C:\Users\Administrator\Desktop\KS_testData\test.txt` into your file explorer, does the file exist (with this exact path)? – achAmháin May 22 '18 at 07:50
  • You can not read a file from a different computer using `file:\\`. This might help you https://stackoverflow.com/questions/2011264/how-to-read-a-file-from-remote-system-using-java?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – RP- May 22 '18 at 07:53
  • @notyou file doesnt not appear in explorer. My dev machine and Remote VM machine both are Windows. – KDS May 22 '18 at 11:48

2 Answers2

0
String inputFileName = "file:\\\\"+ipaddress+"\\"+folderpath+fileName;

The problem is here. This is not a filename, it is a URL.

Get rid of the file: part.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Try with this jcifs library

SmbFile fileToRead= new SmbFile(smb://192.168.0.27/export/myFile.txt);

Hope this would help,

Kovacic
  • 1,473
  • 6
  • 21