FILE HANDLING IN JAVA PROGRAMMING, Exercises of Java Programming

JUST GO THROUGH LINE BY LINE AND IN THE END YOU WILL LEARN FILE HANDLING VERY EASILY

Typology: Exercises

2017/2018

Uploaded on 04/05/2018

shakeeb-arif
shakeeb-arif 🇮🇳

1 document

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File Handling:
what is file??
File is a collection of data which is store on secondary devices(harddisk,pendrive, flopy, cd
etc..).
*There are two part in file:
1-filename 2-extension
extension define which software is suportable to open your file.
rishi.txt
filename: rishi
extenssion: .txt
This file may be open with notepad.
File Handling Operations:-
create file
write file
read file
append file
delete file
1- creating file
import java.io.*;
class FileCreate
{
public static void main(String[] arg)
{
try {
String x="D:/abc/xyz/jay.txt"; // String x="jay.txt"; then file will create in
class folder
File fl = new File(x);
fl.createNewFile();
SOP("File create successfully");
}
catch(Exception ex)
{
System.out.println(" Path is incorrect "+ex);
}
}
}
pf3
pf4
pf5

Partial preview of the text

Download FILE HANDLING IN JAVA PROGRAMMING and more Exercises Java Programming in PDF only on Docsity!

File Handling:

what is file?? File is a collection of data which is store on secondary devices(harddisk,pendrive, flopy, cd etc..).

*There are two part in file: 1-filename 2-extension

extension define which software is suportable to open your file.

rishi.txt

filename: rishi extenssion: .txt

This file may be open with notepad.

File Handling Operations:-

create file write file read file append file delete file

1- creating file import java.io.*; class FileCreate { public static void main(String[] arg) { try { String x="D:/abc/xyz/jay.txt"; // String x="jay.txt"; then file will create in class folder File fl = new File(x);

fl.createNewFile(); SOP("File create successfully"); } catch(Exception ex) { System.out.println(" Path is incorrect "+ex); }

} }

2- write into file

import java.io.*; class WiteFile { public static void main(String[] arg) { try { String path = "d:/javafile/abc.txt"; File fl = new File(path);

FileWriter fw=new FileWriter(fl); BufferedWriter bw=new BufferedWriter(fw);

bw.write("jayprakash"); bw.newLine(); bw.write("monika"); bw.write("java programing");

for(int i=1;i<=100;i++) { bw.write(" "+i); }

bw.close(); System.out.println("Writing is completed");

}catch(Exception ex){ system.out.println("Error: "+ex);

Exercise: write java program to write 1 to 100000 number into a file.

import java.io.*; class LakhNumber { public static void main(String[] arg) { try { String path = "d:/javafile/abc.txt"; File fl = new File(path); FileWriter fw=new FileWriter(fl); BufferedWriter bw=new BufferedWriter(fw); for(int i=1;i<=100000;i++) {

system.out.println("Error: "+ex); } }

4- append in to file: There is same code write and append but difference is:

FileWriter fw=new FileWriter(fl.getAbsoluteFile() , true);

import java.io.*; try { String path = "d:\abc\siddarth.txt"; File fl = new File(path); if (!fl.exists()) { fl.createNewFile(); } FileWriter fw=new FileWriter(fl.getAbsoluteFile()); BufferedWriter bw=new BufferedWriter(fw);

String contents="Hello jayprakash"; bw.write(contents); bw.close();

System.out.println("Writing is completed"); }catch(Exception ex){ system.out.println("Error: "+ex); } }

5- remove file

import java.io.*; try { String path = "d:\abc\siddarth.txt"; File fl = new File(path);

fl.delete(); System.out.println("delete success");

}catch(Exception ex){ system.out.println("Error: "+ex); } }

Example:

1-There is a file in which small letter, capital latter, and digit such as

dsafrqemnfjkhhgGfytrYUHGkjghjfTYryUGjhBVhgFtyRyGjhG2323 323 kjguYTRyt

Now Write a java pragarm to seperate into three file capital, small and digit.

2- Write a program to enter customer name, mobile no and product(name,rate,quantity). print his bill into file.

3- There is file in which have so many line. Write a program to show how much time a word available.

6- change the content of file

7- there is a file in which even and odd numbers. write a program to seperate these number into even.txt and odd.txt file.