Some IO Help Needed, on a project..

Hei!

im doing my very first Java file IO function ;D, function is almost an copy/paste
of suns java IO tutorials but i failed to do an all way working function…

my question is how to make ‘getCodeBase()’ function to work with my project?? ???

FileInputStream in = new FileInputStream (getCodeBase()+“xanadu.txt”); returns an red liner on code…

//----

public void copyBytes () throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream (“c:/xanadu.txt”);
out = new FileOutputStream(“c:/outagain.txt”);
int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

}

//----

When you got errors it’s wise to mention them also so we can know what is wrong.
furthermore you provided copyBytes() while we need to see getCodeBase() instead.

Hei!

When you got errors it’s wise to mention them also so we can know what is wrong.
furthermore you provided copyBytes() while we need to see getCodeBase() instead.

were sorry, here is it, the error…

//------------------

FileInputStream in = new FileInputStream (getCodeBase()+“xanadu.txt”); returns an red liner on code and the following error… ??? ??? ???

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Jari Tapio\FullScreenTest\build\classes
C:\Documents and Settings\Jari Tapio\FullScreenTest\src\fullscreentest\MultiBufferTest.java:82: cannot find symbol
symbol : method getCodeBase()
location: class MultiBufferTest
in = new FileInputStream (getCodeBase ()+“xanadu.txt”);
1 error
BUILD FAILED (total time: 5 seconds)

//------------------

lol

listen, start reading basic java tutorials and books, something in general about java language. Only when you know the basics you should come back and try to make a game. This is a forum for developing games in java, we won’t teach you how to program java here.

About your error, you are trying to use a method that does not exist. Like, it’s not there, you didn’t wrote/code it.

Hei!

Kova wrote :

//----------------------

lol

listen, start reading basic java tutorials and books, something in general about java language. Only when you know the basics you should come back and try to make a game. This is a forum for developing games in java, we won’t teach you how to program java here.

//----------------------

‘listen, start reading basic java tutorials and books… blahblah…’
well, that was a good advice, thanks, as i am a very novice on Java i need to know where to start,
what sites do you people recommend to me and what sites are good to take to my bookmarks, thanks. :smiley:

‘…only when you know basics you should come back and try to make a game…’ :-
well, this was an bit of a unpolite from you, and knowing basics is a complicated thing,
i can say to you that i do know a basics about game programming but i just dont know
how to read a file from a Java ‘/build/class/’ directory getCodeBase () didnt work…

‘…This is a forum for developing games in java, we won’t teach you how to program java here…’ :stuck_out_tongue:
well, im after a game and what i think is that people do teach how to program Java in here
(bufferedimage is a popular thing on this forum then why not ‘FileInputStream’)??

;D and cos ya were thinking that i cant make a real game then here is one for you its my visualc++ cavegame and im doing
it currently to Java hopely you like it, i need to know how to read those level files from my ‘/build/classes’ directory to
make my Java version roll with those old c++ levels…

www.5mingames.eurojari.net/CaveGame99.zip

hope you liked it, i will publish it on my webpage later, i made it at 1999, if you played my game
you can understand that i can say that i am a hobby game developer and i just need to learn some basics,
liked you said, of Java and i was thinking that this forum would be right place to start doing it and doing it with
real people, hopely i was not wrong,

bye…

JariTapio / Helsinki

//--------

Game developing is a way of fun…

Although Kova was a bit rude, he’s quite right.

When trivial errors like this are a showstopper for you, it won’t help when we give you the solution, you’d - unfortunately - bump right into the next trivial error.

Get yourself a nice Java book, and believe me, there are only a few really good ones. Try one that doesn’t mention AWT/Swing at all, because it will only get in the way when learning the basics (i’m dead-serious about this). Command-line applications is the way to go. Once you know all about Java, it’s time to build some GUIs.

Goodluck!

