rouncer
|
 |
«
Posted
2010-04-26 15:22:58 » |
|
Im looking around the net for a file write and read example and ive got an implementation using DataOutputStream and DataInputStream but its not really working, Could someone please explain an easy way to do this, or explain DataOutputStream a little for me? Its giving me a file not found exception but it shouldnt be.
|
|
|
|
ryanm
|
 |
«
Reply #1 - Posted
2010-04-26 15:29:26 » |
|
DataOutputStream and DataInputStream are pretty much self explanatory I'm afraid. You write data, you read data, It Just Works. At least, I've never had any problems. Fortunately, your problem is not with the data streams - you're trying to open a file which doesn't exist.
Post the full exception and the relevant code that triggers it.
|
|
|
|
rouncer
|
 |
«
Reply #2 - Posted
2010-04-26 15:51:08 » |
|
Sorry for asking such ambiguous questions, but thankyou for the reply! Ive got it sorta half working...
Maybe something more direct like =>
a) How do you create a file in java? (the exception was file not found) b) Where does this file go on the disk c) Once its running on the net as an applet, where is the file going to be located now? Im just a little confused. d) could you post a simple example of how to catch an IOException?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #3 - Posted
2010-04-26 19:55:24 » |
|
a) How do you create a file in java? (the exception was file not found)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| File file = new File("C:/Program Files/MyProduct.txt");
OutputStream out = new FileOutputStream(file);
byte[] data = new byte[26]; for(int i=0;i<26; i++) data[i] = (byte)('a'+i);
out.write(data); out.flush(); out.close();
|
b) Where does this file go on the disk
Where you tell it to. Just like in C. You have relative and absolute paths. c) Once its running on the net as an applet, where is the file going to be located now? Im just a little confused.
Won't work with applets. See my reply in your other thread. d) could you post a simple example of how to catch an IOException?
1 2 3 4 5 6 7 8 9
| try { if(!file.exists()) throw new FileNotFoundException(); } catch(IOException exc) { exc.printStackTrace(); } |
Besides that, 1 2
| File file = new File("your path"); new FileInputStream(file); |
will throw a FileNotFoundException if the file does not exist.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
rouncer
|
 |
«
Reply #4 - Posted
2010-04-26 21:19:53 » |
|
Thankyou Riven. Its a pity what you told me tho, so I cant even load a game level!
|
|
|
|
Riven
|
 |
«
Reply #5 - Posted
2010-04-26 21:23:23 » |
|
you can, by uploading/downloading to/from a server (with a php script)
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
rouncer
|
 |
«
Reply #6 - Posted
2010-04-27 00:43:16 » |
|
Thanks for the help, but I tried the code you said that would create the file and it just said "file not found" the same! Can you tell me whats wrong with this? Its just exceptioning out. Maybe there is nothing wrong with it and im stuffing something else up. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| public void save_map() { String FILENAME = "map.map"; DataOutputStream os = new DataOutputStream(new FileOutputStream(FILENAME)); os.writeInt(bstones); int i; int j; for(i=0;i<bstones;i++) { os.writeFloat(bstone[i].fillet_dist); os.writeFloat(bstone[i].fillet_depth); os.writeFloat(bstone[i].depth); os.writeInt(bstone[i].loop_vs); for(j=0;j<bstone[i].loop_vs;j++) { os.writeFloat(bstone[i].loop_v[j].x); os.writeFloat(bstone[i].loop_v[j].y); os.writeFloat(bstone[i].loop_v[j].z); } os.writeInt(bstone[i].vs); os.writeInt(bstone[i].ts); for(j=0;j<bstone[i].vs;j++) { os.writeFloat(bstone[i].v[j].x); os.writeFloat(bstone[i].v[j].y); os.writeFloat(bstone[i].v[j].z); } for(j=0;j<bstone[i].ts;j++) { os.writeInt(bstone[i].t[j].i[0]); os.writeInt(bstone[i].t[j].i[1]); os.writeInt(bstone[i].t[j].i[2]); os.writeInt(bstone[i].t[j].col); os.writeFloat(bstone[i].t[j].nor.x); os.writeFloat(bstone[i].t[j].nor.y); os.writeFloat(bstone[i].t[j].nor.z); } }
os.writeInt(blights); for(i=0;i<blights;i++) { os.writeFloat(blight[i].pos.x); os.writeFloat(blight[i].pos.y); os.writeFloat(blight[i].pos.z); os.writeFloat(blight[i].power); } os.close(); } public void load_map() { String FILENAME = "map.map"; DataInputStream os = new DataInputStream(new FileInputStream(FILENAME)); int k; int j; bstones=os.readInt(); int i; for(i=0;i<bstones;i++) { bstone[i].fillet_dist=os.readFloat(); bstone[i].fillet_depth=os.readFloat(); bstone[i].depth=os.readFloat(); bstone[i].loop_vs=os.readInt(); bstone[i].loop_v=new vector[bstone[i].loop_vs]; for(k=0;k<bstone[i].loop_vs;k++) bstone[i].loop_v[k]=new vector(); for(j=0;j<bstone[i].loop_vs;j++) { bstone[i].loop_v[j].x=os.readFloat(); bstone[i].loop_v[j].y=os.readFloat(); bstone[i].loop_v[j].z=os.readFloat(); } bstone[i].vs=os.readInt(); bstone[i].ts=os.readInt(); bstone[i].v=new vector[bstone[i].vs]; for(k=0;k<bstone[i].vs;k++) bstone[i].v[k]=new vector(); bstone[i].t=new tri[bstone[i].ts]; for(k=0;k<bstone[i].ts;k++) bstone[i].t[k]=new tri(); for(j=0;j<bstone[i].vs;j++) { bstone[i].v[j].x=os.readFloat(); bstone[i].v[j].y=os.readFloat(); bstone[i].v[j].z=os.readFloat(); } for(j=0;j<bstone[i].ts;j++) { bstone[i].t[j].i[0]=os.readInt(); bstone[i].t[j].i[1]=os.readInt(); bstone[i].t[j].i[2]=os.readInt(); bstone[i].t[j].col=os.readInt(); bstone[i].t[j].nor.x=os.readFloat(); bstone[i].t[j].nor.y=os.readFloat(); bstone[i].t[j].nor.z=os.readFloat(); } }
blights=os.readInt(); for(i=0;i<blights;i++) { blight[i].pos.x=os.readFloat(); blight[i].pos.y=os.readFloat(); blight[i].pos.z=os.readFloat(); blight[i].power=os.readFloat(); }
os.close(); } |
|
|
|
|
|
Riven
|
 |
