Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions RANDOM ACCESS FILE CONCEPT
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


SANTOSH AGRASINGH
RANDOM ACCESS FILE CONCEPT
1 Answer(s)      2 years and 4 months ago
Posted in : Java Interview Questions


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.


Advertisement
View Answers

June 13, 2012 at 1:42 PM


In the given code, we have used BufferedWriter class to write even number between 2 and 10 into text file.Then using the RandomAccessFile, we have wrote numbers from 1 to 5. The seek ()function displays the the last 3 digits of file.

import java.io.*;

class  RandomAccessFileExample
{
    public static void main(String[] args) 
    {
        try{
        File f=new File("c:/num.txt");
        BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));
        for(int i=2;i<=10;i++){
            if(i%2==0){
             bw.write(Integer.toString(i));
             bw.newLine();
            }
        }
            bw.close();
            RandomAccessFile randomFile=new RandomAccessFile(f,"rw");
            for(int i=1;i<=5;i++){
               randomFile.seek(f.length());
               randomFile.writeBytes(Integer.toString(i));
            }
             randomFile.close();
             RandomAccessFile access = new RandomAccessFile(f, "r");
             int i=(int)access.length();
             System.out.println("Length: " + i);
             access.seek(i-3);  
              for(int ct = 0; ct < i; ct++){
              byte b = access.readByte();
              System.out.println((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
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
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...() method- It closes the random access file stream. writeBytes( ) method
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
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
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
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!  ...[] args){ int min = 115; int max = 250; Random random = new Random
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
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
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
Pick at Random
the entry from the array before selecting another name at random
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
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
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 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
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
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
 

 

 

DMCA.com