无法从另一台机器读取文本文件

时间:2012-04-04 06:41:18

标签: java

我无法读取另一台具有不同IP的计算机中的文本文件。     以下是我的代码。请看看..

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

2 个答案:

答案 0 :(得分:1)

请检查您传递的网址new URL("http://10.128.82.93/d:/kiranshare/testout.txt");

我认为它应该像new URL("\\10.128.82.93\kiranshare\testout.txt");

如果文件托管在Web服务器上,请尝试从浏览器首先打开它,看看链接是否正确。

答案 1 :(得分:1)

您不应使用HTTP协议和URL类。共享文件夹并直接使用共享文件夹路径使用File类读取文件。

例如你可以说

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

然后您可以使用BufferedReader来读取文件。确保您有足够的权限来读取该文件。

相关问题