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: Saving an array to a buffer.

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Saving an array to a buffer.

    Currently in my Java Project, the item bonuses are stored in a buffered file and read like this:
    	private static final void loadPackedBonuses() {
    		try {
    			RandomAccessFile in = new RandomAccessFile(PACKED_PATH, "r");
    			FileChannel channel = in.getChannel();
    			ByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0,
    					channel.size());
    			itemBonuses = new HashMap<Integer, int[]>(buffer.remaining() / 38);
    			while (buffer.hasRemaining()) {
    				int itemId = buffer.getShort() & 0xffff;
    				int[] bonuses = new int[18];
    				for (int index = 0; index < bonuses.length; index++)
    					bonuses[index] = buffer.getShort();
    				itemBonuses.put(itemId, bonuses);
    			}
    			channel.close();
    			in.close();
    		} catch (Throwable e) {
    			Logger.handle(e);
    		}
    	}

    Now, I unpacked that into a .txt file and I'm stuck on writing what I unpacked back into a buffered file. The part that I'm stuck on is writing the array of bonuses. Here's what an item bonus line looks like:

    16183 - -4 -4 167 -4 0 0 0 0 -1 1 0 0 0 0 160 0 0 0

    Here's my code so far:

     
    	private static final void loadUnpackedBonuses() {
    		try {
    			DataOutputStream out = new DataOutputStream(new FileOutputStream(
    					PACKED_PATH));
    			BufferedReader in = new BufferedReader(
    					new FileReader("data/items/itembonuses.txt"));
    			String line;
    			while ((line = in.readLine()) != null) {
    				String[] data = line.split(" - ");
    				if (data == null)
    					continue;
    				int item = Integer.parseInt(data[0]);
    				int[] bonuses = new int[18];
    				out.writeShort(item);
    				for (int i = 0; i < bonuses.length; i++) {
    					out.writeShort(bonuses[i]);
    				}
    			}
    			in.close();
    			out.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

    All and any help is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Saving an array to a buffer.

    Can you explain what the difference is between the contents of a buffered file and a .txt file?

    part that I'm stuck on is writing the array of bonuses
    What does the current code write to the file?

    One problem I see in the code is that the array: bonuses never has any data put into it. It will be full of 0s.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Saving an array to a buffer.

    There seems to be lot of problems..
    data
    in second code can never be null.
    What is BufferedFile(do you mean to have some array for holding up the data) and what is the data format in the text file you are reading in second part of code..there seems to error in parsing logic..

Similar Threads

  1. Replies: 26
    Last Post: February 16th, 2013, 07:21 AM
  2. Java String Buffer
    By anonb in forum Object Oriented Programming
    Replies: 3
    Last Post: December 19th, 2012, 02:59 AM
  3. [SOLVED] Thread/Double Buffer Help
    By Wakko45 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 20th, 2012, 11:31 PM
  4. Getting pixels from an image, and saving them in an array?
    By jtvd78 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 14th, 2011, 08:48 PM
  5. PLEASE HELP ME with double buffer
    By DouboC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 05:32 PM