RANDOM ACCESS FILE CONCEPT

RANDOM ACCESS FILE CONCEPT

Write a program to write the details of an employee to a file. Details such as year of joining, department code, employee name and salary should be included. You must use RandomAccessFile to accomplish this. Now, read the data that has been written in the previous step and display it on the console. Using the seek pointer, retrieve the employee name and display it.

View Answers

June 15, 2012 at 3:42 PM

The given code accept the details from the user like year of joining, department code, employee name and salary. This data is then stored into the text file using RandomAccessFile class which is then displayed on the console.

import java.io.*;

class  RandomAccessFileExample
{
    public static void main(String[] args) 
    {
        try{
            File f=new File("c:/employee.txt");
            RandomAccessFile randomFile=new RandomAccessFile(f,"rw");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter year of joining: ");
            int year=Integer.parseInt(br.readLine());

            System.out.print("Enter department code: ");
            String dcode=br.readLine();

            System.out.print("Employee Name: ");
            String name=br.readLine();

            System.out.print("Salary: ");
            long sal=Long.parseLong(br.readLine());

            randomFile.seek(f.length());
            randomFile.writeBytes(Integer.toString(year)+" "+dcode+" "+name+" "+Long.toString(sal));
            randomFile.close();
            RandomAccessFile access = new RandomAccessFile(f, "r");
            access.seek(0);  
            int len=(int)access.length();
              for(int i = 0; i < len; i++){
              byte b = access.readByte();
              System.out.print((char)b); 
              }
        }
        catch(Exception e){}
    }
}









Related Tutorials/Questions & Answers:
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  In a random access file, first write the alphabets a,b, c, d till z. Read the file in reverse order and print it on screen
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program that stores the names of four employees in an array of strings. Store the contents of the array in an alphabetical order in a file and display it back on console. Do not use
Advertisements
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to use a File object and print even numbers from two to ten. Then using RandomAccessFile write numbers from 1 to 5. Then using seek () print only the last 3 digits
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to write the details of an employee to a file. Details such as year of joining, department code, employee name and salary should be included. You must use RandomAccessFile
Random Access File.
Random Access File.  Explain Random Access File
Random Access File class
Random Access File class  Write a short note on Random Access File class.   Please visit the following link: Java Random Access File
ModuleNotFoundError: No module named 'random-access-file-reader'
ModuleNotFoundError: No module named 'random-access-file-reader'  Hi...: No module named 'random-access-file-reader' How to remove the ModuleNotFoundError: No module named 'random-access-file-reader' error? Thanks  
Using a Random Access File in Java
RandAccessFile Enter File name : Filterfile.txt Write... the characters from a file using the readByte( ) method...;= in.readLine();   File file = new File(str
FILE CONCEPT
FILE CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does not exist print
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Write a program to read and write the details of an employee like year of joining, Department code, salary using some byte stream classes
Upload file to MS Access
Upload file to MS Access  Iam beginner and need to upload file to msaccess using jsp . Iam using netbeans platform. Kindly explain do I need to mention Attachment format in the table
ls: cannot access >: No such file or directory
ls: cannot access >: No such file or directory  import java.io.BufferedReader; import java.io.InputStreamReader; public class Example...: cannot access >: No such file or directory error is displayed .No f.txt
How to use random access file
How to use random access file In this section, you will learn the use... is used for both reading and writing files. A random access file treats... of the file. rand.close() method- It closes the random access file stream.ADS
Want to access my Ms-access Db file which is placed inside the same Jar file
Want to access my Ms-access Db file which is placed inside the same Jar file  how do i access my Ms-Access file placed in the same jar file where my application code/class file r present??? Want to access it via Code
Maven Repository/Dependency: eu.codlab | kotlin-file-access
Maven Repository/Dependency of Group ID eu.codlab and Artifact ID kotlin-file-access. Latest version of eu.codlab:kotlin-file-access dependencies. # Version Release Date 1
Access Excel file through JDBC
Access Excel file through JDBC In this section, you will learn how to access excel file through Jdbc and display records in JTable. As you know Excel comes... and the excel file. Here the name of the worksheet is equivalent to any database
Maven Repository/Dependency: eu.codlab | kotlin-file-access-android
Maven Repository/Dependency of Group ID eu.codlab and Artifact ID kotlin-file-access-android. Latest version of eu.codlab:kotlin-file-access-android dependencies. # Version Release Date
Maven Repository/Dependency: eu.codlab | kotlin-file-access-jvm
Maven Repository/Dependency of Group ID eu.codlab and Artifact ID kotlin-file-access-jvm. Latest version of eu.codlab:kotlin-file-access-jvm dependencies. # Version Release Date 1
How and Where we Access struts Properties file - Struts
How and Where we Access struts Properties file   Hi Friends, am new to struts.I read that " you can store variables in xml file(properties file) and for future enhancements we just call properties file . Now i want to implement
how to access the messagresource.proprties filevalues in normal class file using struts - Struts
how to access the messagresource.proprties filevalues in normal class file using struts  i want to access the below username and password in class... password=system My class file is import java.io.PrintStream; import
access
access   This is Nitha Sriram i am having a problem that, i am having a combo box in the JSP page when i select an item from that i am calling the onchange function. In javascript i am getting the value now i want to pass
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
how to store image file and videofile in ms access database using java? - JDBC
how to store image file and videofile in ms access database using java?  how to store image file and video file in ms access database using java
Java file not found exception
Java file not found exception This section illustrates you the concept of file not found exception. Java provides a powerful concept of Exceptions... the concept of file not found exception. Output
getting random number in java
access to random class in java Created the object of Random class like - Random...getting random number in java  getting random number in java Hi everyone, i am trying to get a random number in a particular range. for example
Java file exception
Java file exception This section illustrates you the concept of file exception... to understand the concept of file exception. Output:ADS_TO_REPLACE_2... or it is generated by a code.  In the given example, we have specified a file
why it is very slow when I am using the backend as microsoft access file for running the java webapplication - SQL
why it is very slow when I am using the backend as microsoft access file for running the java webapplication  I am using the backend as microsoft access file for running the java webapplication.it is very slow when twoor mor
What is HBase?
by the need to use Big Data. It is highly useful when random access and writing...-time access to large data sets for both read and write functionalities. HBase... very quick access to the queried information. How does HBase Work?ADS
random number
random number  Sir could u please send me the code registration form of roseindia.net with image varification
random number
random number   Please How do I generate a program that gives me random integer from 115 to 250? Using java.util.random. Thank you very much!  ... main(String[] args){ int min = 115; int max = 250; Random random
random numbers
random numbers  hi.. i am creating a website and on feedback form to authenticate user i want to generate a random number on label.if user types that number correctly in textbox then he is allowed to submit feedback otherwise
Java file object
Java file object This section demonstrates you the concept of File object... the programming much easier. Here we will discus the operations performed by File object. Using the File object, you can perform several operations. It parses
Random Creation of password
Random Creation of password  Dear Sir I have created a form with some... has my back end,....My database is Ms Access...my problem is that i am not able to generate a random password and insert into database... Please help me out
Access Specifiers and Access Modifiers
Access Specifiers and Access Modifiers  What are Access Specifiers and Access Modifiers
Explain normalization concept?
Explain normalization concept?  Explain normalization concept
IO concept
file to another and deletes the old file.   Hi Friend, Try...[] args) throws Exception { File f1=new File("C:/data.txt"); File f2=new File("D:/data.txt"); BufferedReader br=new BufferedReader(new
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf using itext?  I am writing a pdf using itext where i add a image in the beginning of the document. When i use image = Image.getInstance("C
Pick at Random
the entry from the array before selecting another name at random
random numbers
random numbers  Hi I need some help with the code for the this program. This program should randomly choose a number between 1 and 1000, and then challenge the user to guess what it is, giving hints ('too high!' or 'too low
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to retreive values from MS Access Database based on the values entered... in MS Access Database. the table structure is Sno JobName ProgramName Problem... a jsp file which contains a textbox, name issuedescription. when user types
access specifiers and access modifiers
access specifiers and access modifiers  I want a powerpoint presentation for access specifiers and access modifiers. Please send me the same and soon
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
How to Access MS Access in jar.
How to Access MS Access in jar.  how do i access my Ms-Access file placed in the same jar file where my application code/class file r present??? Want... this ... i m able to access a Ms-access via JDBC but cant find the file wen
access into radio button
access into radio button  access the four fields of database called access,access file and stored in radio button.please help
Concept of subqueires using JSP
Concept of subqueires using JSP  Can I implement the concept of sub queries in JSP page , if yes please tell me how this can be achieved. I have requirement where output of the SQL query should be given as input to the update

Ads