private static void writeData(int[][][] data, String string) throws Exception {
FileOutputStream os = new FileOutputStream("data.txt");
DataOutputStream out = new DataOutputStream(os);
int elements;
for (int f = 0; f < data.length; f++) {
for (int r = 0; r < data[f].length; r++) {
for (int c = 0; c < data[f][r].length; c++) {
elements = data[f][r][c];
out.writeInt(99); // THIS WORK
out.writeInt(elements); // THIS NOT WORK
System.out.print(elements + " ");
}
}
}
os.close();
out.close();
}
15 Replies - 196 Views - Last Post: 14 January 2013 - 05:16 AM
#1
writeInt problem within the array.
Posted 13 January 2013 - 05:51 PM
I having problem trying to write 3d array into the file. I don't understand why if I put in the exact Int then it able to write into my text file, but when I put in the array elements it doesn't. Please help thank you.
Replies To: writeInt problem within the array.
#2
Re: writeInt problem within the array.
Posted 13 January 2013 - 05:57 PM
My guess is the 3d array isn't correctly initialized, also, did you happen to get an error? If so, please provide the stack trace.
#3
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:03 PM
Should work... array initialized or not
If not initialized it will write 0
You will have to describe what to qualify as "not work"
If not initialized it will write 0
You will have to describe what to qualify as "not work"
#4
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:12 PM
Well, when I tried to test print the element on the screen using System.out. It print the elements correctly. The WORK mean it print the results into the textfile, and the NOT WORK mean it doesn't print anything into the text file.
It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.
Oh, and no error during the run.
It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.
Oh, and no error during the run.
#5
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:35 PM
Use a BufferedWriter instead of a DataOutputStream.
#6
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:37 PM
DaiToBu, on 13 January 2013 - 09:12 PM, said:
It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.
How can you claim that ? What is the size of data.txt ?
How do you verify that it is not in the file ? Do you write a program to read the data back ? If not how do you check that the array is effectively there or not ?
#7
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:44 PM
pbl, on 13 January 2013 - 06:37 PM, said:
DaiToBu, on 13 January 2013 - 09:12 PM, said:
It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.
How can you claim that ? What is the size of data.txt ?
How do you verify that it is not in the file ? Do you write a program to read the data back ? If not how do you check that the array is effectively there or not ?
The file is 276 bytes
When I opened the data.txt it display cÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁcÁ, because i made the bottom code to test it. I want to read it back later, and put the results into another 3d array, but I want to make sure the write method working first.
for (int f = 0; f < data.length; f++) {
for (int r = 0; r < data[f].length; r++) {
for (int c = 0; c < data[f][r].length; c++) {
elements = data[f][r][c];
out.writeInt(99);
out.writeInt(999);
out.writeInt(elements);
System.out.print(elements + " ");
#8
Re: writeInt problem within the array.
Posted 13 January 2013 - 06:51 PM
Sure, the writeInt() method writes the 4 bytes of a int to the file
hardcode:
array[0][0][0] = 0x44434241;
before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44
hardcode:
array[0][0][0] = 0x44434241;
before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44
#9
Re: writeInt problem within the array.
Posted 13 January 2013 - 07:03 PM
pbl, on 13 January 2013 - 06:51 PM, said:
Sure, the writeInt() method writes the 4 bytes of a int to the file
hardcode:
array[0][0][0] = 0x44434241;
before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44
hardcode:
array[0][0][0] = 0x44434241;
before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44
I don't quite understand this. Can you please help me what I should do to fix it?
#10
Re: writeInt problem within the array.
Posted 13 January 2013 - 07:07 PM
An output stream writes the bytes to the file, a writer on the other hand writes the actual character to the file. Like I said before, use a BufferedWriter
#11
Re: writeInt problem within the array.
Posted 13 January 2013 - 07:09 PM
The int are written as binary in the file, you cannot see the ASCII representation of these numbers using an editor.
So you need a program to read back these value.
As the ASCII value of A is 0x41
if you do
array[0][0][0] = 0x41414141; // store ASCII AAAA in the first int
when you will open your file using Notepad or an other editor you should see "AAAA" at the beginning of the file
So you need a program to read back these value.
As the ASCII value of A is 0x41
if you do
array[0][0][0] = 0x41414141; // store ASCII AAAA in the first int
when you will open your file using Notepad or an other editor you should see "AAAA" at the beginning of the file
#12
Re: writeInt problem within the array.
Posted 13 January 2013 - 07:21 PM
I think I get what you mean. The best way to test the results is to read it back instead of reading it in the file. I will try to test some more before posting any further question. Thanks you.
#13
Re: writeInt problem within the array.
Posted 13 January 2013 - 07:56 PM
If I were you I would be using an ObjectOutputStream and an ObjetInputStream to write/read the whole array (with all its dimensions) in a single read/write statement
#14
Re: writeInt problem within the array.
Posted 13 January 2013 - 09:49 PM
I wouldn't mind testing your idea, but my teacher want me to follow the way she layout the homework.
#15
Re: writeInt problem within the array.
Posted 14 January 2013 - 04:54 AM
I don't know how you will read back your array as you just save the data in it on alinear fashion. When you will read back you will get let says 1000 int to read but how will you lay them on a 3D array. An array[10][10][10] or [1000][1][1] or [1][100][10] or ... ?
|
|

New Topic/Question
Reply



MultiQuote




|