I wonder if i writing the correct code segment to read data from binary file.eg.the content
file operation in constructor
try{
raf = new RandomAccessFile("record.dat","rw");
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null,"Error create file");
System.exit(0);
}
write content to file
do
{
Name = JOptionPane.showInputDialog(null,"Please Enter The Account Holder's Name");
}while(Name==null || Name.length()==0);
do
{
ANumber = JOptionPane.showInputDialog(null,"Please Enter The Account Number");
}while(ANumber==null || ANumber.length()==0);
try{
raf.seek(raf.length());
raf.writeUTF(Name);
raf.writeUTF(ANumber);
raf.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
Read content from file:
if(source == signinButton)
{ String stra = JOptionPane.showInputDialog(null,"Please enter the NAME to open existing account");
String strb = JOptionPane.showInputDialog(null,"Please enter the ACCOUNT NUMBER");
try{
DataInputStream input = new DataInputStream(new FileInputStream("record.dat"));
raf.seek(0);
while (raf.getFilePointer() < raf.length()) {
if(input.readUTF() == strb)
{
txtTextArea.append(input.readUTF()+"\n");
txtTextArea.append(input.readUTF()+"\n");
}
}}
catch(IOException ex)
{JOptionPane.showMessageDialog(null,"Invalid Numerals Added");
}}
for example if i enter two sets of data Ivan,456 and Amoo,231 ,all of it could be display correctly if the part of search specified sets of data with the :
while (raf.getFilePointer() < raf.length()) {
if(input.readUTF() == strb)
{}}
has been take off.Instead ,if if i embed it ,there is no data to be display