Programs:-
WriterExample.java
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriterExample {
public
static void main(String[] args) {
try
{
Writer
w = new FileWriter(
"D:\\practice
java\\Filehandling IO\\files\\testout.txt");
String
content = "I love my country ";
w.write(content);
w.close();
System.out.println("Done");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//Path:- file create
//D:\\practice java\\Filehandling IO\\files\\testout.txt
o/p:-
Done
ReaderExample.java
import java.io.FileReader;
import java.io.Reader;
public class ReaderExample {
public
static void main(String[] args) {
try
{
Reader
reader = new FileReader("D:\\practice java\\Filehandling
IO\\files\\testout.txt");
int
data = reader.read();
while
(data != -1) {
System.out.print((char)
data);
data
= reader.read();
}
reader.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
/*
* if not found testoutput.txt (The system
cannot find the file
* specified)
*/
}
}
}
o/p:-
I love my country
PrintWriterExample.java
import java.io.File;
import java.io.PrintWriter;
public class PrintWriterExample {
public
static void main(String[] args) throws Exception {
//
Data to write on Console using PrintWriter
PrintWriter
writer = new PrintWriter(System.out);
writer.write("Javatpoint
provides tutorials of all technology.");
writer.flush();
writer.close();
//
Data to write in File using PrintWriter
PrintWriter
writer1 = null;
writer1
= new PrintWriter(new File("D:\\practice java\\Filehandling
IO\\files\\testout1.txt"));
writer1.write("Like
Java, Spring, Hibernate, Android, PHP etc.");
writer1.flush();
writer1.close();
}
}
o/p:-
Javatpoint provides tutorials of all technology.
OutputStreamWriterExample.java
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class OutputStreamWriterExample {
public
static void main(String[] args) {
try
{
OutputStream
outputStream = new FileOutputStream(
"D:\\practice
java\\Filehandling IO\\files\\testout2.txt");
Writer
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write("Hello
World");
outputStreamWriter.close();
System.out.println("Done");
}
catch (Exception e) {
e.getMessage();
}
}
}
o/p:-
Done
FileReaderExample..java
import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[]) throws Exception {
FileReader
fr = new FileReader("D:\\practice
java\\Filehandling IO\\files\\testout.txt");
int i;
while ((i = fr.read()) !=
-1)
System.out.print((char) i);
fr.close();
}
}
o/p:-
I love my country
FileOutputStreamExample.java
import
java.io.FileOutputStream;
public class
FileOutputStreamExample {
public static void main(String args[])
{
try {
FileOutputStream
fout = new FileOutputStream(
"D:\\practice java\\Filehandling
IO\\files\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}
catch (Exception e) {
System.out.println(e);
}
}
}
o/p:-
success...
DataStreamExample.java
import
java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[])
{
try {
FileInputStream
fin = new FileInputStream(
"D:\\practice
java\\Filehandling IO\\files\\testout.txt");
int i = fin.read();
System.out.print((char) i);
fin.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
o/p:-
A
BufferedReaderExample.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static
void main(String args[]) throws Exception {
InputStreamReader
r = new InputStreamReader(System.in);
BufferedReader
br = new BufferedReader(r);
String
name = "";
while
(!name.equals("stop")) {
System.out.println("Enter
data: ");
name
= br.readLine();
System.out.println("data
is: " + name);
}
br.close();
r.close();
}
}
o/p:-
Enter data:
10
data is: 10
Enter data:
20
data is: 20
Enter data:
stop
data is: stop
No comments:
Post a Comment