Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

java - unable to read a text file from another machine

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("http://10.128.0.1/d:/kiranshare/testout.txt");


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


Please suggest me where I am doing wrong???

Thanks in Advance
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please check the url that you are passing new URL("http://10.128.82.93/d:/kiranshare/testout.txt");

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...