Your SlideShare is downloading. ×
26  io -ii  file handling
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

26 io -ii file handling

301
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
301
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. File Input/ Output • File IO Basics Byte Streams FileInputStream File OutputStream Character Streams. FileReader FileWriter
  • 2. FileInputStream • The FileInputStream class creates an InputStream that you can use to read bytes from a file. • Its two most common constructors are shown here: FileInputStream(String filepath) FileInputStream(File fileObj) • Either can throw a FileNotFoundException. • Here, filepath is the full path name of a file, and fileObj is a File object that describes the file. • The following example creates two FileInputStreams that use the same disk file and each of the two constructors: FileInputStream f0 = new FileInputStream("/autoexec.bat") File f = new File("/autoexec.bat"); FileInputStream f1 = new FileInputStream(f); • When a FileInputStream is created, it is also opened for reading.
  • 3. Read one byte from the file • import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public void readOneByte() throws FileNotFoundException { FileInputStream file = null; byte x = -1; try { file = new FileInputStream("fileName"); x = (byte) file.read(); } catch (FileNotFoundException f) { throw f; } catch (IOException i) { i.printStackTrace(); } finally { try { if (file != null) { file.close(); } } catch (IOException e) { } } } }
  • 4. Read bytes from the file • import java.io.FileInputStream; public class Main { public static void main(String[] argv) throws Exception { FileInputStream fin = new FileInputStream("text.txt"); int len; byte data[] = new byte[16]; // Read bytes until EOF is encountered. do { len = fin.read(data); for (int j = 0; j < len; j++) System.out.printf(data[j]); } while (len != -1); } }
  • 5. Copy one file into another file import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Main { public static void main(String[] args) throws Exception { FileInputStream fin = null; FileOutputStream fout = null; File file = new File("C:/myfile1.txt"); fin = new FileInputStream(file); fout = new FileOutputStream("C:/myfile2.txt"); byte[] buffer = new byte[1024];
  • 6. int bytesRead; while ((bytesRead = fin.read(buffer)) > 0) { fout.write(buffer, 0, bytesRead); } fin.close(); fout.close(); } }
  • 7. FileOutput Stream class • FileOutputStream creates an OutputStream that you can use to write bytes to a file. Its most commonly used constructors are shown here: FileOutputStream(String filePath) FileOutputStream(File fileObj) FileOutputStream(String filePath, boolean append) FileOutputStream(File fileObj, boolean append) • They can throw a FileNotFoundException or a SecurityException. • filePath is the full path name of a file, and fileObj is a File object that describes the file. • If append is true, the file is opened in append mode. • The fourth constructor was added by Java 2, version 1.4.
  • 8. • FileOutputStream will create the file before opening it for output when you create the object. • In the case where you attempt to open a read- only file, an IOException will be thrown.
  • 9. Create FileOutputStream object from String file path • import java.io.FileOutputStream; public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("C:/demo.txt"); } }
  • 10. Create FileOutputStream object from File object • import java.io.File; import java.io.FileOutputStream; public class Main { public static void main(String[] args) throws Exception { File file = new File("C:/demo.txt"); FileOutputStream fos = new FileOutputStream(file); } }
  • 11. Copy byte between FileInputStream and FileOutputStream • import java.io.FileInputStream; import java.io.FileOutputStream; class Copy { public static void main(String[] args) throws Exception { FileInputStream fis = null; FileOutputStream fos = null; fis = new FileInputStream(args[0]); fos = new FileOutputStream(args[1]); int byte_; while ((byte_ = fis.read()) != -1) fos.write(byte_); } }
  • 12. Copy Bytes between FileInputStream and FileOutputStream import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c;
  • 13. while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
  • 14. Write byte array to a file using FileOutputStream import java.io.FileOutputStream; public class Main { public static void main(String[] args) throws Exception { String strFilePath = "C://demo.txt"; FileOutputStream fos = new FileOutputStream(strFilePath); String strContent = “Sir Padampat Singhania University"; fos.write(strContent.getBytes()); fos.close(); } }
  • 15. Append output to file using FileOutputStream import java.io.FileOutputStream; public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("C:/demo.txt", true); fos.write("Appended".getBytes()); fos.close(); } }
  • 16. File Character Stream
  • 17. FileReader • The FileReader class creates a Reader that you can use to read the contents of a file. • Its two most commonly used constructors are shown here: FileReader(String filePath) FileReader(File fileObj)
  • 18. read lines from a file and print these to the standard output stream. import java.io.*; class FileReaderDemo { public static void main(String args[]) throws Exception { FileReader fr = new FileReader("FileReaderDemo.java"); BufferedReader br = new BufferedReader(fr); String s; while((s = br.readLine()) != null) { System.out.println(s); } fr.close(); } }