473,467 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Java and RandomAccessFile - EDITing content

15 New Member
Hey all,

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
  1.  
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5.  
  6.  
  7. public class AWT_ToRAF extends Frame implements ActionListener {
  8.  
  9.     public static Label userName_L, fName_L, lName_L;
  10.     public static TextField userName,firstName,lastName;
  11.  
  12.     public AWT_ToRAF() {
  13.  
  14.  
  15.         setSize(500,500);
  16.         setLayout(null);
  17.  
  18.  
  19.         userName_L = new Label("Enter User Name:");
  20.         userName_L.setBounds(7,60,100,20);
  21.         userName = new TextField();
  22.         userName.setBounds(7,80,100,20);
  23.  
  24.  
  25.         fName_L = new Label("Enter First Name");
  26.         fName_L.setBounds(7,110,100,20);
  27.         firstName = new TextField();
  28.         firstName.setBounds(7,130,100,20);
  29.  
  30.  
  31.         lName_L = new Label("Enter Last Name");
  32.         lName_L.setBounds(7,150,100,20);
  33.         lastName = new TextField();
  34.         lastName.setBounds(7,170,100,20);
  35.  
  36.  
  37.         Button addToFile = new Button("Add Record To File");
  38.         addToFile.setBounds(7,200,130,30);
  39.         addToFile.addActionListener(this);
  40.  
  41.         add(userName_L);
  42.         add(userName);
  43.         add(fName_L);
  44.         add(firstName);
  45.         add(lName_L);
  46.         add(lastName);
  47.         add(addToFile);
  48.  
  49.  
  50.         show();
  51.  
  52.  
  53.     }
  54.  
  55.  
  56.     public static void main(String[] args)
  57.     {
  58.  
  59.         new AWT_ToRAF();
  60.  
  61.  
  62.  
  63.     }
  64.  
  65.     public void actionPerformed(ActionEvent ae)        
  66.     {
  67.         String str = ae.getActionCommand();
  68.         boolean result = false;
  69.  
  70.         if(str.equals("Add Record To File"))
  71.             result = addToRAF("students.txt");
  72.  
  73.         if(result == true)
  74.             System.out.println("Record added successfully!");
  75.  
  76.         readFromFile("students.txt");
  77.  
  78.  
  79.  
  80.  
  81.     }    
  82.  
  83.     public boolean addToRAF(String fileName)
  84.     {
  85.         long recordNumber = 0;
  86.  
  87.         String uName,fName,lName;
  88.  
  89.         try{
  90.  
  91.             RandomAccessFile file = new RandomAccessFile(fileName,"rw");
  92.             uName = userName.getText();
  93.             fName = firstName.getText();
  94.             lName = lastName.getText();
  95.  
  96.  
  97.             file.seek(file.length());
  98.             file.writeUTF(uName);
  99.             file.writeUTF(fName);
  100.             file.writeUTF(lName);
  101.  
  102.             file.close();    
  103.  
  104.  
  105.         }
  106.         catch(IOException e){
  107.  
  108.         }
  109.  
  110.         return true;
  111.  
  112.     }
  113.  
  114.  
  115.     public void readFromFile(String str)
  116.     {
  117.  
  118.         try
  119.         {
  120.  
  121.             RandomAccessFile raf = new RandomAccessFile(str,"r");
  122.             int i;
  123.  
  124.             String uName,fName,lName;
  125.  
  126.             for(i=0; i<raf.length();i++)
  127.             {
  128.                 uName = raf.readUTF();
  129.                 System.out.println("UserName :" + uName);
  130.                 fName = raf.readUTF();
  131.                 System.out.println("First Name:" + fName);
  132.                 lName = raf.readUTF();
  133.                 System.out.println("Last Name:" + lName);
  134.  
  135.  
  136.             }
  137.  
  138.             raf.close();
  139.         }catch(IOException e){}
  140.  
  141.         searchFromFile("students.txt");    
  142.  
  143.     }
  144.  
  145.  
  146.  
  147.     public void searchFromFile(String str)
  148.     {
  149.  
  150.         try
  151.         {
  152.  
  153.  
  154.         RandomAccessFile file = new RandomAccessFile(str,"r");
  155.  
  156.         String search,uName;
  157.  
  158.         search = TextIO.getWord();
  159.  
  160.         int i;
  161.  
  162.         file.seek(0);
  163.  
  164.         for(i=0; i<file.length();i++)
  165.         {
  166.             uName = file.readUTF();
  167.  
  168.             if(uName.equals(search))
  169.             {
  170.                 System.out.println(file.readUTF());                
  171.                 System.out.println(file.readUTF());    
  172.             }
  173.             else
  174.                 System.out.println("No such ID");                
  175.  
  176.         }
  177.  
  178.         file.close();
  179.  
  180.  
  181.         }catch(IOException e){
  182.         }
  183.  
  184.  
  185.         editDetails("students.txt");
  186.  
  187.     }
  188.  
  189.  
  190.  
  191.  
  192.     public void editDetails(String str)
  193.     {
  194.              try
  195.         {
  196.                     RandomAccessFile file = new RandomAccessFile(str,"rw");
  197.  
  198.  
  199.                     String search,uName;
  200.  
  201.                     String newFname,newLname;
  202.  
  203.                     search = TextIO.getWord();
  204.  
  205.                     int i;
  206.  
  207.                     file.seek(0);
  208.  
  209.                     for(i=0; i<file.length();i++)
  210.                     {
  211.                         uName = file.readUTF();
  212.  
  213.                         if(uName.equals(search))
  214.                         {
  215.                         System.out.println("Enter new First Name");
  216.                         newFname = TextIO.getWord();                
  217.                         file.writeUTF(newFname);
  218.  
  219.                         System.out.println("Enter new Last Name");
  220.                         newLname = TextIO.getWord(); 
  221.                         file.writeUTF(newLname);
  222.                         }
  223.  
  224.                     }
  225.  
  226.  
  227.                 file.close();    
  228.                 }catch(IOException e){
  229.                 }
  230.                 System.exit(0);
  231.  
  232.     }
  233.  
  234.  
  235.  
  236. }
  237.  
  238.  
  239.  
Sep 26 '07 #1
0 1598

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nospam | last post by:
Amazon wins patent for ordering forms, Collapsing and Maximizing Form Areas.... NAME OF PATENT Method and system for displaying and editing of information # 6,615,226 ...
4
by: nospam | last post by:
Amazon wins patent for ordering forms, Collapsing and Maximizing Form Areas.... NAME OF PATENT Method and system for displaying and editing of information # 6,615,226 ...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: jyohere | last post by:
I want to know which one will be faster randomAccessFile or Bufferedeader. I am reading a file about 18 MB. For that using randomAccessFile i will read from the beginning of the file the first time...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.