Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default writing to files from arrays

    I have a program that has made 8 arrays, array1 array2........array8, I need to write each array to a separate file called select1 select2.......select8 each select file corresponding to each array. I have a loop that will do it but its a for loop and when it is reading from the array I have no way to change the number of the array to the next one after its done reading and writing from the first, is there any way to do this without creating 8 separate loops?

    Java Code:
    for (int k = 0; k < 8; k++){
    	FileWriter file = new FileWriter("select" + (k+1) + ".txt");
    	BufferedWriter writer = new BufferedWriter(file);
    	for (int i = 0; i < FILESIZE/8; i++){
    		writer.write(new Integer(array1.toString()+"\n"));
    	}//for
    	writer.close();
    }// for

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Your eight arrays could be themselves elements of an array.

    Java Code:
    int data[][] = new String[8][];

    You can now access array1 as data[0] etc.

    I am assuming the individual elements are int, but the same would apply if they were String. Either way there's something wrong with the line

    Java Code:
    writer.write(new Integer(array1.toString()+"\n"));

    (perhaps a misplaced parenthesis?) You can't parse a string ending in "\n" as an int.

  3. #3
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    There was a problem with that line, i had to change it to:

    writer.write(new Integer(data[k].toString()+"\n"));

    using the idea you came up with, thank you for the help.

  4. #4
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. writing the files for particular time
    By damuammu in forum Advanced Java
    Replies: 3
    Last Post: 03-15-2011, 07:32 PM
  2. writing to binary files
    By TopNFalvors in forum New To Java
    Replies: 10
    Last Post: 03-04-2011, 09:29 AM
  3. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 10:21 PM
  4. Applets writing to files
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 09:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •