[go: up one dir, main page]

0% found this document useful (0 votes)
12 views4 pages

Oops Exp No 8

Oops

Uploaded by

anu303184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Oops Exp No 8

Oops

Uploaded by

anu303184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX NO :8 PERFORM FILE HANDLING

Aim:

Write a java program to create a new file.

Algorithm:

Step 1: Start the program


Step 2: createNewFile() method is used for creating a file.
Step 3: createNewFile() method returns true when it successfully creates a new file
and returns false when the file already exists.
STEP 4: Import scanner and file classes.
Step 5: Use scanner method to get file name from user.
Step 6: Create a file object.
Step 7: Call the respective methods to display file properties like getName(), getPath() etc.
Step 8: Call the respective methods
String.getBytes(),FileOutputStream.flush() ,FileOutputStream.close()
Step 9: String.getBytes() - returns the bytes array.
Step 10: FileOutputStream.flush() - Is used to clear the output steam buffer.
Step 11: FileOutputStream.close() - Is used to close output stream (Close the file).
Step 12: Stop the program

PROGRAM:

FILE CREATION:

import java.io.File;
import java.io.IOException;
class CreateFile
{
public static void main(String args[])
{
try
{

File f0 = new File("D:File1.txt");


if (f0.createNewFile())
{
System.out.println("File " + f0.getName() + " is created successfully.");
}
else {
System.out.println("File is already exist in the directory.");
}
} catch (IOException exception)
{
System.out.println("An unexpected error is occurred.");
exception.printStackTrace();
}
}
}

DISPLAY FILE:

import java.util.Scanner;
import java.io.File;
class fileDemo
{
public static void main(String[] args)
{
System.out.println("Enter the file name:");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
File f1=new File(s);
System.out.println("-------------------------");
System.out.println("File Name: " +f1.getName());
System.out.println("Path: " +f1.getPath());
System.out.println("Abs Path: " +f1.getAbsolutePath());
System.out.println("This file: " +(f1.exists()?"Exists":"Does not exists"));
System.out.println("File: " +f1.isFile());
System.out.println("Directory: " +f1.isDirectory());
System.out.println("Readable: " +f1.canRead());
System.out.println("Writable: " +f1.canWrite());
System.out.println("Absolute: " +f1.isAbsolute());
System.out.println("File Size: " +f1.length()+ "bytes");
System.out.println("Is Hidden: " +f1.isHidden());
}
}

WRITE CONTENT IN TO A FILE:

import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;
public class WriteFile { public static void main(String args[])
{
final String fileName = "file.txt";
try
{
File objFile = new File(fileName);
if (objFile.exists() == false)
{ if (objFile.createNewFile())
{ System.out.println("File created successfully.");
}
else
{
System.out.println("File creation failed!!!");
System.exit(0);
}
}

String text;
Scanner SC = new Scanner(System.in);
System.out.println("Enter text to write into file: ");
text = SC.nextLine();

FileOutputStream fileOut = new FileOutputStream(objFile);


fileOut.write(text.getBytes());
fileOut.flush();
fileOut.close();
System.out.println("File saved.");
}
catch (Exception Ex)
{
System.out.println("Exception : " + Ex.toString());
}
}

Output:

C:\ >javac fileDemo.java


C:\ >java fileDemo
Enter the file name:
fileDemo.java
-------------------------
File Name:
fileDemo.java Path:
fileDemo.java
Abs Path: D:\ Ex 08\fileDemo.java
This file: Exists
File: true
Directory: false
Readable: true
Writable: true
Absolute: false
File Size: 895bytes
Is Hidden: false

RESULT:
Thus the java application to perform File Handling was implemented and the program was
executed successfully.

You might also like