Reading a file from a remote location using java

I have some doubts about the way to read a file from a remote (within our network) location using java. I am developing a simple java application from a Windows machine and I can get a remote file that is on a unix machine like this:

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

      

In the same application, a colleague of mine who uses kubuntu for development cannot get to the file. I am getting FileNotFoundException.

What could be the problem?

UPDATE 1 I would like to use jcfis to solve my problem, but in this case the application will work more like on windows than on Linux?

+3


source to share


3 answers


I decided to use jcifs library as follows

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

      



This works in both development environments (Windows and Linux)

+2


source


EDIT: The OP has figured out which protocol he will want to use. This answer does not use this protocol, so it is no longer valid.

Use a URL object instead:

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

      

With this url, you can open an InputStream:

InputStream is = url.openStream();

      



What can you read with BufferedReader

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

      

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

Complete 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();

      

+1


source


If you are using FTP or HTTP you can use @AlphSpirit's answer directly. Otherwise, you can use java.net.Socket

and implement your own protocol (which is easier than using FTP or HTTP, you will need java.net.ServerSocket

one running on a different computer).

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

0


source







All Articles