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




Have Programming Question? Ask it here!
 
 
 


sushmasham
java io
1 Answer(s)      2 years and 2 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


Advertisement
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
java io
java io  by using java coding how to invoke a particular directory
Advertisements
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
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
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
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
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
How to read file in java
How to read file in java Java provides IO package to perform reading... file line by line  in java. FileInputStream- This class reads bytes from... file in Java: Another way of reading a file: The another program 
java - Java Interview Questions
Java io package hierarchy   What is the hierarchy for Java io Package
Using a Random Access File in Java
C:\nisha>javac RandAccessFile.java C:\nisha>java RandAccessFile Enter File name : Filterfile.txt Write...: C:\nisha>java ReadAccessFile Enter File name : Filterfile.txt
How to copy a file
How to copy a file In this section, you will learn how to copy a file using Java IO library. For this, we have declared a function called copyfile() which creates two File instances to specify source and destination file respectively
Java thread
Java thread  Why do threads block on I/O?   When a thread is suspended for a IO operation, it allows the IO operation to be completed and in that time some other thread which is not waiting for that IO gets a chance
java - Java Beginners
What is constructor in java?  What is constructor in java?  Hi,There a tutorial on roseindia.net at http://www.roseindia.net/java/example/java/io/ConstructingFileNamePath.shtmlPlease check the tutorial.I think
core java - Java Beginners
core java  what is object serialization ? with an example   Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/io/SerializingObject.shtml Thanks
Java - Java Interview Questions
Java  What is meant by Stream Tokenizer ?   hi friend, To understand the Stream Tokenizer, visit to: http://www.roseindia.net/java/example/java/io/tokenizingjavasourcecode.shtml Thanks RoseIndia Team
java - JavaMail
java  sir I want some java file codings pls send it.........  Hi Friend, Please visit the following code: http://www.roseindia.net/java/example/java/io/ Thanks
java - Java Interview Questions
://www.roseindia.net/java/example/java/io/SerializingObject.shtml http://www.roseindia.net/java/example/java/io/DeserializingObject.shtml Thanks
java - Java Interview Questions
Friend, Please visit the following links: http://www.roseindia.net/java/java-tips/background/60garbage_collection.shtml http://www.roseindia.net/java/beginners/GarbageCollection.shtml http://www.roseindia.net/java/example/java/io/code
java - Java Beginners
://www.roseindia.net/java/exceptions/index.shtml http://www.roseindia.net/java/example/java/io/ Thanks
FILEOUTPUTSTREAM and FILEINPUTSTREAM - Java Beginners
://www.roseindia.net/java/example/java/io/java-write-to-file.shtml http://www.roseindia.net/java/example/java/io/java-read-file-line-by-line.shtml Hope that the above links
java - Java Beginners
java  I can i pls have an example of serialization. Thank you.  Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java/io/SerializingObject.shtml http://www.roseindia.net
java - Java Beginners
java  Can i pls have a program for serialization?  Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java/io/SerializingObject.shtml http://www.roseindia.net/javatutorials
java - Java Beginners
java  pls tell me file handling tutorials with examples in detailed manner..  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/io/ Hope that it will be helpful for you
File copy through intranet - Java Beginners
File copy through intranet  Can i copy files from system A to System B connected through intranet?? Is this possible through java IO? If yes, please let me know
Read Text from Standard IO
operations on files.  Java also supports three Standard Streams...); Working with Reader classes: Java provides the standard I/O... ReadStandardIO.java C:\nisha>java ReadStandardIO
Readfile - Java Beginners
Readfile  How to read a file from place to another place in java  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/io/java-read-file-line-by-line.shtml Thanks
Java Swing - Java Beginners
/tutorial/java/io/createJarFile.html Hope that it will be helpful for you...Java Swing  Hi, I'm a java programmer, developed few java swing... or .exe is that possible? after converting is it necessary that java should
how to create a jar file for java classes? - Java Interview Questions
://www.roseindia.net/tutorial/java/io/createJarFile.html Hope that it will be helpful...how to create a jar file for java classes?  I have 2 classes x1 and x2. packages names are com.bank.xyz; how to create a jar file for classes
Java I/O Examples
provided by java. io package. This is a class that allows you to read... In this section we will discuss the Java IO Object Streams. Scanning... about the I/O from the command line in Java. Java IO Path
java - Java Beginners
/java/example/java/io/flat-file-xml.shtml http://www.roseindia.net/xml/dom...java   hi, i have one xml file .i want to parse the xml file ,validate the xml file and stored it into the mysql databzse. Iwant the sample code
JAVA - Java Interview Questions
link: http://www.roseindia.net/java/example/java/io/SerializingObject.shtml
Getting Current Directory from java - Java Beginners
://www.roseindia.net/java/java-get-example/get-CWD.shtml http://www.roseindia.net/java/example/java/io/GetCurrentDir.shtml Thanks... in linux where i need to get the current directory of my java file... I'm using
Serializaiton - Java Interview Questions
, serialization code in Java and serialization example Java Please visit the following serialization in Java tutorial here: http://www.roseindia.net/java/example/java/io.../java/beginners/StringBuffer.shtml Regards Meeya
Java I/O - Java Beginners
://www.roseindia.net/java/example/java/io/java-create-directory.shtmland run...Creating Directory Java I/O  Hi, I wanted to know how to create a directory in Java I/O?  Hi, Creating directory with the help of Java
explanation - Java Beginners
/GarbageCollection.shtml http://www.roseindia.net/java/example/java/io/code...explanation  I have create small java appication. I don't know about... java application.   Hi Friend, Please visit the following links
throws IOException - Java Beginners
, Most methods of IO class throw an IOException if anything goes wrong...://www.roseindia.net/java/exceptions/ http://www.roseindia.net/help/java/i/java-ioexception.shtml http://www.roseindia.net/tutorial/java/scjp/part2/question4.html Thanks
java program
java program  write a java program to read a file content into a string object using available and read methods of java bufferedinputstream.   Please visit the following link: http://www.roseindia.net/java/example/java
streams - Java Beginners
. Java's input and output (I/O) is based on streams. For more information,visit the following link: http://www.roseindia.net/java/example/java/io/ Thanks
Advertisements
 

 

 

DMCA.com