Results 1 to 3 of 3
Thread: Reading a Field in a Binary File
- 01-08-2008, 06:57 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
Reading a Field in a Binary File
Hi All,
I have a binary file to read which contains the fields i specified in FileStructure. I read tht using DataInputStream.
example i gave u only some fields but there are 150 fields r thr those r short int and char[] s.
wht is the way of reading tht. output should print the values of variables i specified in the constructor.
class FileStructure I used only for ur reference. data i hav to read from binary file.
Class FileStructure
{
short aaaaa;
short bbbbb;
int ccccc;
int ddddd;
int eeeee;
char[] ffffff[2];
short gggg;
public FileStructure()
{
aaaaa= 640;
bbbbb= 304044;
ccccc=0;
ddddd=0;
eeeee=0;
gggg=0;
}
}
public class ReadFile
{
public static void main(String args[])
{
File f=new File("C:\\Documents and \\VRP_U071201092710OSS00012.DAT");
FileInputStream in = new FileInputStream(f);
DataInputStream dataInputStream = new DataInputStream(in);
short s=dataInputStream.readShort();
short s1=dataInputStream.readShort();
// how to loop this thr r multiple short and ints and char[] are thr.
}
}Last edited by janakiram.attuluri; 01-08-2008 at 11:25 AM.
- 01-08-2008, 07:20 AM #2
Java Code:public class FileStructure { public static void main(String[] args) throws Exception { Object data[] = new Object[150]; for(int i = 0 ; i < 150 ; i ++){ data[i] = (short)43 ; // It is just an example /* * Autoboxing allows you to add any primitive char and bytes ... */ } for(int i = 0 ; i < 150 ; i ++){ if(data[i] instanceof Short ){ System.out.println("Short Data :" + data[i]); } // Similarly you can check with other primitive Wrappers .. } } }dont worry newbie, we got you covered.
- 01-09-2008, 10:47 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
Hi,
I am able to read it in this way. But, i am not sure abt the program
I have to find a value in the binary file which is at 628th byte(short value) which is 0 i have to change tht to 1 i did tht. now i need to write tht back to the same file in a binary format how to do tht?
please help me
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadBinaryBuffer
{
public static void main(String args[])
{
try {
File file = new File("C:\\Documents and Settings\\jattulu\\files\\VRP_U071201092710OSS0001 2.DAT");
FileInputStream fin = new FileInputStream(file);
FileChannel fc = fin.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect((int)fc.size());
fc.read(buf);
byte buffer[] = new byte[(int)fc.size()];
//BIG_ENDIAN is the default
buf.order(ByteOrder.LITTLE_ENDIAN);
buf.rewind();
buf.get(buffer);
ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
BufferedReader reader = new BufferedReader(new InputStreamReader(bin));
DataInputStream stream = new DataInputStream(bin);
buf.position(628);
short s=buf.getShort();
System.out.println("cocot_ind value:"+s);
if(s==0)
{
buf.putShort(628,(short)1);
}
buf.position(628);
short s1=buf.getShort();
System.out.println("cocot_ind value after change:"+s1);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Similar Threads
-
Reading Binary File and Changing data
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-21-2007, 10:10 AM -
reading a binary file with a RAF
By jkurth in forum Advanced JavaReplies: 2Last Post: 12-20-2007, 07:30 AM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 03:49 AM -
How to load binary content of a .class file
By boy22 in forum Advanced JavaReplies: 1Last Post: 08-03-2007, 06:21 PM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks