Nothing Special   »   [go: up one dir, main page]

File Handling in Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

File Handling in Java

Stream

 Stream Represents an input source or output destination


• Disk files, devices, other programs, and memory arrays
 Stream supports
• Simple bytes primitive data types, localized characters and
objects
 Stream represent a simple model to programs
 Stream represents sequence of data
Stream

Program ---------uses input stream---------->to read data from


source

Program ---------uses output stream---------->to read data to


destination
OutputStream class InputStream class

• Base class of all output • Base class of all input stream


stream of bytes of bytes
• Sub classes are • Sub classes are
– FileOutputStream – FileInputStream
– ByteArrayOutputStream – ByteArrayInputStream
– FilterOutputStream – FilterInputStream
• DataOutputStream • DataInputStream
• BufferedOutputStream • BufferedInputStream
• PrintStream • PushBackInputStream
– PipedOutputStream – PipedInputStream
– ObjectOutputStream – ObjectInputStream
Byte Stream

 Programs use Byte Streams to perform I/O in the form of


bytes
 Byte Stream classes are inherited from InputStream and
OutputStream classes
 For File I/O
• FileInputStream and FileOutputStream classes
Copying from the file
public static void main(String[] args) throws IOException
{
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream("myfile.txt");
fout = new FileOutputStream("myfile1.txt");
int c;
while ((c = fin.read()) != -1) {
fout.write(c);
}
} finally {
if (fin != null) {
fin.close();
}
if (fout != null) {
fout.close();
}
} // end of finally
} //end of main
Points to note

 Import statements
• import java.io.FileInputStream;
• import java.io.FileOutputStream;
• import java.io.IOException;
 throws IOException
 Closing the streams
• Whether or not error occurs
 Return type of read method – int
• Value bytes is returned as int in the range 0 to 255
• -1 on reaching EOF
When used?

 To perform low level I/O

 For other purposes use Character Streams


Character Streams

 How are character values stored on Java platform?


• Unicode

 Classes responsible for using Character Streams translate


to and from local character set
• The program adapts to local character set
 Inherit from Reader and Writer classes
 For File I/O
• FileReader and FileWriter classes
Copying from the file
public static void main(String[] args) throws IOException
{ FileReader fin = null;
FileWriter fout = null;
try {
fin = new FileReader("myfile.txt");
fout = new FileWriter("myfile1.txt");
int c;
while ((c = fin.read()) != -1) {
fout.write(c);
}
} finally {
if (fin != null) {
fin.close();
}
if (fout != null) {
fout.close();
}
}
}
Points to note

 Import statements
• import java.io.FileReader;
• import java.io.FileWriter;
• import java.io.IOException;
 throws IOException
 Closing the streams
• Whether or not error occurs
 Return type of read method – int
• Stores value in last 16 bits
• -1 on reaching EOF

 Wrappers for Byte Streams


Line oriented I/O

BufferedReader inputStream = null;


PrintWriter outputStream = null;

try {
inputStream = new BufferedReader(new FileReader("myfile.txt"));
outputStream = new PrintWriter(new FileWriter("myfile1.txt"));

String str;
while ((str = inputStream.readLine()) != null) {
outputStream.println(str);
}
} finally {

//closing the streams


}
File Operations
The File class

 Import java.io package


 Methods
• canRead
• canWrite
• createNewFile
• delete
• exists
• getName
• length
• list
• mkdir
• getAbsolutePath
Files class

 How to use the methods of File class?

File obj1 = new File(“myfile.txt”);


if(obj1.createNewFile())
{
SOP(“New file created” + obj1.getName())
SOP(obj1.getAbsolutePath());
}
else
SOP(“File already exists”);

*****Handle IOException
File obj1 = new File(“myfile.txt”);

if (obj1.exists())
{
      System.out.println("File name: " + obj1.getName()); 
      System.out.println("Absolute path: " + obj1.getAbsolutePath()); 
      System.out.println("Writeable: " + obj1.canWrite()); 
      System.out.println("Readable " + obj1.canRead()); 
      System.out.println("File size in bytes " + obj1.length());
   }
else 
{
      System.out.println("The file does not exist.");
}
public static void main(String[] args)
{
try {
File myObj = new File("myfile.txt");
Scanner r1 = new Scanner(myObj);

while (r1.hasNextLine())
{
String data = r1.nextLine();
System.out.println(data);
}
r1.close();

} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

You might also like