• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

reading binary file in java(with code also)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my c++ program

#include<iostream>
#include<fstream>

using namespace std;

struct cdata
{
char tcode[6];
char ttype[1];
char fname[12];
char lname[14];
char ipur[52];
char ipri[5];
char tdate[8];
}data;

int main()
{
char choice='Y';

ofstream out("DataFile",ios: ut|ios::binary);

if(!out)
{
cout<<"can not create data file\n";
exit(1);
}

while(choice=='Y')
{
cout<<"Enter transation code:";
cin>>data.tcode;
cout<<"Enter transation type:";
cin>>data.ttype;
cout<<"Enter First Name:";
cin>>data.fname;
cout<<"enter last Name:";
cin>>data.lname;
cout<<"Enter item purchase:";
//fflush(stdin);
cin>>data.ipur;
cout<<"Enter item prise:";
//cin>>data.ipri;
getline(cin,data.ipur);
cout<<"Enter date of tram:";
cin>>data.tdate;

out.write((char *)&data,sizeof(data));
fflush(stdin);

cout<<"Want to enter more record[Y/N]";
cin>>choice;

if(choice!='Y')
break;
}

out.close();
return(0);
}

and here is my java program

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class MappedChannelRead
{

public static void main(String args[])
{
FileInputStream fIn;
FileChannel fChan;
long fSize;
byte buf[];//=new byte[98];
MappedByteBuffer mBuf;
int pos,lim;
String str=null;
try
{
fIn=new FileInputStream("DataFile");
fChan=fIn.getChannel();
fSize=fChan.size();

mBuf=fChan.map(FileChannel.MapMode.READ_ONLY,0,fSize);

pos=mBuf.position();
lim=mBuf.limit();

while(pos<=lim)
{
buf=new byte[98];
if(buf.length<mBuf.remaining())
{
mBuf.get(buf,0,98);
str=new String(buf,33,52);
System.out.print(str);
str=new String(buf,90,8);
System.out.println("--"+str);
buf=null;
pos=mBuf.position();
}
else
{
mBuf.get(buf,0,mBuf.remaining());
str=new String(buf,33,52);
System.out.print(str);
str=new String(buf,90,8);
System.out.println("--"+str);
buf=null;
pos=mBuf.position();
break;
}
//System.out.println();
//pos=mBuf.position();
}
fChan.close();
fIn.close();
}catch(IOException e){
System.out.println(e);
}
}
}


the problem is that jvm does not allocate new memory for string str as we know that strings are immutable means we cant change the contents of string in my progeam i use specific bytes of array and create a string so for all string object it has to allocate memory at different position but i think jvm is not allocation new memory and it use the previously allocated memory may be it is some optimization.In my program i create a 'buf' every time when i read a record from ByteBuffer so every time the content of buf is change according to file record the problem is in str when i create string with some part of byte it does not create a new string it uses the previously allocated string


So please solve my problem along with code if possible
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

You might be getting a little ahead of yourself here. Your definition of immutable Strings (or immutable objects, more generally speaking) seems a little confusing. Try to forget about it for the time being.
The real problem seems to be your while(pos<=lim). Did you check if the buffer's position is incremented along the loop? For as far as I know, the get() method does not increment the buffer's position, so you would have to set it using mBuf.position(someLoopVariable).
Maybe you should try to use just an input stream to begin with as the code would be more readable, and then refactor the working code to use a channel. Best regards,

Henrique
 
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One important issue here, by the way, is that the format of the binary file written by this program will vary by platform and even by compiler! The layout of a struct is not specified by the C standard. There may or may not be padding bytes inside the struct, and of course you'd have to know about those before trying to read the data in Java (or in C using another platform.) Examining the file with a hex editor is one way to figure out the layout on your platform.

Another issue is that Java strings aren't zero-terminated. If in your C program, one of your strings is smaller than the size you're allocating, there will be a zero at the end, followed by garbage. In Java, you'll get a String that's the full width of the field, including the zero and all the garbage. You'll have to actually scan through the buffer, find the zero, and then create the Java string based on your computed string length, not the size of the field in the file.
 
reply
    Bookmark Topic Watch Topic
  • New Topic