Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble with writing/reading array to/from file

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    New Zealand
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Trouble with writing/reading array to/from file

    I'm using a mid-point displacement algorithm to generate a map, and the height values are then stored in an array.
    The trouble I'm having is loading the numbers back out of the file and into the array when i press F7.

    Here's my code for the reader/writer object:
    public class Writer {
     
        final int DATA_SIZE = 257;
     
            FileWriter fw;
            Scanner sc;
            File filename = new File("savedmap.max");
     
     
        public void write(MPDGenerator mpd) throws IOException{
     
            fw = new FileWriter(filename);
     
     
               fw.write("version=1.0 seed="+mpd.RSEED+"\n");
            for(int j=0;j<DATA_SIZE-1;j++){
                for(int k=0;k<DATA_SIZE-1;k++){
     
                    fw.write((int)(mpd.data[j][k]+2000));
     
                }
     
            }
     
     
     
     
        }
     
        public void read(MPDGenerator mpd) throws FileNotFoundException {
            sc = new Scanner(filename);
            sc.nextLine();
     
            for(int j=0;j<DATA_SIZE-1;j++){
                for(int k=0;k<DATA_SIZE-1;k++){
     
                    if (sc.hasNextInt()) {
                        mpd.data[j][k] = (int) (sc.nextInt()-2000);
                    }
                }
            }
            mpd.print();
        }
     
    }
    It doesn't give me any Errors, but doesn't reload the map.

    mpd.print(); is the method in the generator which converts the height values into a texture array for the renderer(which uses lwjgl)

    the +2000 and *-2000 is so it looks neater in the file.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,632
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Trouble with writing/reading array to/from file

    doesn't reload the map.
    Try debugging your code by adding printlns that print out was was read from the file before trying to store it into the array. That will show you what the program is reading from the file.

    Where is the possible exception thrown by the method handled? Is there a call to printStackTrace() in the catch block?

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    New Zealand
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with writing/reading array to/from file

    I re-wrote the writing to file so that it created a new line for every value, and then followed your advice. It now works.

    Thanks

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 10:53 AM
  2. Blackberry Api: Writing an array of bytes to a file
    By jules0075 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: July 18th, 2011, 01:50 PM
  3. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM