Posts

Showing posts with the label File IO

Program to create text file, write some data and read the same using single character method without using string functions

 In this tutorial, you'll learn how to create TEXT file, write some data to file and read the data from text file. Before going to start you should aware of the following concepts: Java File IO FileWriter class FileReader class

Program to split the words in sentence of string without using split in Java

 In this program, you'll learn how to split the words in sentence of string without using split() function. Hello Guys, if you know about split( ) method of String class, it can be used to split the sentence into words. Here in this program we will split the using without using this split() method. So let us start.

Program to read records from file "stud.dat" and calculate total and percentage

Here, we read the details of students from file and display the total marks and percentage of each student on console. The  DataInputStream  class is used in the context of DataOutputStream and can be used to read primitives. Following is the constructor to create an InputStream: DataInputStream in = DataInputStream (InputStream in); PROGRAM import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; class ReadRecordFromFile { public static void main(String[] args) { try { File f=new File("stud.dat"); FileInputStream fos=new FileInputStream(f); DataInputStream dos=new DataInputStream(fos); int roll_no,m1,m2,tot; float per; String name;

Program to store the entered details (records) in a file named "stud.dat"

In this program, we have read the details of students like roll no, name, marks1, marks2 and store them in a file using  DataOutputStream  class. The  DataOutputStream  stream lets you write the primitives to an output source. Following is the constructor to create a DataOutputStream. DataOutputStream out = DataOutputStream( OutputStream out); PROGRAM import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Scanner; class StoreRecordInFile { public static void main(String[] args) { try { File f=new File("stud.dat"); FileOutputStream fos=new FileOutputStream(f); DataOutputStream dos=new DataOutputStream(fos);

Program to copy contents of one file into other file

This program copied the contents of one file into other file suing  FileInputStream  and  FileOutputStream  classes. The name of files for reading and writing are passed through command line arguments. PROGRAM import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; class CopyFileContentsInOtherFile { public static void main(String[] args) { try { File f1=new File(args[0]); File f2=new File(args[1]);

Implement a program to count number of lines and words in the file

Using a random access file, we can read from a file as well as write to the file. Reading and writing using the file input and output streams are a sequential process. Using a random access file, we can read or write at any position within the file. An object of the RandomAccessFile class can do the random file access. We can read/write bytes and all primitive types values to a file. RandomAccessFile can work with strings using its readUTF() and writeUTF() methods. We create an instance of the RandomAccessFile class by specifying the file name and the access mode. RandomAccessFile raf = new RandomAccessFile("randomtest.txt", "rw"); A random access file has a file pointer that moves forward when we read data from it or write data to it. We can get the value of file pointer by using its getFilePointer( ) method. When we create an object of the RandomAccessFile class, the file pointer is set to zero. We can set the file pointer at a specific locat...