unable to read a text file from another machine

Go To StackoverFlow.com


I am unable to read a text file which is there in another machine with different IP. Below is my code. Please take a look it..

URL url = 
                    new URL("");

                            br = new BufferedReader(new InputStreamReader(is));
            File file=new File(url.getFile());
            //   InputStream is = url.openStream(); 
               ByteArrayOutputStream os = new ByteArrayOutputStream();                  
               byte[] buf = new byte[4096]; 
               int n;                   
               while ((n = is.read(buf)) >= 0)  
                       os.write(buf, 0, n); 
               byte[] data = os.toByteArray(); 
       } catch (MalformedURLException e) { 
       } catch (IOException e) { 

Please suggest me where I am doing wrong???

Thanks in Advance
2012-04-04 06:41
by user1216228
13 questions and none accepted. Please work on that. See how-accept-rate-worksjuergen d 2012-04-04 06:43


Please check the url that you are passing new URL("");

i think it should be something like new URL("\\\kiranshare\testout.txt");

if the file is hosted on a web server , try opening first it from the browser and see if the link is correct.

2012-04-04 06:44
by Akash Yadav
Be careful about backslashes in Java strings - Ray Toal 2012-04-04 06:46
when i tried using new URL("\\kiranshare\testout.txt")below is the error which i gotjava.net.MalformedURLException: no protocol: \\d:\kiranshare\testout.txt at java.net.URL.(URL.java:567) at java.net.URL.(URL.java:464) at java.net.URL.(URL.java:413) at com.hcl.read.FileRead.main(FileRead.java:28 - user1216228 2012-04-04 07:08
you dont need to read that as URL , directly read it as File fl = new File("\\kiranshare\testout.txt") - Akash Yadav 2012-04-04 07:15


You should not use HTTP protocol and URL class. Share the folder and directly use the shared folder path to read the file using File class.

For example you can say

java.io.File myFile = new java.io.File("\\\\\\kiranshare\\testout.txt");

and then you can use BufferedReader to read the file. Make sure that you have sufficient privileges to read that file.

2012-04-04 06:48
by Ravindra Gullapalli
Thanks Ravi...Now its reading text file without any problem..Thank - user1216228 2012-04-04 07:14
Accept / Upvote the answers if they are useful - Ravindra Gullapalli 2012-04-04 07:50