• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1eqs  Members

122
Like
Likes
Like

Posted 22 February 2000 - 01:56 PM

Ok, Im just starting to learn how to read and write to files on the HD. My method for writing is this: public void write(String outDATA) { FileOutputStream out; // declare a file output object PrintStream p; // declare a print stream object try { out = new FileOutputStream("myfile.txt"); // Connect print stream to the output stream p = new PrintStream(out); p.println (outDATA); p.close(); TEMP = "FILE WROTE SUCCESSFULLY. \n"; } catch (Exception e) { System.err.println ("Error writing to file"); TEMP = "ERROR WRITTING FILE. \n"; // 2nd } } I dont need people telling me how crap it is, that i know. What i DO need to know is why it doesnt work. When i run it i end up with the 2nd TEMP string and no myfile.txt. Why is that? Thanks. PEACE GAZZ

### #2SiCrane  Moderators

11528
Like
Likes
Like

Posted 22 February 2000 - 03:44 PM

My first guess would be that the java runtime system you''re using is sandboxing you in. However, it could be any number of things. I would suggesting printing the exception and posting that on the board so we can be of more help.

### #3 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 22 February 2000 - 03:55 PM

Here is the entire thing I was playing with today:

import java.applet.*;
import java.awt.*;
import java.io.*;
class filetest extends Applet {

int MAX;
String TEMP;
String DATA;
String START = "STARTING APPLET...\n";
String ERROR = "ERROR WRITTING TO FILE.. \n";
String TEMPFAIL= "ERROR IN WRITE METHOD. \n";
String WROTE = "FILE WRITTEN TO SUCCESSFULLY";
TextArea TEXTBOX = new TextArea();

public void init() {
setLayout(new GridLayout(2,1));
add(new Button("OK")); // Just to fill the space
test();
}

public void test() {
MAX = TEXTBOX.getRows();
TEXTBOX.insert(START,MAX);
write(DATA);
if(TEMP!=null)
TEXTBOX.insert(TEMP,MAX);
else
TEXTBOX.insert(TEMPFAIL,MAX);
}

public void write(String outDATA) {
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try
{
out = new FileOutputStream("myfile.txt");

// Connect print stream to the output stream
p = new PrintStream(out);
p.println (outDATA);
p.close();
TEMP = "FILE WROTE SUCCESSFULLY. \n";
}
catch (Exception e)
{
System.err.println ("Error writing to file");
TEMP = "ERROR WRITTING FILE. \n";
}

}

}

As far as errors go i dont get one. I did get a null pointer exception before, but that was my fault by not assigning the DATA string a value before calling write(). The only thing i get is the debug from the TEMP Sting i have. I allways see the ''TEMP = "ERROR WRITTING FILE. \n";'', meaning i have some exception being thrown right?

Ive prpbably missed something stupid, its not unusual for me. Any help pointing it out would be great!

### #4javanerd  Members

122
Like
Likes
Like

Posted 22 February 2000 - 09:54 PM

Umm... You are catching all exceptions but you are not displaying them anywhere, so you don''t know what goes wrong. How about if you would change your exception handling a bit:
catch (FileNotFoundException noSuchFile){  System.err.println ("File not found.");  TEMP = "COULDN''T FIND FILE. \n";}

The FileNotFoundException is the only exception thrown in that code block that you can excpect. If any other exception is thrown the Java VM will handle it by printing it to the output stream. This way you gain some knowledge of what goes wrong.

### #5javanerd  Members

122
Like
Likes
Like

Posted 22 February 2000 - 09:59 PM

Aha! I noticed that you are using:
class filetest extends Applet

Sorry no bonus, Applets are restricted in the way they can access system resources. E.g. access to file system is prohibited if the applet is not "signed". The signing is a process where a digital ID is embedded to the applet JAR file and that is used to identify that YOU made this applet. The user can then choose if he is going to TRUST YOUR APPLETS to access his filesystem or not. To get more info about signing you should browse the www.javasoft.com website.

Or another solution would be to convert your applet to an application (that can''t be set on a webpage, but run in the users machine like any other application). But at least you could get the code working immediately...

If you don''t know how to convert the Applet to an application or don''t know what is the difference, I suggest walking to your nearest bookstore and buying (at least) one book about basics of Java programming...

### #6eqs  Members

122
Like
Likes
Like

Posted 23 February 2000 - 04:19 AM

Ive been to the bookstore plenty. I got ''teach yourself Java 1.1 in 21 days'', ''Thinking in Java'' and ''the black art of java game programming''.

Ill have to read them again, but i dont remember them saying an Applet had the security issue. How do i get it signed then? This was actually a test to make a high score server for a game I may or may not write. I thought i had to save the data to a file. If i made it an app, can i still put it on the web?

I do have one question about apps in Java: How do i run them without going to Dos prompt or using a web page?

Thanks for your help guys! Ive posted at other Java sites and only this one do i get a responce.

PEACE
GAZZ

### #7joeG  Members

172
Like
Likes
Like

Posted 23 February 2000 - 04:42 AM

quote:
Original post by eqs

Ive been to the bookstore plenty. I got 'teach yourself Java 1.1 in 21 days', 'Thinking in Java' and 'the black art of java game programming'.

Ill have to read them again, but i dont remember them saying an Applet had the security issue. How do i get it signed then? This was actually a test to make a high score server for a game I may or may not write. I thought i had to save the data to a file. If i made it an app, can i still put it on the web?

I do have one question about apps in Java: How do i run them without going to Dos prompt or using a web page?

Thanks for your help guys! Ive posted at other Java sites and only this one do i get a responce.

PEACE
GAZZ

Man, this will be fun. Most browsers are supposed to allow your applet network access to the server from which the applet was downloaded (specified in the CODEBASE tag I think). So, if you want your high scores to be stored server-centric (i.e. in one place), you'll want to, within your applet, set up a socket that connects to a Java application running 24-7 on the server where you're game will be. Writing the client (your Applet's socket code) is relatively easy compared to figuring out how to write the server socket code(the application that runs on the server, 24-7). Basically, for the server socket code, you need to derive a new Socket class and a new ServerSocket class, sort of like the one you did for the client, then you need to overide the accept method (of the class that is derived from ServerSocket) to call implAccept(yourSpecializedSocket socketInstance). After you call implAccept you can then open up the high scores file (because the server is an application, not an applet) and write (if the client did get a high score) a new entry into the high scores.

Probably missed a lot of stuff,

JoeG

Edited by - joeG on 2/23/00 10:44:33 AM

### #8eqs  Members

122
Like
Likes
Like

Posted 23 February 2000 - 05:14 AM

Yea, i understand the network side of things, but im no-where near that far yet!!

I did what you said Javanerd, and went back to apps. I wrote everything out like i thought it was suppost to. I compile it, then tried running it with java filename.class. I get an error everytime. I even took some code straight out of my 21 days book:

class apptest{

public static void main(String args[]) {
System.out.println("TESTING");
}
}

ANd i get the same error:
Exception in thread ''main'' java.lang.NoClassDefFoundError: apptest/class

Whats that mean? Also, so i dont have to bother yall soo much, is there anywhere that shows the errors and their meanings? I remember Pascal came with error meanings, as did C. Does Java?

Thanks again guys!
PEACE
GAZZ

### #9Jim_Ross  Members

122
Like
Likes
Like

Posted 23 February 2000 - 07:17 AM

I bet your filename does not match up with the class name. For class apptest make sure you save it as apptest.java. I know you probably know that but sometimes when you're changing files their names don't like to be changed. Anyways, make sure that you are typing
javac apptest.java
and not
javac apptest.class
And finally, make sure that if you have a package definition at the top of you java source file, that it is in a sub directory from the current directory with the same name.
package playerCharacter; class hero {.... }javac playerCharacter/hero.java
p.s. I can't believe that those books didn't mention that applets have restricted security. Black art good, 21 days not so.

Edited by - Jim_Ross on 2/23/00 1:19:21 PM

### #10eqs  Members

122
Like
Likes
Like

Posted 23 February 2000 - 10:21 AM

I went through the 21 days book again. I did find the Security issues in the end. There wasnt much there, but i get the idea.
Anyways, I occasionally get the error when compiling now. I decided to use Jpad again and sometimes it compiles and sometimes it dont. Oh well.

I can read and write files in a basic maner now, THANKS GUYS! My next goal is to get it back into an applet.

PEACE
GAZZ

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.