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