«
Reply #8 - Posted
2010-04-27 06:01:31 » |
|
If the file doesn't exist yet, just do this: 1 2 3 4 5
| try{ FILENAME.createNewFile(); }catch(IOException ex){ ex.printStackTrace(); } |
That won't help at all. If you are going to read data from a file that doesn't exist, it won't really help you much creating an empty file right there...
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
rouncer
|
 |
«
Reply #9 - Posted
2010-04-27 06:55:26 » |
|
But it doesnt work if im reading or writing, I actually havent even tested the read yet because I havent been able to write anything to read!
Ok, I got the file to create, it went into the bin directory (thats the relative directory I just found out) But the rest of the code still doesnt work it goes file not found if the file is there or not, so writing the way I've done it doesnt work.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Nate
|
 |
«
Reply #10 - Posted
2010-04-27 07:23:29 » |
|
I have no idea what problem you are having. It sounds like you are trying to read before you have written the file, but we shouldn't have to guess. Post the exception if you are getting one. The code you were given works, eg: 1 2 3 4 5 6 7 8 9 10 11 12
| static public void main (String[] args) throws IOException { String fileName = "map.map";
DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName)); output.writeInt(123); output.close();
DataInputStream input = new DataInputStream(new FileInputStream(fileName)); int value = input.readInt(); input.close(); System.out.println(value); } |
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2010-04-27 07:29:45 » |
|
Maybe "./map.map" is a directory. That will throw a FileNotFoundException in the FileOutputStream constructor too.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
CommanderKeith
|
 |
