 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nimmi Srivastav Guest
|
Posted: Sun Jul 20, 2003 3:58 pm Post subject: Problem with FileWriter class |
|
|
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:
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());
}
}
}
|
|
| Back to top |
|
 |
FGB Guest
|
Posted: Sun Jul 20, 2003 6:20 pm Post subject: Re: Problem with FileWriter class |
|
|
Nimmi Srivastav wrote:
| Quote: | 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
|
It worked ok for me. I uncommented the line that creates fileName(s)
and used the a "data file" with the following URLs:
http://java.sun.com
http://groups.google.com
Not sure what is happening for you...
Fred Burkley
|
|
| Back to top |
|
 |
Steve Horsley Guest
|
Posted: Sun Jul 20, 2003 8:57 pm Post subject: Re: Problem with FileWriter class |
|
|
On Sun, 20 Jul 2003 08:58:10 -0700, Nimmi Srivastav wrote:
| Quote: | 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:
snip |
I suspect you have the wrong file name. Instead of using:
System.out.println(fileName);
try using:
System.out.println("fileName = '" + fileName + "'");
This should show up any unexpected characters such as 'r'.
If you want to read a text file, you would be better off treating it as a
text file and using a Reader. This way you will more likely get the
characterset right, and handle line endings better.
Steve
|
|
| Back to top |
|
 |
Nimmi Srivastav Guest
|
Posted: Wed Jul 23, 2003 7:02 pm Post subject: Re: Problem with FileWriter class |
|
|
FGB <au714 (AT) osfn (DOT) org> wrote
| Quote: | Nimmi Srivastav wrote:
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
It worked ok for me. I uncommented the line that creates fileName(s)
and used the a "data file" with the following URLs:
http://java.sun.com
http://groups.google.com
Not sure what is happening for you...
Fred Burkley
|
Since the original posting, I have tried this program on Solaris. It
works fine on Solaris, but on Windows (98 & 2000) I keep getting error
messages. Could someone kindly try it out on Windows and share their
experience please?
--NS
|
|
| Back to top |
|
 |
Sandeep Sharma Guest
|
Posted: Wed Aug 06, 2003 10:56 am Post subject: Re: Problem with FileWriter class |
|
|
[email]nimmi_srivastav (AT) yahoo (DOT) com[/email] (Nimmi Srivastav) wrote in message news:<8b0c42d.0307231102.1482972 (AT) posting (DOT) google.com>...
| Quote: | Since the original posting, I have tried this program on Solaris. It
works fine on Solaris, but on Windows (98 & 2000) I keep getting error
messages. Could someone kindly try it out on Windows and share their
experience please?
--NS
|
Nimmi,
I tried your program on Solaris and Windows, and I ran into similar
problems on Windows. It worked fine on Solaris.
Regards,
Sandeep
|
|
| Back to top |
|
 |
Weichao Wang Guest
|
Posted: Mon Aug 18, 2003 12:36 pm Post subject: Re: Problem with FileWriter class |
|
|
[email]nimmi_srivastav (AT) yahoo (DOT) com[/email] (Nimmi Srivastav) wrote in message news:<8b0c42d.0307200758.64d76213 (AT) posting (DOT) google.com>...
| Quote: | if((c = (char) ret) != 'n')
{
urlAddr.append(c);
continue;
}
|
Replace the code above with the following, it may work on Windows:
if((c = (char) ret) != 'r')
{
urlAddr.append(c);
continue;
} else {
ret = finStm.read(); // skip 'n'
}
The reason is that the line end is 'n' on Unix whereas it is 'rn'
on Windows/DOS.
I have not tested the code (because of firewall?). If someone has tested
it, please post the result here.
Weichao
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Mon Aug 18, 2003 8:05 pm Post subject: Re: Problem with FileWriter class |
|
|
On 18 Aug 2003 05:36:59 -0700, [email]weichao_wang (AT) yahoo (DOT) de[/email] (Weichao Wang)
wrote or quoted :
| Quote: | nimmi_srivastav (AT) yahoo (DOT) com (Nimmi Srivastav) wrote in message news:<8b0c42d.0307200758.64d76213 (AT) posting (DOT) google.com>...
if((c = (char) ret) != 'n')
{
urlAddr.append(c);
continue;
}
|
Check out the PrintWriterPlus class in the hunkio package.
See http://mindprod.com/products.html#HUNKIO
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Thomas Weidenfeller Guest
|
Posted: Tue Aug 19, 2003 5:47 am Post subject: Re: Problem with FileWriter class |
|
|
Roedy Green <roedy (AT) mindprod (DOT) com> writes:
Roedy, if you look at the original, very old posting, you will see that
the problem is not the FileWriter. There are a number of problems in
the original code, including:
- Using an InputStream and not a Reader to read some text file
- Doing line-by-line reading via the InputStream with a home-made EOL
detection that will break on any file not using the Unix convention,
instead of using a LineReader.
- Usage of a DataInput stream for reading from a URL, serving no
apparent purpose other then messing up the input data.
- Improper handling of empty lines (might be an artefact of the
problems with handling EOL).
- Rather strange mixture of using String and StringBuffer, including
converting a StringBuffer to a String and back to a StringBuffer on
the next line, multiple times converting the same StringBuffer (with
the same contents) to a String, etc.
- Improper attempt to convert a URL to a local file name (here the
broken EOL detection can sneak control chars into the name, and some
valid URL chars are not handled at all, like '=', '&', or '%').
And the control char r from the name is why the creation of the
FileWriter breaks.
- Usage of the deprecated DataInputStream.readLine() to read a String.
- Usage of FileWriter to write bytes - which have just been converted
from the String gotten from DataInputStream.readLine() ...
- Manual memory handling: Manually clearing existing objects instead of
just creating new ones.
And I might have missed a few things.
In other words: THAT CODE DESERVES TO BE TAKEN OUT AND SHOT! The
remains have to be treated as toxic waste. Please, leave it alone.
/Thomas
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|