Your SlideShare is downloading. ×
0
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
Oop lecture9 11
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

Oop lecture9 11

237

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
237
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
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. Lecture 11File IO operations in Java Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. List all files/folders under a directoryimport java.io.File;public class DirDemo { static final String DIR_NAME = "/home/user"; public static void main(String[] args) { File file = new File(DIR_NAME); String[] files = file.list(); for (String f : files){ System.out.println(f); } }} java.io.File is the class of interest
  • 3. Outputoop_lecture1.pptoop_lecture7.pptoop_lecture9_10.pptoop_lecture9.pptoop_lecture3.pptcomoop_lecture9_11.pptoop_lecture4.pptoop_lecture6.pptoop_lecture5.pptoop_lecture8.ppt
  • 4. How many are folders and how many are files under a directorypublic class FileDirCheckDemo { static final String DIR_NAME = "/home/user"; public static void main(String[] args) { File file = new File(DIR_NAME); String[] files = file.list(); int countOfDir = 0; int countOfFile = 0; for (String f : files){ File fileReal = new File(DIR_NAME + File.separator + f); if (fileReal.isDirectory()) countOfDir++; else countOfFile++; } System.out.println("No of directories: " + countOfDir + "; no offiles: " + countOfFile); }} Output: No of directories: 2; no of files: 18
  • 5. Other useful methods on FileCreate a new File: File f = new File("D:tempnewfile.txt"); f.createNewFile();Delete a File: File f = new File("D:tempnewfile.txt"); f.delete();Rename a File: File f = new File("D:tempnewfile.txt"); File renF = new File("D:tempnewfile.txt"); f.renameTo(renF);
  • 6. Copy a file to anotherpublic class FileDemo { static final String FIRST_FILE_NAME= "/home/user/raihan_fb.png"; static final String SECOND_FILE_NAME = "/home/user/out.png"; public static void main(String[] args) { try{ File file1 = new File(FIRST_FILE_NAME); File file2 = new File(SECOND_FILE_NAME); byte[] b = new byte[1024]; FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file2); int len = 0; while ((len = fis.read(b)) != -1){ fos.write(b, 0, len); } fis.close(); fos.close(); }catch(Exception e){ } }}
  • 7. Reading a text file and output to consolepublic class ReadTextDemo { static final String FILE_NAME = "/home/user/test.txt"; public static void main(String[] args) { File file = new File(FILE_NAME); try{ BufferedReader br = new BufferedReader(new FileReader(file)); String s = null; while ( (s = br.readLine()) != null){ System.out.println(s); } br.close(); }catch(Exception e){ e.printStackTrace(); } }}
  • 8. Outputtext.txt: Console output:Apples are red Apples are red Coconuts are greenCoconuts are green Grapes are sourGrapes are sour
  • 9. Interface revisited Two utility methodspublic class InterfaceDemo { static final String FILE_NAME = "/home/user/data.txt"; public static int getTotalVolume(List<Volume>items){ int total = 0; for (Volume v : items){ total += v.getVolume(); } return total; } public static int getTotalWeight(List<Weight>items){ int total = 0; for (Weight w : items){ total += w.getWeight(); } return total;}
  • 10. public static void main(String[] args) { List<Volume>vols = new ArrayList<Volume>(); List<Weight>wts = new ArrayList<Weight>(); File file = new File(FILE_NAME); try{ BufferedReader br = new BufferedReader(new FileReader(file)); String s = null; while ( (s = br.readLine()) != null){ String[] split = s.split(","); if (split[0].equals("Book")){ Book book = new Book(); book.width = Integer.parseInt(split[1]); book.height = Integer.parseInt(split[2]); book.depth = Integer.parseInt(split[3]); vols.add(book); wts.add(book); }else { Container c = new Container(); c.radius = Integer.parseInt(split[1]); c.height = Integer.parseInt(split[2]); vols.add(c); wts.add(c); } } br.close(); System.out.println("Total volume: " + getTotalVolume(vols)); System.out.println("Total weight: " + getTotalWeight(wts)); }catch(Exception e){ e.printStackTrace(); }}}
  • 11. Two interfaces interface Volume{ int getVolume(); } interface Weight{ int getWeight(); }data.txt:Book,10,20,3Book,20,40,2Container,20,40Container,30,50
  • 12. class Book implements Volume, Weight{ int id; int width; int height; int depth; public int getVolume() { return width * height * depth; } public int getWeight() { return getVolume() * 2; }}class Container implements Volume, Weight{ int id; int radius; int height; public int getVolume() { return (int)(Math.PI * radius * radius * height); } public int getWeight() { return getVolume() * 5; }}
  • 13. OutputTotal volume: 193836Total weight: 962580
  • 14. Lessons learnedPolymorphism: an object can take more than one forms. For example, getTotalVolume() sees the items as of type Volume; getTotalWeight() sees the items as of type Weight

×