Share on Google+Share on Google+

sushmasham
java io
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners

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

Ads

View Answers

June 18, 2012 at 12:46 PM


In the given code, we have used RandomAccessFile to write number from 1 to 5 in the text file. 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");
        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();
             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:
java io - Java Beginners
Java IO Streams  What is the Java IO Stream
IO in java
IO in java  Your search mechanism will take as input a multi-word query and will return a list of documents where the given words appear. 1. Your score for this portion of the assignment will be based on the intelligence of your
Advertisements
java io
java io  by using java coding how to invoke a particular directory
Java IO
Java IO  What an I/O filter
java IO
java IO  Write a Java program to accept a file name as command line argument. Append the string "File Modified by Programme" to the end of the same file and print the contents of the modified File
java io
java io  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
io packages - Java Beginners
io packages  How can I add two integers in java using io pacages
java IO programing
java IO programing  how Java source file is used as input the program will echo lines
java binary file io example
java binary file io example  java binary file io example
IO File - Java Beginners
IO File  Write a java program which will read an input file & will produce an output file which will extract errors & warnings. It shall exclude the standard errors & warnings. The standard errors & warnings you can find out
java-io - Java Beginners
java-io  Hi Deepak; down core java io using class in myn...://www.roseindia.net/java/java-conversion/two-dimensional-array-program-using-nested-loop.shtml http://www.roseindia.net/java/beginners/SquareMatrix.shtml Thanks
Import java IO - Java Beginners
Import java IO  for example i know java IO is for input and output. I am using Netbeans5.5.1. How can i see all the classes related to java IO for example; stream reader, buffer reader
io - Java Beginners
. http://www.roseindia.net/java/ Thanks Amardeep
Simple IO Application - Java Beginners
Simple IO Application  Hi, please help me Write a simple Java application that prompts the user for their first name and then their last name. The application should then respond with 'Hello first & last name, what
io
io  create a meanu based text editor in java having features of creating file,viewing file,deleting file,renaming file,copying file and cutting file
io
io  write a program in java to accept a filename from user and print its content on the monitor   Hello Friend, Try the following code: import java.io.*; import java.util.*; class DisplayContentOfFile { public
Java IO Path
Java IO Path In this section we will discuss about the Java IO Path. Storage of a file or folder(Directory/Subdirectory) in a Hard Drive or other media... without complete information. To work with Path in Java, the Path class
Difference between Java IO Class - Java Beginners
Difference between Java IO Class  What is the difference in function between Two set of Stream class as mention below- 1)FileInputStream... information. http://www.roseindia.net/java/ Thanks
Working With File,Java Input,Java Input Output,Java Inputstream,Java io Tutorial,Java io package,Java io example
:\nisha>java CreateFile1 New file "myfile.txt" has been created... CreateFile1.java C:\nisha>java CreateFile1 The specified file
core java ,io operation,calling methods using switch cases
core java ,io operation,calling methods using switch cases  How to create a dictionary program,providing user inputs using io operations with switch cases and providing different options for searching,editing,storing meanings
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
IO concept
IO concept  Write a java program that moves the contents of the one file to another and deletes the old file.   Hi Friend, Try...!"); } } For more information, visit the following link: Java Move File Thanks
Java IO FilterWriter
Java IO FilterWriter In this example we will discuss about the FilterWriter in Java. java.io.FilterWriter is an abstract class allows to write the filtered...(); bw.write("Java IO FilterWriter Example"); System.out.println("Data is written
Java IO StringReader
Java IO StringReader In this section we will discuss about the StringReader class in Java. StringReader class reads the string from the input stream...; String str = "\n Java IO StringReader \n"; try
Java IO FileReader
Java IO FileReader In this tutorial we will learn about the FileReader class in Java. java.io.FileReader class is used to read the character streams. To read... you about how to use java.io.FileReader. In this example I have created a Java
Java IO OutputStreamWriter
Java IO OutputStreamWriter In this section we will learn about the Java..."); osw = new OutputStreamWriter(os); osw.write("Java IO..."); bw.newLine(); bw.write("Java IO OutputStreamWriter Example
Java IO CharArrayReader
Java IO CharArrayReader In this tutorial we will learn about the CharArrayReader class in Java. CharArrayReader class in java.io package provides... the stream of char array. In this example I have created a Java class to read
Java IO FilterReader
Java IO FilterReader In this section we will learn about the FilterReader class in Java. java.io.FilterReader is an abstract class provides the facility... created a Java class named JavaFilterReaderExample.java. In this class I have used
Java IO PipedWriter
Java IO PipedWriter In this tutorial we will learn about the PipedWriter in Java. java.io.PipedWriter writes the characters to the piped output stream. Characters written to the piped output stream can be read by the corresponding
Java IO StringWriter
Java IO StringWriter In this section we will discussed about the StringWriter in Java. java.io.StringWriter writes the String to the output stream. To write... in the Java program. In this example I have created a Java class named
Java IO Reader
Java IO Reader In this section we will discuss about the Reader class in Java. Reader class is an abstract class in java.io package provided for reading... a Java class named JavaReaderExample.java into which created an input stream
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads