Beruflich Dokumente
Kultur Dokumente
1->when you want to develop any java application then you will get the requireme
nt to
interact with variouse input-output devices.
2->I/O devices are the part of underlying hardware and operating system.
3->so for I/O device handling java vendor provide java.io package. by which you
can interact
with various input-output devices from java program.
4->All the method and some constructor of java.io package are throwing checked e
xception
called java.IOException.
5->because of checked exception you must have to report about the exception by w
riting
try-catch block or by using throws keyword.
Input and Output->I/O is used to process the input and produce the output based
on the input.
----------------- Java uses the concept of stream to make I/O operations fast.
java.io package contains all the classes required for input an
d output operations.
Stream->A stream is a sequence of data.In Java a stream is composed of bytes.
--------It's called a stream because it's like a stream of water that continues
to flow.
1) System.out: standard output stream
2) System.in: standard input stream
*InputStream->it is used to read the data from keyboard,file,or
socket to primary memeory
*OutputStream->it is used to write the data to monitor,file or
socket from primary memeory.
Object
|
Reader
-----------------------|
|
BufferReader
InputStreamReader
|
FileReader
Object
|
Writer
-----------------------------------------|
|
|
BufferedWriter PrintWriter
OutputStreamWriter
|
FileWriter
1-note->take input from keyboard
ex-2-import java.io.*;
public class TestIR
{
public static void main(String args[])
{
String str;
try
{
BufferedReader br = new BufferedReader(new FileReader("C://ss//d
d//name.txt"));
while ((str = br.readLine()) != null)
{
System.out.println(str);
}
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
ex-3>import java.io.*;
public class TestIR
{
public static void main(String args[])
{
String s;
try
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
while ((s = br.readLine()) != null)
{
System.out.println(s);
}
br.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
ex-4->
import java.io.*;
import java.util.*;
public class TestBR2
{
public static void main(String args[])
{
String str;
double radius;
StringTokenizer stk;
try
{
BufferedReader br =new BufferedReader(new FileReader("C://sphere.txt"));
while ((str = br.readLine()) != null)
{
stk = new StringTokenizer(str);
while (stk.hasMoreTokens())
{
str = stk.nextToken();
}
radius = Double.parseDouble(str);
System.out.println("area is " + Math.PI*radius*radius);
}
br.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
return;
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
ex-5->
import java.io.*;
public class Demo
{
public static void main(String args[]) throws IOException
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("C://ss//dd//n.txt",true); // che
ck without true
BufferedWriter bwr = new BufferedWriter(fw);
{
char ch='Y';
do
{
System.out.println("enter first Line");
String info=br.readLine();
bwr.write(info);
System.out.println("enter second Line");
String info1=br.readLine();
bwr.write(info1);
bwr.newLine();
System.out.println("Do you want to add more:[Y/N]");
ch=(char)br.readLine().charAt(0);
}
while(ch=='Y');
bwr.close();
}
}
}
ex-6>
import java.io.*;
public class Demo
{
//->First.java
//-> null
//-> First.java
canRead()
canWrite()
isHidden()
exists()
isFile()
isDirectory()
isAbsolute()
{
public static void main(String args[])
{
File fobj = new File("G://tmp2");
System.out.println(fobj.mkdir());
if (fobj.isDirectory())
{
System.out.println(fobj.getName() + " is a directory");
}
}
}
ex-4->
import java.io.*;
public class Demo
{
public static void main(String args[])
{
File fobj = new File("G://tmp2");
System.out.println("Path of " + fobj.getName() + " is "+ fobj.getPath
());
System.out.println("Parent of " + fobj.getName() + " is "+ fobj.getPa
rent());
System.out.println("Absolute Path is" + fobj.getAbsolutePath());
if (!fobj.delete())
{
System.out.println(fobj.getName() + " cannot be deleted");
}
}
}
ex-5->
import java.io.*;
public class Demo
{
public static void main(String args[])
{
File fobj = new File("G://song//bollywood");
String[] fileList = fobj.list();
System.out.print("Files in " + fobj.getName() + " are ");
for (int i = 0; i < fileList.length; ++i)
{
System.out.println(fileList[i] + " ");
}
System.out.println("");
}
}