• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

File Handling and IO Handling in Java Programming

April 18, 2012 by ProjectsGeek Leave a Comment

File Handling and IO Handling in Java Programming


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 :

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

Other Methods of File Class

  1. boolean canRead()
  2. boolean canWrite() 
  3. boolean delete() 
  4. long lastModified()
  5. long length()
  6. String [] list()
  1. boolean mkdir()
  2. 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 :

  1. InputStream
  2. OutputStream
  3. 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

  1. FileInputStream
  2. FileOutputStream
  3. BufferedInputStream
  4. BufferedOutputStream
  5. BufferedReader
  6. 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?

Other Projects to Try:

  1. Threads in Java Tutorial for Beginners
  2. Java Programming from program analysis to program Design By -DS Malik
  3. To Perform File Handling in Java
  4. File Handling program using Java
  5. Implement Conflation Algorithm using File Handling in Java

Filed Under: Java Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy