Table of Contents
hide
Example : Write a java program for writing/display output on console simply (or without using PrintWriter class).
class Example
{
public static void main(String[] args)throws Exception
{
char ch1,ch2;
ch1 = 'J';
System.out.write(ch1);
System.out.write('\n');
ch2 = '5';
System.out.write(ch2);
System.out.write('\n');
}
}
Example : Write a program in Java to Create a new file using file handling methods.
import java.io.File;
import java.io.IOException;
class FileHandlingExample
{
public static void main(String[] args)
{
try
{
File file = new File("example.txt");
boolean created = file.createNewFile();
if (created)
{
System.out.println("File created successfully.");
}
else
{
System.out.println("File already exists.");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Example : Write a program in Java to Write/Store data into a newly created Java file using file handling methods.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
class FileHandlingExample
{
public static void main(String[] args)
{
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"));
writer.write("Hello, World!");
writer.close();
System.out.println("Data is written to the file.");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Example : Write a program in Java to Read/Fetch data from an existing or created Java file using file handling methods.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class FileHandlingExample
{
public static void main(String[] args)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
String input;
while ((input= reader.readLine()) != null)
{
System.out.println(input);
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Example : Write a program in Java to Delete/Remove an existing or created Java file using file handling methods.
import java.io.File;
class FileHandlingExample
{
public static void main(String[] args)
{
File file = new File("example.txt");
boolean deleted = file.delete();
if (deleted)
{
System.out.println("File deleted successfully.");
}
else
{
System.out.println("Failed to delete the file.");
}
}
}
Example : Write a java program for writing/display output on console using PrintWriter class.
import java.io.PrintWriter;
class Example
{
public static void main(String[] args)throws Exception
{
PrintWriter wt = new PrintWriter(System.out, true);
wt.println("File Handling Console Writing Program");
wt.println(200);
wt.println(-32.01);
}
}
-------- OR --------
import java.io.PrintWriter;
class Example
{
public static void main(String[] args)
{
System.out.println("First File");
PrintWriter wt = new PrintWriter(System.out);
wt.write("File Handling Program1"); // or wt.println("File Handling Program1");
wt.flush();
wt.close();
}
}
Output :
First File
File Handling Program1
Example : Write a java program for writing text in the file at specific location using PrintWriter class.
import java.io.File;
import java.io.PrintWriter;
class Example
{
public static void main(String[] args)throws Exception
{
PrintWriter wt =null;
wt = new PrintWriter(new File("D:\\New folder\\test.txt"));
wt.write("File Handling Program2");
wt.flush();
wt.close();
}
}
Output :
Open the test.txt file in D drive of the computer system and open it to see the output File Handling Program2.
Example : Write a java program for writing text in the file at specific location copy them into another file and then display the copied text using File concept.
import java.io.PrintWriter;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class Example
{
public static void main(String[] args)
{
try
{
//New file creation code
PrintWriter wt =null;
wt = new PrintWriter(new File("D:\\New folder\\test1.txt"));
wt.write("File Handling Program2");
wt.flush();
wt.close();
//copy text from test1.txt to another file test2.txt
FileReader fr = new FileReader("D:\\New folder\\test1.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("D:\\New folder\\test2.txt", true);
String str1,str2;
while ((str1 = br.readLine()) != null) // Read a line of text
{
fw.write(str1);
fw.flush();
}
br.close();
fw.close();
System.out.println("File copied successfully");
//display the copied text from file test2.txt
BufferedReader br1 = new BufferedReader(new FileReader("D:\\New
folder\\test2.txt"));
System.out.println("\nThe output are =");
while ((str2 = br1.readLine()) != null)
{
System.out.println(str2);
}
br1.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Example : Write a java program for reading text from the file stored at specific location and display them line by line and word by word using File handling concept.
import java.io.*;
import java.util.*;
class Example
{
public static void main(String[] args) throws Exception
{
// Reading a text file contents line by line using Scanner class
System.out.println("Reading a text file contents line by line: ");
Scanner sc1 = new Scanner(new File("D:\\New folder\\test.txt"));
while (sc1.hasNext())
{
String str1 = sc1.nextLine();
System.out.println(str1);
}
sc1.close();
// Reading a text file contents word by word using Scanner class
System.out.println("Reading a text file word by word: ");
Scanner sc2 = new Scanner(new File("D:\\New folder\\test.txt"));
while (sc2.hasNext())
{
String str2 = sc2.next();
System.out.println(str2);
}
sc2.close();
}
}
Output :
Reading a text file contents line by line:
File Handling Program3
Reading a text file word by word:
File
Handling
Program3
Example : Write a java program to create a new file, write/store data in the created file, modify the stored contents, read/display the contents from the file using File handling concept.
import java.io.*;
class Example
{
public static void main(String[] args)
{
try
{
// Creating new Text file
File file = new File("D:\\New folder\\test1.txt");
if (!file.exists())
{
file.createNewFile();
System.out.println("File created Successfully.");
}
FileOutputStream fos=new FileOutputStream("D:\\New folder\\test1.txt");
// Writing contents to Text file
String str="Welcome in your site Codershelpline";
byte bt[]=str.getBytes();
fos.write(bt);
fos.close();
//System.out.println("Writing into text file Complete!");
FileInputStream fis = new FileInputStream("D:\\New folder\\test1.txt");
//Reading from Text file
int i;
while((i=fis.read())!=-1)
{
System.out.print((char)i);
}
fis.close();
fos=new FileOutputStream("D:\\New folder\\test1.txt");
// Modifying Text file
str="\nThis site contains various topics in simpler form";
bt=str.getBytes();
fos.write(bt);
fos.close();
//System.out.println("\n Now Modification Completed!");
fis = new FileInputStream("D:\\New folder\\test1.txt");
//Reading/displaying data from Text file
while((i=fis.read())!=-1)
{
System.out.print((char)i);
}
fis.close();
}
catch (IOException e)
{
System.out.println("I/O Exception occurred.");
}
}
}
0 Comments