I was a bit rude, but I think I had right to be rude in this instance. At the end I told you what the problem is. If it’s about File I/O or something like that I would reply nicely, but you don’t know how to use methodes (functions), and that’s basics of basics, not just for java but for all programming. Second, java compiler told you exactly what is wrong, but you don’t know how to read errors also. So although you have some experience in programming (didn’t try your game) you have not enough experience in java to code … well… pretty much anything. I’m not trying to be rude here, just speaking the truth as I see it.

What helped me the most when I was starting out with java is free e-book: Thinking in Java by Bruce Eckel. I had 3rd edition and it is free, but I think 4th edition isn’t free anymore. Thinkin in java is for begginers in java that have basic programming experience. Try www.bruceeckel.com, maybe you can still download 3rd edition for free.
EDIT: …3rd edition is still there, here is direct link to zip file

Hei! :slight_smile:

My first post was a bit miss leading, sorry for that, as im not that beginner what it lets people to understand, sorry, i just havent made IO routines with Java, ever.

i can build many different types of games with c++ and some with Java, but, liked i said, i havent made Java IO routines, ever, and would really presiate if
someone could help me a bit…

the problem is that i dont know how to read a data file from a path where .CLASS files are…
in c++ if you want to load a file from a directory where .exe file is launched you simply use “thefilename.dat” and nothing else this seems not to be true with java ???
if i put a line to my code…
in = new FileInputStream (“input.txt”);
i receive an error even if my ‘input.txt’ is on my .CLASS directory, why… :-X

public void copyBytes () throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream (“c:/input.txt”);
out = new FileOutputStream(“c:/output.txt”);
int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

}

code works fine but when i change the line
in = new FileInputStream (“c:/input.txt”);
to
in = new FileInputStream (“input.txt”);
to load file ‘input.txt’ from where the .class files are, i receive an ‘file not found’, why ???
from where is Java loading ‘input.txt’ when there is no path included?? c++ loads where the .exe is ???

java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at java.io.FileInputStream.(FileInputStream.java:66)
at MultiBufferTest.copyBytes(MultiBufferTest.java:84)
at MultiBufferTest.(MultiBufferTest.java:106)
at MultiBufferTest.main(MultiBufferTest.java:184)

JariTapio / Helsinki

//—

Game developing is a way of fun, thanks for your code…

The file is relative to the working-directory.

If you have a class Abc in package xyz.qrs, that would be invoked like:

Say you have your classes at:
C:\java\classes

make that your current-directory (cmd.exe -> cd)
then call:
java xyz.sqr.Abc

Now new File(“input.txt”) will be relative to your working-dir, being:
C:\java\classes\input.txt

However, if you have another current-directory, like:
cd D:
java -classpath C:\java\classes xyz.qrs.Abc

now the file “D:\input.txt” would be searched for.

Well… this is exactly the same in C/C++ with EXE-files… your working-directory just happened to be the same as the directory that contained the EXE

Hei! 8)

thanks Riven, i now get my “input.txt” file to be loaded from the same directory where my executable jar file is launched… ;D ;D ;D

my new questions are -
how to modify the code to make the “input.txt” file to be loaded inside the jar :stuck_out_tongue:
secondly is it possible to write a file “output.txt” to a jar :stuck_out_tongue:
and finally, if im using NetBeans IDE 5.5 how to modify the code to make my application
run without errors from IDE when “input.txt” is located at ‘/build/classes/’ directory ?? ???

i need somekind of a classPath (); function 8)

public void copyBytes () throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream (“input.txt”);
out = new FileOutputStream(“output.txt”);
int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

}

JariTapio / Helsinki

//----

Game developing is a way of fun, thanks for your code…

You can access files that are located in the classpath using a ClassLoader. More specifically you can use the methods ClassLoader#getSystemResourceAsStream or ClassLoader#getResourceAsStream.
As far as I know j2se doesn’t provide any API to update an existing jar file. It’s pretty straightforward to create a new one though using a JarOutputStream.