OOPS Lab-9 [ File Handling ]
Post date: Nov 28, 2017 6:02:45 AM
1. Create File:- For creating a new file File.createNewFile( ) method is used. This method returns a boolean value true if the file is created otherwise return false. If the mentioned file for the specified directory is already exist then the createNewFile() method returns the false otherwise the method creates the mentioned file and return true.
import java.io.*;
public class CreateFile1{
public static void main(String[] args) throws IOException{
File f;
f=new File("myfile.txt");
if(!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.txt\" has been created to the current directory");
}
else
System.out.println("The specified file is already exist");
}
}
Read File:-
import java.io.*;
class FileRead{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Program of File handling
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class f {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Your file should be placed at : " + System.getProperty("user.dir"));
Scanner readMyFile = new Scanner(new File(args[0]));
//Open file for writing content
System.out.println("Output file will be created at : " + System.getProperty("user.dir"));
PrintWriter writeToMyFile = new PrintWriter(new File(args[1]));
while(readMyFile.hasNext()) {
// Read the content of input file
// Read 3 integers
int a=readMyFile.nextInt();
int b=readMyFile.nextInt();
int c=readMyFile.nextInt();
//Read the string
String name=readMyFile.next(); //not readMyFile.nextLine()
//Read a float
float f=readMyFile.nextFloat();
//Display the content of input file
System.out.printf("%d %d %d %s %f%n", a,b,c,name,f);
//You can also use System.out.print to display one data type at a time.
/*
* %n is a new line character appropriate to the platform running the application.
* You should always use %n, rather than \n.
*/
//Write data to file
int result=(a*a) + (b*b) +(c*c);
System.out.format("(a*a + b*b + c*c) = %d %s %f%n", result, name, f);
writeToMyFile.format("(a*a + b*b + c*c) = %d %s %f%n", result, name, f);
/*
* Again writeToMyFile.print can be use to write one data type at a type.
*/
}
readMyFile.close();
writeToMyFile.close();
}
}
/* now try to read inputs for these fields from keyboard */
Contents of the input file: input.txt
0 1 2 Uma 3.4
1 2 3 Saroja 4.5
2 3 4 Nirmala 5.6
3 4 5 Varun 6.7
4 5 6 Vijay 7.8
Contents of the created output file
a*a + b*b + c*c)= 5 Uma 3.400000
(a*a + b*b + c*c)= 14 Saroja 4.500000
(a*a + b*b + c*c)= 29 Nirmala 5.600000
(a*a + b*b + c*c)= 50 Varun 6.700000
(a*a + b*b + c*c)= 77 Vijay 7.800000
COMPILATION, LINKING and EXECUTION STEPS
-----------------------------------------
$javac f.java
$java f input.txt output.txt
OOPS Lab on Files
1. Write a program to read contents from a file called InputData and write all consonants to a file called Consonant and all vowels to a file called Vowel.
a. Use File Input and Output Streams
b. Use File Reader and file Writer
2. 2. Write a program to read the details of five items from the user, the details include item no, description, unit price, tax and total price. The details of these items are written to a file. Now add a value of 10% of the tax as the new tax since VAT must be included for the item. Now write the new price of the items to an output file.