Results 1 to 6 of 6
Thread: Manipulating binary files
- 11-08-2011, 06:03 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Manipulating binary files
I'm working with multidimensional arrays and i have a question that i can't solve. I'll try to summarize what i've done and the problem that i encountered.
First i work with IDL and JAVA. IDL is an interface description language.
In the IDL i did:
To read 2 files: .sav and .cnj, where these files are arrays of the type DOUBLE = Array[4, 16, 16, 16]. I had to put these 2 files into 1 file , in a binary file. (OK - DONE)
In the JAVA:
After in the Java i read this binary and restructures into a new array, also of type [4, 16, 16, 16]. (OK - DONE)
However, now I came across the following problem, the array [4, 16, 16, 16] has dimensions variable according to the file read in IDL, that is, the array would look like [4, X, X, Y] where X and Y are the dimensions that can vary.
So, in the IDL i did: i record in binary file the positions of these dimensions.
you can see my code in IDL :
Java Code:PRO READ_FILE ; --------------------- ; Reading the file .sav ; --------------------- OpenR, lun, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav', /Get_lun file_sav = strarr(file_lines('C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav')) ReadF, lun, file_sav close, lun ; --------------------- ; Reading the file .cnj ; --------------------- OpenR, lun, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj', /Get_lun file_cnj = strarr(file_lines('C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj')) ReadF, lun,file_cnj close, lun ; ------------------------------------ ; Restoring the files .sav e .cnj ; ------------------------------------ restore, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav' restore, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj' ; ------------------------------------------------------------------ ; Saving to new file, creating the binary file ; ------------------------------------------------------------------ OpenW, Unit, 'C:\Documents and Settings\Projeto_Estudo\data.bin', /Get_lun, /SWAP_IF_LITTLE_ENDIAN ;----------------------------------------------------------------------------------------------------------------- ; Comparing dimensions and saving positions mod_imag [1], mod_imag [3] to file data.bin ;----------------------------------------------------------------------------------------------------------------- conj_imag = size(conjugado.imag, /dim) mod_imag = size(modelo.imag, /dim) if ((mod_imag[1] eq mod_imag[3]) and (conj_imag[1] eq conj_imag[3])) then begin writeu, unit, mod_imag[1], mod_imag[3] endif else begin print, 'Error: Dimension of the .sav e cnj' stop endelse writeu, unit, modelo.imag close, unit END
My code - JAVA:
Then I thought of doing something, read the binary in Java, get the value of positions mod_imag [1], mod_imag [3] and stored in a variable, and then to use in "for"Java Code:import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; public class LeituraArquivo { public static void main(String args[]) { // Declaring the array mat double mat[][][][] = new double[4][16][16][16]; // Call the method readFileBin() mat=readFileBin("C:/Documents and Settings/Web/IDLWorkspace/Projeto_Estudo/data.bin"); // Printing the array mat for(int i=0; i< 3; i++) { for(int j=0; j< 15; j++) { for(int k=0; k< 15; k++){ for(int l=0; l< 15; l++){ } } } } } public static double[][][][] readFileBin(String origem) { double aux[][][][] = new double[4][16][16][16]; try{ File fileOrigin = new File(origem); DataInputStream in = new DataInputStream(new FileInputStream(fileOrigin)); for(int l=0; l< 15; l++) { for(int k=0; k< 15; k++) { for(int j=0; j< 15; j++){ for(int i=0; i< 4; i++){ aux[i][j][k][l] = (double)in.readDouble(); System.out.print(" " + aux[i][j][k][l]); } } } } in.close(); }catch(Exception e){ System.out.println("Error to read file: "+origem); e.printStackTrace(); aux = null; } return(aux); } }
Example:
I know that the positions mod_imag [1], mod_imag [3] are of type LONG in IDL.Java Code:X= mod_imag[1]; Y = mod_imag[3]; double mat[][][][] = new double[4][X][X][Y];
If you can help me.
Thank you!!!
- 11-08-2011, 07:03 PM #2
Re: Manipulating binary files
Not sure I understand your problem.
Are you trying to create a 4 dimensional array in java with data read from a file?
- 11-08-2011, 07:26 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Manipulating binary files
So, these files .sav and .cnj are arrays.
However, according to the file read in IDL dimensions may vary, where the array would look like [4, X, X, Y] where X and Y are the dimensions that can vary.
So, in the IDL i did: i record in binary file the positions of these dimensions. I save the positions mod_imag [1], mod_imag [3] to file data.bin (line 34 - 43 in code - IDL)
Then I thought of doing something, read the binary in Java, get the value of positions mod_imag [1], mod_imag [3]
Do you understand?
- 11-08-2011, 07:32 PM #4
Re: Manipulating binary files
Sorry I don't see where your problem is.
Does this code that you posted do what you want?
Java Code:X= mod_imag[1]; Y = mod_imag[3]; double mat[][][][] = new double[4][X][X][Y]; // create the array with the given dimensions
- 11-09-2011, 11:33 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Manipulating binary files
My problem is ....i have an array of the type [4, 16, 16, 16], but this array can vary dimension, as for example: [4, 17, 17, 15] or [4, 14, 14, 12]. I don't know to do this in JAVA.
Do you understand?
- 11-09-2011, 12:45 PM #6
Similar Threads
-
Generating audio files from a binary source
By _max_ in forum New To JavaReplies: 2Last Post: 10-05-2011, 07:23 AM -
writing to binary files
By TopNFalvors in forum New To JavaReplies: 10Last Post: 03-04-2011, 08:29 AM -
Converting mp3 files to binary
By kylefrank in forum Advanced JavaReplies: 4Last Post: 06-09-2010, 08:25 PM -
Read binary files
By ronaldo121 in forum New To JavaReplies: 5Last Post: 03-04-2010, 03:37 AM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks