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

Recommended Answers

All 2 Replies

http://java.sun.com/docs/books/tutorial/essential/io/rafs.html

RandomAccessFile will allow you to read from the file but it will also allow you to access it at any point in the file, which it seems you already know. I don't see anywhere where you are *only* trying to read from the file (and if you were trying to read the whole file why use RAF?). Basically - What problem are you having?

http://java.sun.com/docs/books/tutorial/essential/io/rafs.html

RandomAccessFile will allow you to read from the file but it will also allow you to access it at any point in the file, which it seems you already know. I don't see anywhere where you are *only* trying to read from the file (and if you were trying to read the whole file why use RAF?). Basically - What problem are you having?

I need to know that how to write the code segment of search particular record which had been entered into a .DAT file,since it is a bit difference than the c++, please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.