• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

eqs

[java] Read/write files.

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 LOADING = "LOADING FILE....\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
add(TEXTBOX);
DATA = START+LOADING;
test();
}

public void test() {
MAX = TEXTBOX.getRows();
TEXTBOX.insert(START,MAX);
TEXTBOX.insert(LOADING,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!
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites