Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  binary file writing and reading  (Read 2509 times)
0 Members and 1 Guest are viewing this topic.
Offline rouncer

Junior Member





« 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.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« 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.
Offline rouncer

Junior Member





« 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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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  
// (this is a representation of a file, just an object in RAM, not on disk)
File file = new File("C:/Program Files/MyProduct.txt");

// now we are going to write a file. If it doesn't exist, it will be created (!)
OutputStream out = new FileOutputStream(file);

// write the plain text alphabet to a file.
byte[] data = new byte[26];
for(int i=0;i<26; i++)
   data[i] = (byte)('a'+i);

out.write(data);
out.flush(); // sync the file-descriptor
out.close(); // close the file-descriptor


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()) // or whatever you which to do
     throw new FileNotFoundException(); // this is a subclass of IOException
}
catch(IOException exc)
{
   exc.printStackTrace(); // for easy debugging
}


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
Offline rouncer

Junior Member





« 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!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline rouncer

Junior Member





« 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();
 }
Offline CommanderKeith
« Reply #7 - Posted 2010-04-27 04:37:28 »

If the file doesn't exist yet, just do this:

1  
2  
3  
4  
5  
try{
 FILENAME.createNewFile();
}catch(IOException ex){
 ex.printStackTrace();
}



See: http://java.sun.com/javase/6/docs/api/java/io/File.html#createNewFile()

Sun's java tutorial is pretty good on this stuff, worth a look:
http://java.sun.com/docs/books/tutorial/essential/index.html

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline rouncer

Junior Member





« 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!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« 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);
}

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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){
     
      //To do a write, do what Riven said before,  but create the file if it isn't there:
     
      // (this is a representation of a file, just an object in RAM, not on disk)
     File file = new File(System.getProperty("user.home"), "MyProduct.txt");
                // make sure that the file exists since we're going to write to it.
     if (file.isFile() == false){
         try{
           file .createNewFile();
         }catch(IOException ex){
            ex.printStackTrace();
         }
      }
      OutputStream out = null;
        try{
            out = new FileOutputStream(file);
         // write the plain text alphabet to a file.
        byte[] data = new byte[26];
         for(int i=0;i<26; i++)
            data[i] = (byte)('a'+i);
         
         out.write(data);
         out.flush(); // sync the file-descriptor
        out.close(); // close the file-descriptor
       }catch(IOException ex){
         ex.printStackTrace();
        }
        System.out.println("done");
   }
}

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline rouncer

Junior Member





« 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...
Offline CommanderKeith
« Reply #15 - Posted 2010-04-27 07:39:02 »

Never knew that, ta  Smiley

Offline 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

Offline 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.");
}


Offline rouncer

Junior Member





« 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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #19 - Posted 2010-04-27 08:10:25 »

Spoon feeding Smiley

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
Offline rouncer

Junior Member





« Reply #20 - Posted 2010-04-27 08:20:33 »

OOOOOOOOooohhh!

It worked!  THANKYOU!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline cylab

JGO Ninja


Medals: 49



« 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_exceptions

And 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!
Offline rouncer

Junior Member





« 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!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« 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.

Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #25 - Posted 2010-04-27 18:37:29 »

+1 Java tutorial.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (7 views)
2014-09-21 02:42:18

BurntPizza (8 views)
2014-09-21 01:30:30

BurntPizza (11 views)
2014-09-21 00:34:41

moogie (11 views)
2014-09-21 00:26:15

UprightPath (23 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!