0% found this document useful (0 votes)
17 views7 pages

Chapter 5 Example

The document provides examples of performing common file I/O operations in Java, including writing to a file, reading from a file, appending to a file, copying a file, deleting a file, and getting the size of a file. The examples demonstrate how to use classes like BufferedWriter, FileWriter, BufferedReader, and File to perform these operations and handle exceptions. Output from running the examples is also displayed.

Uploaded by

Oz G
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)
17 views7 pages

Chapter 5 Example

The document provides examples of performing common file I/O operations in Java, including writing to a file, reading from a file, appending to a file, copying a file, deleting a file, and getting the size of a file. The examples demonstrate how to use classes like BufferedWriter, FileWriter, BufferedReader, and File to perform these operations and handle exceptions. Output from running the examples is also displayed.

Uploaded by

Oz G
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/ 7

Eg1, Write file

package bwritestext;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

public class Bwritestext {

public static void main(String[] args) throws IOException {

// try {

BufferedWriter out = new BufferedWriter(new FileWriter("c:/Test2.txt"));

out.write("**************************************");

out.newLine(); //new line

out.write("* Name IdNo Age *");

out.newLine();

out.write("------------------------------------");

out.newLine();

out.write("* Kassahun * 121212 * 19 *");

out.newLine();

out.write("* Tigst * 121213 * 24 *");

out.newLine();

out.write("* Titna * 121214 * 19 *");

out.newLine();

out.write("*************************************");

out.close();

System.out.println("File created successfully");

Set by kassahun Page 1


//}

//catch (IOException e) {

//System.out.println(e.getMessage());

//}

Output
File created successfully
BUILD SUCCESSFUL (total time: 0 seconds)

Eg2, Reade file


package filereading;

import java.io.*;

public class Filereading {

public static void main(String[] args) throws FileNotFoundException, IOException {

BufferedReader in=new BufferedReader(new FileReader("c:/test3.txt"));

String str;

while((str=in.readLine())!=null){

System.out.println(str);

Eg3, Appende file


Set by kassahun Page 2
package appneding;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class Appneding {

public static void main(String[] args) {

try {

String newcontent="java is object orated programing language";

String add="java is case sensetive";

File file=new File("c:/test1.txt");

if(!file.exists()){

file.createNewFile();}

FileWriter fw=new FileWriter(file,true);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(newcontent);

bw.write(add);

bw.close();

System.out.println("data successfully Append");

BufferedReader br= new BufferedReader(new FileReader("E:/Test3.txt"));

String str;

while ((str = br.readLine()) != null) {

Set by kassahun Page 3


System.out.println(str);

br.close();

catch(IOException e) {

System.out.println("exception occoured"+ e);

output
data successfully Append
exception occouredjava.io.FileNotFoundException: E:\Test3.txt (The device is
not ready)
BUILD SUCCESSFUL (total time: 0 seconds

Eg4, Copy file


package copy;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class Copy {

public static void main(String[] args) throws FileNotFoundException, IOException {

BufferedReader in=new BufferedReader(new FileReader("c:/test3.txt"));

String str;

Set by kassahun Page 4


BufferedWriter bw = new BufferedWriter(new FileWriter("c:/test1.txt"));

while((str=in.readLine())!=null){

bw.write(str);

in.close();

bw.close();

System.out.println("copied successfully");

Output
copied successfully
BUILD SUCCESSFUL (total time: 0 seconds)

Eg5, Delete file


package delete;

import java.io.File;

public class Delete {

public static void main(String[] args) {

try{

//File f=new File("c:/kasa.txt");

File f=new File("f:/cs.docx");

if(f.exists()){

f.delete();

Set by kassahun Page 5


System.out.println(f.getName()+"is deleted");

else{

System.out.println("file not exist");

catch(Exception e){

System.out.println(e.getMessage());

// TODO code application logic here

Eg6, File Size


package filesize;

import java.io.File;

public class Filesize {

public static long getFileSize(String Filename){

File file=new File(Filename);

if(!file.exists()){

System.out.println("not fund");

return file.length();

public static void main(String[] args) {

Set by kassahun Page 6


long size=getFileSize("c:/test1.txt");

System.out.println(size);

// TODO code application logic here

Set by kassahun Page 7

You might also like