• 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

To Perform File Handling in Java

July 1, 2012 by ProjectsGeek Leave a Comment

                                      Assignment No 02

AIM: 


To perform file handling in Java. Take two filenames from user, check the file is present in c:\java folder, if not present, display the message “file not present” , else display the size and file name. Also display the file contents.

THEORY: 


Objects of the File class can be created using File Constructors.

Constructors:

File f1=new File(“c:/java/temp.txt “);
File f1 = new File( “c:/java”, “temp.txt “);
File f1 = new File( “java”, “temp.txt “);

Methods are : f.getName( );f.getPath( );f.getAbsolutPath( );f.getParent( );f.exists( ); f.isFile( ); f.isDirectory( );f.canRead( );f.canWrite( );f.lastModified( );f.length( );f.delete( ); f.renameTo(f1); f.list( ); f.mkdir( );

A stream is a path of communication between the source of information and the destination. Methods of InputStream/Reader are : read( );skip( );available( );close( );mark( );reset( ); Methods of OutputStream/ Writer are : write( );flush( );close( );

ByteArrayInputStream class uses a byte array as its input source.
ByteArrayInputStream b = new ByteArrayInputStream(buf [ ]);
ByteArrayOutputStream class implements a buffer, which can be used as an OutputStream
ByteArrayOutputStream o =new ByteArrayOutputStream( );

This creates a buffer of 32 bytes to store the data.
ByteArrayOutputStream o =new ByteArrayOutputStream( int );


RandomAccessFile class can be used for both input and output to a single file.

// rw for read & write mode while r for read only mode.

RandomAccessFile    rd = new RandomAccessFile (“      “,”rw”);

The seek ( ) method specifies the byte-offset from the beginning of the file.

Reader and write classes are used to read/write characters instead of bytes.

CONCLUSION:

File handling is successfully done in Java.



PROGRAM CODE



import java.io.*;
class prg {
public static void main( String arg [ ] ) throws IOException {
FileInputStream fi = new FileInputStream(“ “);
FileOutputStream fo = new FileOutputStream (“ “);
int size = fi.available( );
for( int i =0; i < size; i++ )
{
int m =fi.read( );
System.out.println( (char) m);
fo.write( m );
}
fo.close( );
fi.close( );
}

// using BuffredInputstream and BuffredOutputstream.
BuffredInputstream bi = new BuffredInputstream (fi);
BuffredOutputstream bo = new BuffredOutputStream( fo);
bo.flush( );

//using DataInputStream and DataOutputStream.
DataInputStream di = new DataInputStream( fi );
DataOutputStream do = new DataOutputStream( fo );

String line;
while( line =di.readLine( ) != null )
{
System.out.println(line);
do.writeBytes(line);
}

//using BufferedReader and BufferdWriter
BufferedReader br =new BufferedReader (new InputStreamReader (fi));
BufferedWriter bw =new BuffredOutputStream (new OutputStreamWriter (fo));

// using BuffredInputstream and BuffredOutputstream.
while (int a = System.in.read ()! = 13)
{
fo.write (a);
}

//using BufferedReader and DataOutputStream.
while (line=br.readLine! = null)
{
if (line. equals (“end”))
break;
else
{
String z;
do.writeBytes (z);
}
}

To Perform File Handling in Java 3

Download Code Here

Other Projects to Try:

  1. Socket programming in Java
  2. File Handling and IO Handling in Java Programming
  3. File Handling program using Java
  4. Implement Conflation Algorithm using File Handling in Java
  5. To Perform various String Operation in Java

Filed Under: Java Assignments

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