This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem to access the remote file without socket  RSS feed

 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi all,

I m getting the following exceptions when i m connect with URL class . I want to access remote file without socket it means through shared directory . I 've write permission to the shared directory as well . But still i m getting exceptions
in the following code

import java.net.*;
import java.io.*;

public class URLReader {
public static void main(String[] args) throws Exception {
URL h = new URL("\\\\192.168.0.3\\save data\\ahmed\\hello.txt.txt");
System.out.println(" yes :"+h);
BufferedReader in = new BufferedReader(new InputStreamReader(h.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Output:
Exception in thread "main" java.net.MalformedURLException: no protocol: \\192.168.0.3\save data\ahmed\hello.txt.txt
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at URLReader.main(URLReader.java:6)

Please help me it is very urgent
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All URLs need a protocol; for file access that's "file:". Any particular reason you're not using a FileInputStream?
 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please tell me line of code for FileInputStream ..

 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I 've used "file:" as well ... I new to java... please tell me piece of code

C:\Till_now>java URLReader
yes :file:/192.168.0.3/save data/ahmed/hello.txt
Exception in thread "main" java.io.FileNotFoundException: \192.168.0.3\save data
\ahmed\hello.txt (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown So
urce)
at java.net.URL.openStream(Unknown Source)
at URLReader.main(URLReader.java:8)
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skip java.net.URL and use java.io.File:
java.io.File is able to handle files and folders on shares as long as they are accessible for the user executing the Java application.
 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still getting error please help me out ..

yes :\\192.168.0.3\save data\ahmed\hello.txt
Exception in thread "main" java.io.FileNotFoundException: \\192.168.0.3\save dat
a\ahmed\hello.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at URLReader.main(URLReader.java:11)
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the file actually exist? What happens if you enter that path in a Windows Explorer address bar, for instance?
 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Got it, Thanks to all - very happy .. I m giving .txt.txt then only it is working ....




 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!