This Java turorial basically focus on the File handling operation and IO portion if java programming . Till we have covered some of the Basic of Core java programming .
We will cover following topics during this part of java tutorial .
- File Handling in Java
- IO Handling in Java
File Handling in Java
Files are the primary source and destination for data within most programs. File are also used for orgainising the data in file structures .
Java devotes a whole range of methods found in a class called File in the java.io package to perform these operation.
Constructor for Files
Objects of the file class can be created using three types of File constructor.
File f1= new File (“c:\\java\\sample.txt”)
File f1= new File (“c:\\java”, “sample.txt”)
File f1= new File (“java”, “sample.txt”)
Methods of File Class
The method present in File class are :
- String getName();
- String getPath();
- String getParent();
- boolean exists()
- boolean isFile()
- boolean isDirectory()
Other Methods of File Class
- boolean canRead()
- boolean canWrite()
- boolean delete()
- long lastModified()
- long length()
- String [] list()
- boolean mkdir()
- boolean renameTo (File dest)
Streams in Java File Handling
A Stream is a path of communication between the source of information and the destination.
Streams can be divided under three categories as follows :
- InputStream
- OutputStream
- Reader/Writer
Example 1
byte c;
c=(byte) System.in.read();
System.out.println (“Entered Character is ” );
System.out.println ((char )c);
Example 1 (modified)
byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.read();
System.out.println (“Entered Character is ” );
for (int i=0; i<10; i++)
System.out.println ((char )c[i]);
Example 1 (modified)
byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.read();
System.out.println (“Entered String is ” );
System.out.write(c);
File Streams in Java
There are various types of streams found in the java.io package
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
- BufferedReader
- BufferedWriter
Example 2
byte b[]= {65,66,67,68,65,66,67,68,65,66};
FileOutputStream f= new FileOutputStream (“c:\\abc.txt”);
for (int i=0;i<10; i++)
f.write(b[i]);
Example 2 (modified)
char c[]= {‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G‘};
FileOutputStream f= new FileOutputStream (“c:\\abc.txt”);
for (int i=0;i<5; i++)
f.write(c[i]);
Example 2 (modified)
FileInputStream f= new FileInputStream (“c://abc.txt”);
for (int i=0; i
System.out.print( (char) f.read());
Example 3
byte b[]= {65,66,67,68,65,66,67,68,65,66};
BufferedOutputStream f= new BufferedOutputStream( new FileOutputStream (“c:\\abc.txt”);
f.write(b);
Example 3 (Modified)
char c[]= {‘A”, ‘B’, ‘C’, ‘D’ };
BufferedOutputStream f= new BufferedOutputStream( new FileOutputStream (“c:\\abc.txt”);
f.write(c);
Example 4
To take String from User during the program . You can use bufferreader in java programs to take input from the user during the program execution .
So, here is the complete code to take input from user .
BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
————————-
String str2=b.readLine();
Example 4
To take Integer from User
BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
Int a= Integer.parseInt (str1);
————————-
String str2=b.readLine();
Questions for Quick Revision
Explain three different ways for creating File Object?
Explain different ways for taking Input from the User?
Leave a Reply