Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2008, 08:57 AM
Member
 
Join Date: Dec 2007
Posts: 6
janakiram.attuluri is on a distinguished road
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 01:25 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-08-2008, 09:20 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-09-2008, 12:47 PM
Member
 
Join Date: Dec 2007
Posts: 6
janakiram.attuluri is on a distinguished road
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();
}




}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Binary File and Changing data janakiram.attuluri Advanced Java 1 12-21-2007 12:10 PM
reading a binary file with a RAF jkurth Advanced Java 2 12-20-2007 09:30 AM
Reading a file for use peachyco New To Java 2 11-27-2007 05:49 AM
How to load binary content of a .class file boy22 Advanced Java 1 08-03-2007 08:21 PM
Reading from a file leebee New To Java 1 07-23-2007 02:02 PM


All times are GMT +3. The time now is 11:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org