4

I have some doubts about the way to read file from a remote (inside our network) location with java. I'm developing a simple java application from a windows machine and I'm able to reach a remote file that resides on a unix machine in this way:

File fileToRead=new File(new URI(file:////192.168.0.27/export/myFile.txt))

With the same application my collegue that is using kubuntu to develop is not able to reach the file. I get FileNotFoundException.

What can be the problem?

UPDATE 1 I would want to use jcfis to solve my problem, but in this cas the application will work both on windows than on linux?

Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • First, probably "file:" is not correct. If the file is remote, then you need "ftp:" or "http:" or some other protocol. Second, I'd guess you also need a `Socket` class, not `File`. – markspace Jun 11 '15 at 15:32
  • 2
    Windows is probably defaulting to using Samba/CIFS to access the file, while kubuntu has no handler for the `file://` protocol. – Jonathon Reinhart Jun 11 '15 at 15:33
  • see this question about file: URLs: http://superuser.com/questions/352133/why-do-file-urls-start-with-3-slashes – Remigius Stalder Jun 11 '15 at 15:41

3 Answers3

3

I solved using a jcifs library in the following way

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

This works in both developing environment (Windows and Linux)

Skizzo
  • 2,883
  • 8
  • 52
  • 99
2

EDIT: The OP has specified which protocol he would want to use. This answer fails to use that said protocol so it is not valid anymore.

Use a URL object instead:

URL url = new URL("http://someaddress.com/somefile.txt");

With this URL, you can open a InputStream:

InputStream is = url.openStream();

Which you can read with a BufferedReader

BufferedReader br = new BufferedReader(new InputStreamReader(is));

EDIT: This will work fine assuming HTTP can be used to download the file.

Full code:

URL url = new URL("http://someaddress.com");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// You can read lines like this
String line = br.readLine();
Community
  • 1
  • 1
AlphSpirit
  • 148
  • 1
  • 10
  • This presupposes that http is a workable protocol for opening this file. It very well may not be. – Ian McLaird Jun 11 '15 at 15:43
  • In my case URL url = new URL("http://someaddress.com") could become URL url = new URL("file://192.168.0.27/export/myFile.txt)")? – Skizzo Jun 11 '15 at 15:46
  • @IanMcLaird I've added your comment to the post. The OP specified no protocol in particular so I made a assumption. – AlphSpirit Jun 11 '15 at 15:46
  • @Skizzo Try it, but you will need to tell us more about what protocol you can use to transfer the file. If you have none, then good luck. – AlphSpirit Jun 11 '15 at 15:47
0

If you are using FTP or HTTP you can directly use @AlphSpirit's answer. Otherwise you can use the java.net.Socket and implement your own protocol (which is easier than using FTP or HTTP, you will need a java.net.ServerSocket running on the other machine).

Edit: You said you would like to use JCFIS as it works both on Windows and Linux, but the JRE does too.

nom
  • 256
  • 3
  • 16