Hi, I am trying to run an applet on a website, but when trying to access a file to write (reading works like a charm), do some wrapping:

try {
FileOutputStream fos = new FileOutputStream("http://mitch9654.zymichost.com/songs.DAT");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }

when I do the "fos = new FileOutputStream("http://mitch9654.zymichost.com/songs.DAT");" thing, it goes kablooie.

My // goes to \, and my other / goes to \

I found that out with

System.out.println(fos.toString());

Thanks,
Mitch

Check it out here: http://mitch9654.zymichost.com/

Recommended Answers

All 22 Replies

it goes kablooie

What does that mean? Please show the full text of any error messages.

FileOutputStream("http://mitch9654.zymichost.com/songs.DAT");"

How can a URL with an http: protocol be a local file?
The API doc for FileOutputStream(String) says:

Creates an output file stream to write to the file with the specified name.

By going kablooie, it inverts all of the slashes !

Well, if the applet reads files from the website, (the view songs button) Why can't it write? If it can read the files that are on the website (local because the applet's html file is in the same directory), why can't I write?

All I want is a writer to a file thats URL contains '/' things


Thanks,
Mitch

When you "write" data to a server, there must be code on the server to read and process
that data. What do you expect to happen if you are able to write something to a server? All websites have servers.

applet reads files from the website

Do you have some code that reads files from a website? If you look at that you might get some ideas how to write. Often classes come in pairs: one named ..Input.. and the other ..Output..

When you "write" data to a server, there must be code on the server to read and process
that data. What do you expect to happen if you are able to write something to a server? All websites have servers.

Do you have some code that reads files from a website? If you look at that you might get some ideas how to write. Often classes come in pairs: one named ..Input.. and the other ..Output..

Weel, it is not so much a server but a website. All of the files (including the html file and the file to be written to and the jar file with the classes are in teh same directory in the website. It isn't really a server.

For reading from the site, I have this:

BufferedReader bf;
BufferedReader bf2;
InputStream in;
InputStream in2;
URL file2 = getDocumentBase();
URL file;
String temp;
int total = 0;
String all = "";


     temp = file2.toString();
     temp = temp.replace("index.html", "songs.DAT");
     temp = temp.replace("Main.html", "songs.DAT");
     
     if(temp.equals(file2.toString()))
         temp += "songs.DAT";
     try {
     file = new URL(temp);
     }
     catch (MalformedURLException e) {
         System.out.println("Malformed URL");
     }
        try{                                                
      in = file.openStream();
      bf = new BufferedReader(new InputStreamReader(in));
      while (temp != null) {                                  //
          temp = bf.readLine();                               //To get the total amount 
                total++;                                      //of lines
            }
      }
    
    catch(IOException e){
      e.printStackTrace();
    }
            try{
            bf.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

      in2 = file.openStream();
      bf2 = new BufferedReader(new InputStreamReader(in2));
for (int i = 0; i < total; i++) {
try {
        temp += bf2.readLine();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
}

System.out.println(temp); //Works!

it is not so much a server but a website

A website can NOT function without code to receive messages and send responses. The name for that code is a server.

To read a file from a server you send an HTTP GET message to the server requesting that it return a file to you. The HTTP protocol defines the messages that are sent and what the responses should be.

A website can NOT function without code to receive messages and send responses. The name for that code is a server.

To read a file from a server you send an HTTP GET message to the server requesting that it return a file to you. The HTTP protocol defines the messages that are sent and what the responses should be.

It is free hosting. check it out here:

www.zymic.com

I could pm you my user and pass if need be

So how can I do my BuffedWriter and all the wrapping without FileOututSream changing my slashes?

Write the slashes correctly for the classes you are giving them to and they shouldn't be changed.

but look at the java console when checking out my website.
I made it show the string that was used when I ran it from an html file on my computer (that doesn't work also, but it runs like a charm in appletviewer.

Here is the html file on my computer java console message:

(In java console)

the url is: /E:/Documents%20and%20Settings/Mitch/My%20Documents/NetBeansProjects/DanceProject/build/songs.DAT
java.io.FileNotFoundException: E:\Documents%20and%20Settings\Mitch\My%20Documents\NetBeansProjects\DanceProject\build\songs.DAT (The system cannot find the path specified)

etc...

Notice how it changes the slashes:

the url is: /E:/Documents%20and%20Settings/Mitch/My%20Documents/NetBeansProjects/DanceProject/build/songs.DAT

to

java.io.FileNotFoundException: E:\Documents%20and%20Settings\Mitch\My%20Documents\NetBeansProjects\DanceProject\build\songs.DAT

Thanks,
Mitch

A URL starts with a protocol like HTTP or FILE
Your URL isn't valid.

File paths use separators between path segements. On Windows the separator is \
What you show is a file path.
You are giving a "path" to class that expects a file path and formats what you've given it according to the rules on that OS. For Windows it uses \

I converted the url to string and did the

string = string.replace("file:", " ");
string = string.trim();

commands.

Also,
it NEEDS to have no "file:" in the url. IF it has a "file:", nothing works. Even appletviewer errors out. If I do my commands above, appletviewer works, but running on an html page makes it change the slashes and "ruin my life" :P

Thanks,
Mitch

Please show the error messages and the code that is generating them.

Please explain your understanding of the syntax of a URL. I think the first part of it is the protocol: http:, ftp:, file: etc. The API doc gives:
<scheme>://<authority><path>?<query>#<fragment>

Please show the error messages and the code that is generating them.

On the html page:

with "file:" included:

the url is: file:/E:/Documents%20and%20Settings/Mitch/My%20Documents/NetBeansProjects/DanceProject/build/songs.DAT
java.io.FileNotFoundException: file:\E:\Documents%20and%20Settings\Mitch\My%20Documents\NetBeansProjects\DanceProject\build\songs.DAT (The filename, directory name, or volume label syntax is incorrect)
	at java.io.FileOutputStream.open(Native Method)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at Root.Panels.AddPanel.filehandle(AddPanel.java:253)
	at Root.Panels.AddPanel.access$100(AddPanel.java:16)
	at Root.Panels.AddPanel$Handler.actionPerformed(AddPanel.java:183)
	at java.awt.TextField.processActionEvent(Unknown Source)
	at java.awt.TextField.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
	at java.io.Writer.<init>(Unknown Source)
	at java.io.OutputStreamWriter.<init>(Unknown Source)
	at Root.Panels.AddPanel.filehandle(AddPanel.java:259)
	at Root.Panels.AddPanel.access$100(AddPanel.java:16)
	at Root.Panels.AddPanel$Handler.actionPerformed(AddPanel.java:183)
	at java.awt.TextField.processActionEvent(Unknown Source)
	at java.awt.TextField.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

without "file:" :

the url is: E:/Documents%20and%20Settings/Mitch/My%20Documents/NetBeansProjects/DanceProject/build/songs.DAT
java.io.FileNotFoundException: E:\Documents%20and%20Settings\Mitch\My%20Documents\NetBeansProjects\DanceProject\build\songs.DAT (The system cannot find the path specified)
	at java.io.FileOutputStream.open(Native Method)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at Root.Panels.AddPanel.filehandle(AddPanel.java:253)
	at Root.Panels.AddPanel.access$100(AddPanel.java:16)
	at Root.Panels.AddPanel$Handler.actionPerformed(AddPanel.java:183)
	at java.awt.TextField.processActionEvent(Unknown Source)
	at java.awt.TextField.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-3" java.lang.NullPointerException
	at java.io.Writer.<init>(Unknown Source)
	at java.io.OutputStreamWriter.<init>(Unknown Source)
	at Root.Panels.AddPanel.filehandle(AddPanel.java:259)
	at Root.Panels.AddPanel.access$100(AddPanel.java:16)
	at Root.Panels.AddPanel$Handler.actionPerformed(AddPanel.java:183)
	at java.awt.TextField.processActionEvent(Unknown Source)
	at java.awt.TextField.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

FileOutputStream.<init>(

That shows the constructor for the FileOutputStream class.
Can you show me the Constructor for the FileOutputStream that takes a URL as an argument?

and the code that is generating them.

Where's the source code for line 253?

Please show the error messages and the code that is generating them.

Please explain your understanding of the syntax of a URL. I think the first part of it is the protocol: http:, ftp:, file: etc. The API doc gives:
<scheme>://<authority><path>?<query>#<fragment>

I don't get it, appletviewer is happier without "file:" attached to it. and besides, if you look here:
http://www.javacoffeebreak.com/faq/faq0004.html
it shows that you don't put in:

fout = new FileOutputStream ("file:myfile.txt");

instead, you put in:

fout = new FileOutputStream ("myfile.txt");

though maybe I can't use FileOutputStream because I am using an applet... Any suggestions for something to access a file for writing?

I may also add that reading the file is fine, but that is because I can just go:

URL file = getDocumentBase();
InputStream in = file.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));

and that keeps the "file:" part still in

I just wish that I could do something like:

URL file = getDocumentBase();
OutputStream out = file.openStream(); //I'd love an OutputStream here, not an InputStream

Thanks,
Mitch

YES!!!!!

It is all of the %20s!!!

I did some:

string.replace("%20", " ");

and it works like a charm!!!!


Thanks,
Mitch

appletviewer is happier without "file:" attached to it

You mean a specific class constructor, not the program running the applet.

ny suggestions for something to access a file for writing?

Where is the applet supposed to write this file? If on the local system, it will need permission. If on the server, there is NO way for an applet to directly write a file on server. The applet can REQUEST the server to write a file. This requires code on the server to accept and validate the request, read the data for the file and then write the file.

You mean a specific class constructor, not the program running the applet.


Where is the applet supposed to write this file? If on the local system, it will need permission. If on the server, there is NO way for an applet to directly write a file on server. The applet can REQUEST the server to write a file. This requires code on the server to accept and validate the request, read the data for the file and then write the file.

Maybe I shouldn't worry about the whole server thing, but I DO need a way to create a File or OutputStream (any type; FileOutputStream, DataOutputStream, anything that implements/extends OutputStream) object without having my '/' slashes changes to '\' slashes...


Please, I'll fix the server problem later.

Thanks,
Mitch

without having my '/' slashes changes to '\' slashes

If you are passing a String as a filepath on a Windows system, the path separators should be \. The Software will change them as needed.
If you pass the String as a URL they shouldn't be changed as \ has no meaning in a URL.

If you are passing a String as a filepath on a Windows system, the path separators should be \. The Software will change them as needed.
If you pass the String as a URL they shouldn't be changed as \ has no meaning in a URL.

What constructer for an OutputStream or its children classes should I use that uses a URL as an arguement?

Also, If my applet is on a webpage, it shouldn't change slashes because the webpage shows as
http://mitch9654.zymichost.com/
in my browser, no
http:\mitch9654.zymichost.com\

Do you have the API doc for Java? That's where to look to see what constructors use URLs.
I don't know the API well enough to answer your question. I'd have to take a look thru the various classes to find the answer. If you do the lookup yourself, you'll learn how to use the API doc and probably find a lot of things that you didn't know that will be useful in the future.
For internet I/O look at the Socket, HttpURLConnection and the URLConnection classes.

Do you have the API doc for Java? That's where to look to see what constructors use URLs.
I don't know the API well enough to answer your question. I'd have to take a look thru the various classes to find the answer. If you do the lookup yourself, you'll learn how to use the API doc and probably find a lot of things that you didn't know that will be useful in the future.
For internet I/O look at the Socket, HttpURLConnection and the URLConnection classes.

OK, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.