File Handling and IO Handling in Java Programming

Posted on
File Dealing with and IO Dealing with in Java Programming



This Java turorial mainly concentrate on the File dealing with operation and IO portion if java programming . Until we’ve lined a number of the Primary of Core java programming .


We are going to cowl following subjects throughout this a part of java tutorial . 

  • File Dealing with in Java
  • IO Dealing with in Java

File Dealing with in Java


Information are the first source and vacation spot for information inside most packages. File are additionally used for orgainising the info in file constructions .


Java devotes a complete vary of strategies present in a category known as File within the java.io package deal to carry out these operation.


Constructor for Information 


Objects of the file class could be created utilizing three forms of File constructor.


File f1= new File (“c:javapattern.txt”)
File f1= new File (“c:java”, “pattern.txt”)
File f1= new File (“java”, “pattern.txt”)


Strategies of File Class


The tactic current in File class are :

  1. String getName();
  2. String getPath();
  3. String getParent();
  4. boolean exists()
  5. boolean isFile() 
  6. boolean isDirectory()

Different Strategies of File Class

  1. boolean canRead()
  2. boolean canWrite() 
  3. boolean delete() 
  4. lengthy lastModified()
  5. lengthy size()
  6. String [] checklist()

  1. boolean mkdir()
  2. boolean renameTo (File dest) 

Streams in Java File Dealing with 


A Stream is a path of communication between the source of information and the vacation spot.


Streams could be divided below three classes as follows :

  1. InputStream
  2. OutputStream
  3. Reader/Author

Instance 1


byte c;
c=(byte) System.in.learn();
System.out.println (“Entered Character is ” );
System.out.println ((char )c);


Instance 1 (modified)


byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.learn();


System.out.println (“Entered Character is ” );
for (int i=0; i<10; i++)
System.out.println ((char )c[i]);
Instance 1 (modified)
byte c[]= new byte[10];
for (int i=0; i<10; i++)
c[i]=(byte) System.in.learn();


System.out.println (“Entered String  is ” );
System.out.write(c);




File Streams in Java 


There are numerous forms of streams discovered within the java.io package deal

  1. FileInputStream
  2. FileOutputStream
  3. BufferedInputStream
  4. BufferedOutputStream
  5. BufferedReader
  6. BufferedWriter

Instance 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]);


Instance 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]);




Instance 2 (modified)


FileInputStream f= new FileInputStream (“c://abc.txt”);


for (int i=0; i
System.out.print( (char) f.learn());


Instance 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);


Instance 3 (Modified)


char c[]= {‘A”, ‘B’, ‘C’, ‘D’ };


BufferedOutputStream f= new BufferedOutputStream( new FileOutputStream (“c:abc.txt”);
f.write(c);


Instance 4


To take String from Consumer throughout this system . You need to use bufferreader in java packages to take enter from the consumer throughout this system execution .


So, right here is the whole code to take enter from consumer .


BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
————————-
String str2=b.readLine();
Instance 4
To take Integer from Consumer 
BufferedReader b= new BufferedReader( new InputStreamReader (System.in);
String str1= b.readLine();
Int a= Integer.parseInt (str1);
————————-
String str2=b.readLine();




Questions for Fast Revision 


Clarify three alternative ways for creating File Object? 


Clarify alternative ways for taking Enter from the Consumer?

Supply projectgeek.com