oop lecture9 11
TRANSCRIPT
File IO operations in Java
Lecture 11
Object Oriented ProgrammingEastern University, Dhaka
Md. Raihan Kibria
List all files/folders under a directory
import 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
Outputoop_lecture1.pptoop_lecture7.pptoop_lecture9_10.pptoop_lecture9.pptoop_lecture3.pptcomoop_lecture9_11.pptoop_lecture4.pptoop_lecture6.pptoop_lecture5.pptoop_lecture8.ppt
How many are folders and how many are files under a directory
public 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 of files: " + countOfFile); }} Output:
No of directories: 2; no of files: 18
Other useful methods on File
Create a new File:File f = new File("D:\temp\newfile.txt");f.createNewFile();
Delete a File:File f = new File("D:\temp\newfile.txt");f.delete();
Rename a File:File f = new File("D:\temp\newfile.txt");File renF = new File("D:\temp\newfile.txt");f.renameTo(renF);
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){
} }}
Reading a text file and output to console
public 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(); } }}
Output
text.txt:
Apples are redCoconuts are greenGrapes are sour
Console output:
Apples are redCoconuts are greenGrapes are sour
Interface revisited
public 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;}
Two utility methods
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(); }}}
Two interfaces
interface Volume{ int getVolume();}
interface Weight{ int getWeight();}
data.txt:
Book,10,20,3Book,20,40,2Container,20,40Container,30,50
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; }}
Output
Total volume: 193836Total weight: 962580
Lessons learned
Polymorphism: 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