Need some Java help here. This is a code fragment that does a few things.
1) Allows adding to a RAF file.
2) Searches for a match and displays related records.
and
3) Searches for a match and accepts new data and modifies it right there.
It is # 3 I am having issues with. If the name previously stored was Sommerson and if a new name 'Joe' was entered then it goofs up since there is no trimming involved. Its my first time with this kind of RAF so please HELP and do suggest better and more efficient ways to edit records in a RAF file! :)
Thanks guys.
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.io.*;
- import java.awt.event.*;
- public class AWT_ToRAF extends Frame implements ActionListener {
- public static Label userName_L, fName_L, lName_L;
- public static TextField userName,firstName,lastName;
- public AWT_ToRAF() {
- setSize(500,500);
- setLayout(null);
- userName_L = new Label("Enter User Name:");
- userName_L.setBounds(7,60,100,20);
- userName = new TextField();
- userName.setBounds(7,80,100,20);
- fName_L = new Label("Enter First Name");
- fName_L.setBounds(7,110,100,20);
- firstName = new TextField();
- firstName.setBounds(7,130,100,20);
- lName_L = new Label("Enter Last Name");
- lName_L.setBounds(7,150,100,20);
- lastName = new TextField();
- lastName.setBounds(7,170,100,20);
- Button addToFile = new Button("Add Record To File");
- addToFile.setBounds(7,200,130,30);
- addToFile.addActionListener(this);
- add(userName_L);
- add(userName);
- add(fName_L);
- add(firstName);
- add(lName_L);
- add(lastName);
- add(addToFile);
- show();
- }
- public static void main(String[] args)
- {
- new AWT_ToRAF();
- }
- public void actionPerformed(ActionEvent ae)
- {
- String str = ae.getActionCommand();
- boolean result = false;
- if(str.equals("Add Record To File"))
- result = addToRAF("students.txt");
- if(result == true)
- System.out.println("Record added successfully!");
- readFromFile("students.txt");
- }
- public boolean addToRAF(String fileName)
- {
- long recordNumber = 0;
- String uName,fName,lName;
- try{
- RandomAccessFile file = new RandomAccessFile(fileName,"rw");
- uName = userName.getText();
- fName = firstName.getText();
- lName = lastName.getText();
- file.seek(file.length());
- file.writeUTF(uName);
- file.writeUTF(fName);
- file.writeUTF(lName);
- file.close();
- }
- catch(IOException e){
- }
- return true;
- }
- public void readFromFile(String str)
- {
- try
- {
- RandomAccessFile raf = new RandomAccessFile(str,"r");
- int i;
- String uName,fName,lName;
- for(i=0; i<raf.length();i++)
- {
- uName = raf.readUTF();
- System.out.println("UserName :" + uName);
- fName = raf.readUTF();
- System.out.println("First Name:" + fName);
- lName = raf.readUTF();
- System.out.println("Last Name:" + lName);
- }
- raf.close();
- }catch(IOException e){}
- searchFromFile("students.txt");
- }
- public void searchFromFile(String str)
- {
- try
- {
- RandomAccessFile file = new RandomAccessFile(str,"r");
- String search,uName;
- search = TextIO.getWord();
- int i;
- file.seek(0);
- for(i=0; i<file.length();i++)
- {
- uName = file.readUTF();
- if(uName.equals(search))
- {
- System.out.println(file.readUTF());
- System.out.println(file.readUTF());
- }
- else
- System.out.println("No such ID");
- }
- file.close();
- }catch(IOException e){
- }
- editDetails("students.txt");
- }
- public void editDetails(String str)
- {
- try
- {
- RandomAccessFile file = new RandomAccessFile(str,"rw");
- String search,uName;
- String newFname,newLname;
- search = TextIO.getWord();
- int i;
- file.seek(0);
- for(i=0; i<file.length();i++)
- {
- uName = file.readUTF();
- if(uName.equals(search))
- {
- System.out.println("Enter new First Name");
- newFname = TextIO.getWord();
- file.writeUTF(newFname);
- System.out.println("Enter new Last Name");
- newLname = TextIO.getWord();
- file.writeUTF(newLname);
- }
- }
- file.close();
- }catch(IOException e){
- }
- System.exit(0);
- }
- }