N
Nimmi Srivastav
Brief:- I am having trouble getting a FileWriter to write to a
non-existent file if I instantiate the FileWriter with a String that
is manipulated at run time. It works fine with a hard-coded char
array. The error that I get in the former case is:
(The specified path is invalid)tion
Detailed:- This is my first serious foray into Java ----- I recently
completed the Hello World program ----- so my apologies if I am not
doing things the right way (which, I am sure is the case).
I am trying to write a Java application that reads a list of URLs from
a file and accesses each URL and stores it associated web-page locally
on the file system. The filename associated with each webpage is the
same as its URL except that the following characters are replaced by
the '^' character:
\
/
:
*
?
<|
I am not getting any error if I uncomment the line that says
//[UNCOMMENT ME] and comment the immediately preceding line (which
says [COMMENT ME]).
I was also able to successfully get the application to work by
maintaining an array of hard-coded strings and indexing into that
array for each iteration of the main loop.
Here's the program listing. Again my apologies for not doing things
the "right" way:
=======================Program Listing begin ==================
import java.net.*;
import java.io.*;
public class URLDownloader
{
public static void main(String[] args) throws Exception
{
if(args.length == 0)
{
System.out.println("usage: <this-utility> <filename>");
System.out.println("Exiting.....");
return;
}
File inputFile = new File(args[0]);
FileInputStream finStm = new FileInputStream(inputFile);
int ret;
StringBuffer urlAddr = new StringBuffer("");
char c;
DataInputStream inStm = null;
try
{
while ((ret = finStm.read()) != -1)
{
if((c = (char) ret) != '\n')
{
urlAddr.append(c);
continue;
}
System.out.println(urlAddr);
int len = urlAddr.length();
if(len <= 1)
continue;
URL thisURL = new URL(urlAddr.toString());
try
{
inStm = new DataInputStream(thisURL.openStream());
}
catch (Exception xcpn)
{
// FileNotFoundException, ProtocolException,
ConnectionException
urlAddr.delete(0, len);
System.out.println("Error: " + xcpn.toString());
continue;
}
String urlAddrStr = new String(urlAddr);
StringBuffer urlFile = new StringBuffer(urlAddrStr);
for(int i=0; i < urlFile.length(); i++)
{
char ch = urlFile.charAt(i);
switch(ch)
{
case '/':
case '\\':
case ':':
case '*':
case '?':
case '\"':
case '<':
case '>':
case '|':
urlFile.setCharAt(i, '^');
break;
default:
break;
}
}
String fileName = new String(urlFile);
System.out.println(fileName);
FileWriter fWriter = new FileWriter(fileName); // [COMMENT ME]
//[UNCOMMENT ME] FileWriter fWriter = new
FileWriter("http^^^www.yahoo.com");
String inputLine;
while ((inputLine = inStm.readLine()) != null)
{
char[] bytes = inputLine.toCharArray();
fWriter.write(bytes);
}
fWriter.close();
urlAddr.delete(0, urlAddr.length());
urlFile.delete(0, urlFile.length());
}
}
catch (Exception e)
{
System.out.println("Error: " + e.toString());
}
}
}
non-existent file if I instantiate the FileWriter with a String that
is manipulated at run time. It works fine with a hard-coded char
array. The error that I get in the former case is:
(The specified path is invalid)tion
Detailed:- This is my first serious foray into Java ----- I recently
completed the Hello World program ----- so my apologies if I am not
doing things the right way (which, I am sure is the case).
I am trying to write a Java application that reads a list of URLs from
a file and accesses each URL and stores it associated web-page locally
on the file system. The filename associated with each webpage is the
same as its URL except that the following characters are replaced by
the '^' character:
\
/
:
*
?
<|
I am not getting any error if I uncomment the line that says
//[UNCOMMENT ME] and comment the immediately preceding line (which
says [COMMENT ME]).
I was also able to successfully get the application to work by
maintaining an array of hard-coded strings and indexing into that
array for each iteration of the main loop.
Here's the program listing. Again my apologies for not doing things
the "right" way:
=======================Program Listing begin ==================
import java.net.*;
import java.io.*;
public class URLDownloader
{
public static void main(String[] args) throws Exception
{
if(args.length == 0)
{
System.out.println("usage: <this-utility> <filename>");
System.out.println("Exiting.....");
return;
}
File inputFile = new File(args[0]);
FileInputStream finStm = new FileInputStream(inputFile);
int ret;
StringBuffer urlAddr = new StringBuffer("");
char c;
DataInputStream inStm = null;
try
{
while ((ret = finStm.read()) != -1)
{
if((c = (char) ret) != '\n')
{
urlAddr.append(c);
continue;
}
System.out.println(urlAddr);
int len = urlAddr.length();
if(len <= 1)
continue;
URL thisURL = new URL(urlAddr.toString());
try
{
inStm = new DataInputStream(thisURL.openStream());
}
catch (Exception xcpn)
{
// FileNotFoundException, ProtocolException,
ConnectionException
urlAddr.delete(0, len);
System.out.println("Error: " + xcpn.toString());
continue;
}
String urlAddrStr = new String(urlAddr);
StringBuffer urlFile = new StringBuffer(urlAddrStr);
for(int i=0; i < urlFile.length(); i++)
{
char ch = urlFile.charAt(i);
switch(ch)
{
case '/':
case '\\':
case ':':
case '*':
case '?':
case '\"':
case '<':
case '>':
case '|':
urlFile.setCharAt(i, '^');
break;
default:
break;
}
}
String fileName = new String(urlFile);
System.out.println(fileName);
FileWriter fWriter = new FileWriter(fileName); // [COMMENT ME]
//[UNCOMMENT ME] FileWriter fWriter = new
FileWriter("http^^^www.yahoo.com");
String inputLine;
while ((inputLine = inStm.readLine()) != null)
{
char[] bytes = inputLine.toCharArray();
fWriter.write(bytes);
}
fWriter.close();
urlAddr.delete(0, urlAddr.length());
urlFile.delete(0, urlFile.length());
}
}
catch (Exception e)
{
System.out.println("Error: " + e.toString());
}
}
}