This discussion is archived
2 Replies Latest reply: Oct 15, 2006 7:39 PM by 807607 RSS

FileOutputStream problem...

807607 Newbie
Currently Being Moderated
the following code should create a file where every group of 3 Bytes should be
1, 1, 23
for now, when you java init cf.
but when i java init rf it says that only the first few groups of bytes are
following the 1, 1, 23 pattern then the rest are all 0.

so my question is why does it only do the pattern a few times then just
place 0's ?
import java.io.*;

class Init {
     Tile tiles[][];
     int mapSize;
     String mapName = "testmap";


     public Init(String[] arg) {
          int x;
          int y;
          this.mapSize = 2;

          tiles = new Tile[this.mapSize][this.mapSize];

          if(arg[0].equals("cf")) {

               System.out.println("Creating MAP file...");

               int[] mapData = new int[((this.mapSize*this.mapSize)*3)];

               System.out.println("Initilised mapData array [" + mapData.length + "]");


               for(int i = 0; i < ((this.mapSize*this.mapSize)); i = i+3) {
                         mapData[i] = 1;
                         mapData[i+1] = 1;
                         mapData[i+2] = 23;
               }

               System.out.println("Created map-Data");

               try {
                    FileOutputStream mapFile = new FileOutputStream(mapName + ".map");

                    System.out.println("Creating file");

                    for(int i = 0; i < mapData.length; i++) {
                         mapFile.write(mapData);
                         System.out.println("writing Data >> " + (i+1) + "/" + mapData.length);
                    }
                    mapFile.close();
                    System.out.println("Done!");

               } catch (IOException e) {
                    System.out.println("ERROR -- " + e.toString());
               }


          } else if(arg[0].equals("rf")) {


               System.out.println("Reading Map file...");

               int[] mapData = new int[(this.mapSize*this.mapSize)*3];

               System.out.println("Initilised mapData array [" + mapData.length + "]");

               try {
                    FileInputStream mapFile = new FileInputStream(mapName + ".map");

                    System.out.println("Reading file");
                    System.out.println(mapFile.available());
                    for(int i = 0; i < mapData.length; i++) {
                         mapData[i] = mapFile.read();
                         System.out.println("reading Data >> " + (i+1) + "/" + mapData.length);
                    }

                    

                    mapFile.close();
                    System.out.println("Done!");

               } catch (IOException e) {
                    System.out.println("ERROR -- " + e.toString());
               }


               System.out.println("creating tiles from map " + mapName + " | map size : " +this.mapSize);
               System.out.println("_________________________________________________________");

               int counter = 0;
               for(x = 0; x < this.mapSize; x++) {
                    for(y = 0; y < this.mapSize; y++) {
                         tiles[x][y] = new Tile(mapData[counter], mapData[(counter+1)], mapData[(counter+2)]);
                         System.out.println("new tile " + x + " X " + y + " | " + (counter+2) + " | terrain =" + tiles[x][y].terrain + " | restriction =" + tiles[x][y].movementRestrict + " | speed ="+ tiles[x][y].speed +" |");

                         counter += 3;
                    }
               }
               

          }
     System.out.println("ending...");
     }

     public static void main(String[] arg) {
          Init run = new Init(arg);
     }
}

class Tile {
     int terrain;
     int movementRestrict;
     int speed;

     public Tile(int t, int mR, int s) {
          this.terrain = t;
          this.movementRestrict = mR;
          this.speed = s;
     }
}
  • 1. Re: FileOutputStream problem...
    807607 Newbie
    Currently Being Moderated
    Tell me what's wrong with these lines of code:
                   for(int i = 0; i < ((this.mapSize*this.mapSize)); i = i+3) {
                             mapData[i] = 1;
                             mapData[i+1] = 1;
                             mapData[i+2] = 23;
                   }
  • 2. Re: FileOutputStream problem...
    807607 Newbie
    Currently Being Moderated
    thanks...
    now that i slept on it.
         
    for(int i = 0; i < ((this.mapSize*this.mapSize)*3); i = i+3) {
          mapData[i] = 1;
          mapData[i+1] = 1;
          mapData[i+2] = 23;
    }
    ;)