«
Reply #12 - Posted
2010-04-27 07:30:12 » |
|
This works for me. Adapt it to your method and it should work. Like Riven says, creating the file won't help if you're reading (but here we're writing). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import java.io.*;
public class Test{ public static void main(String[] args){ File file = new File(System.getProperty("user.home"), "MyProduct.txt"); if (file.isFile() == false){ try{ file .createNewFile(); }catch(IOException ex){ ex.printStackTrace(); } } OutputStream out = null; try{ out = new FileOutputStream(file); byte[] data = new byte[26]; for(int i=0;i<26; i++) data[i] = (byte)('a'+i); out.write(data); out.flush(); out.close(); }catch(IOException ex){ ex.printStackTrace(); } System.out.println("done"); } } |
|
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2010-04-27 07:31:10 » |
|
This works for me. Adapt it to your method and it should work. Like Riven says, creating the file won't help if you're reading (but here we're writing).
That won't help either. FileOutputStream(file) will create the file. Doing it manually is not required.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
rouncer
|
 |
«
Reply #14 - Posted
2010-04-27 07:37:42 » |
|
Please excuse me while I go shoot myself... I know, I went through the docs at sun and they are telling me the exact same thing you guys are telling me but its just not working! "map.map" cant be a directory, its just going "file not found" exception then the rest of it fails afterwards cause I'm not try and catching it. So you guys have no idea whats going wrong... I tell you what I probably typed it in right FIRST TIME and I shouldn't have even been here with my problem. So theres nothing wrong with my code, DAMN IT! This code here is returning the same error... 1 2 3 4 5 6 7 8 9 10 11 12
| FileOutputStream out = null;
out = new FileOutputStream("map.map"); int c;
byte[] data = new byte[26]; for(int i=0;i<26; i++) data[i] = (byte)('a'+i); out.write(data);
out.close(); |
Exception in thread "Thread-4" java.lang.Error: Unresolved compilation problems: Unhandled exception type FileNotFoundException Unhandled exception type IOException Unhandled exception type IOException at ball.save_map(ball.java:626) at ball.run(ball.java:1015) at java.lang.Thread.run(Unknown Source) The only thing that worked was create file...
|
|
|
|
CommanderKeith
|
 |
«
Reply #15 - Posted
2010-04-27 07:39:02 » |
|
Never knew that, ta 
|
|
|
|
CommanderKeith
|
 |
«
Reply #16 - Posted
2010-04-27 07:43:45 » |
|
Exception in thread "Thread-4" java.lang.Error: Unresolved compilation problems: Unhandled exception type FileNotFoundException Unhandled exception type IOException Unhandled exception type IOException
at ball.save_map(ball.java:626) at ball.run(ball.java:1015) at java.lang.Thread.run(Unknown Source)
The only thing that worked was create file...
That's a compilation error.... Put your code in a try catch block Maybe try this tutorial as well: http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
|
|
|
|
Roquen
|
 |
«
Reply #17 - Posted
2010-04-27 07:48:44 » |
|
That's a compile error. One of your classes can't compile so Eclipse (or whatever your using) is creating a method that simply throws an exception until you correct the problem. So (in effect) you're running: 1 2 3 4
| save_map() { throw new Error("Unresolved compilation problems."); } |
|
|
|
|
rouncer
|
 |
«
Reply #18 - Posted
2010-04-27 07:56:28 » |
|
Doesnt it only make the "unhandled exception" once the exception happens. It wouldnt come up with that im not handling the exception if the exception never arose.
|
|
|
|
Riven
|
 |
«
Reply #19 - Posted
2010-04-27 08:10:25 » |
|
Spoon feeding  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.io.*;
public class Whatever { public static void main(String[] args) { try {
byte[] data = new byte[26]; for(int i=0;i<26; i++) data[i] = (byte)('a'+i);
String path = "map.map"; File file = new File(path); System.out.println("absolute path: " + file.getAbsolutePath()); System.out.println("exists: " + file.exists()); System.out.println("is directory: " + file.isDirectory());
FileOutputStream out = new FileOutputStream(file); out.write(data); out.close();
} catch(IOException exc) { exc.printStackTrace(); } } } |
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
rouncer
|
 |
«
Reply #20 - Posted
2010-04-27 08:20:33 » |
|
OOOOOOOOooohhh!
It worked! THANKYOU!
|
|
|
|
Riven
|
 |
«
Reply #21 - Posted
2010-04-27 10:53:10 » |
|
so what was the difference? It looks all the same for me.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
cylab
|
 |
«
Reply #22 - Posted
2010-04-27 11:40:37 » |
|
Doesnt it only make the "unhandled exception" once the exception happens. It wouldnt come up with that im not handling the exception if the exception never arose.
The java compiler wants you to catch specific exceptions. If you ignore to do so, the compiler simply won't compile your code, regardless of the fact that the exception would not occur at runtime. See http://en.wikipedia.org/wiki/Checked_exceptionsAnd maybe it may be a good idea to spend some time at http://java.sun.com/docs/books/tutorial/ after all...
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
rouncer
|
 |
«
Reply #23 - Posted
2010-04-27 12:21:32 » |
|
Im slowly working stuff out, you never have to use exceptions in c thats all, in java they force you to and Ive never done it before!
|
|
|
|
CaptainJester
|
 |
«
Reply #24 - Posted
2010-04-27 13:28:19 » |
|
Im slowly working stuff out, you never have to use exceptions in c thats all, in java they force you to and Ive never done it before!
That's why I told you to read through The Java Tutorial in another thread. Every problem you have been having so far is answered in there. If you take some time now to read through it you will save yourself a lot of time and headaches. If you don't you will just keep coming back here with more questions. While Java and C are very similar, there are also a lot of differences.
|
|
|
|
Nate
|
 |
«
Reply #25 - Posted
2010-04-27 18:37:29 » |
|
+1 Java tutorial.
|
|
|
|
|