Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 24. In last lecture we have discussed need and advantage of file handling in development of programs. File handling enhance scope of our programs.

Similar presentations


Presentation on theme: "Lecture 24. In last lecture we have discussed need and advantage of file handling in development of programs. File handling enhance scope of our programs."— Presentation transcript:

1 Lecture 24

2 In last lecture we have discussed need and advantage of file handling in development of programs. File handling enhance scope of our programs and we can create programs having more functionality. We can create programs that can:  take long inputs from files  write output in files to share to other programs  do tasks in parts where task done in any time can be saved  fulfills needs of any business concern by saving and loading their running data [sales, purchases, employees, goods, debtors, creditors, bank cash etc. records]

3 Reading & Writing are two main operations: Writing:  use PrintWriter class from java.io  use print & println function like you use SOP  at the end close function is required Reading:  use File class from java.io [give file name & path]  use Scanner class from java.util  pass object of File to Scanner  use same functions of Scanner class you were using before for input

4 Meta data is extra information added into files by creator/ writer of file to help/ guide reader. Examples of meta-data:  count as meta-data about number of values to be read, stored in start of file  for a table type data where each row have same number of columns, row & column count as meta- data, again stored in start of file  for a table type data where each row may have different number of columns, row count as meta- data, stored in start of file, where column count as meta-data, stored before start of each row

5  4    

6  "cgpas.txt“ File file=new File("cgpas.txt"); Scanner in=new Scanner(file); final int COUNT=in.nextInt(); double cgpas[]=new double[COUNT]; int i; double sum=0; for (i=0;i

7 ScoresWkts OppositionStart Date ScoresWkts OppositionStart Date 46-England28-Aug-0630West Indies21-Apr South Africa2-Feb-07714Zimbabwe16-Sep Kenya4-Sep-07513Zimbabwe18-Sep Scotland12-Sep-07131Sri Lanka25-Nov Sri Lanka17-Sep-07252Bangladesh29-Nov Australia18-Sep-07232England23-Feb Bangladesh20-Sep-0701England25-Feb New Zealand22-Sep-0700England27-Feb-12 10India24-Sep-0700Sri Lanka1-Jun-12 DNB1Bangladesh1-May-10240Sri Lanka3-Jun Australia2-May-10172Australia5-Sep England6-May-10450Australia7-Sep-12 80New Zealand8-May-1090Australia10-Sep-12 10South Africa10-May-10430New Zealand23-Sep-12 DNB0Australia14-May-10450Bangladesh25-Sep England7-Sep-10152South Africa28-Sep South Africa26-Oct-10150India30-Sep South Africa27-Oct-1042Australia2-Oct New Zealand26-Dec-10421Sri Lanka4-Oct New Zealand28-Dec-10610India25-Dec New Zealand30-Dec-10550India28-Dec-12

8 Reading file without meta-data: File file=new File("../hafeez_t20.csv"); Scanner in=new Scanner(file); System.out.println("Score\tWkts\tCountry\t\tDate"); while (in.hasNext()){ System.out.print(in.next()+"\t"); System.out.print(in.next()+"\t\t"); System.out.println(in.next()); }

9 Output:

10 File file=new File("../hafeez_t20.csv"); Vector scores=new Vector (); Vector wkts=new Vector (); Vector countries=new Vector (); Vector matchDates=new Vector (); Scanner in=new Scanner(file); while (in.hasNext()){ scores.add(in.next()); wkts.add(in.next()); countries.add(in.next()); matchDates.add(in.next()); } int i; System.out.println("Date\t\tCountry\tScore\tWkts"); for (i=0;i

11 Output:

12 Counting number of times bat & number of times bowl int i, noBat=0, noBall=0; char ch1, ch2; for (i=0;i='0' && ch1<='9') noBat++; if (ch2>='0' && ch2<='9') noBall++; } System.out.println("No of times bat:"+noBat); System.out.println("No of times bowl:"+noBall);

13 Writing scores & wickkets with meta-data PrintWriter pw=new PrintWriter("../records.txt"); pw.println(noBat); for (i=0;i='0' && ch1<='9') pw.print(scores.get(i)+" "); } pw.println("\n"+noBall); for (i=0;i='0' && ch2<='9') pw.print(wkts.get(i)+" "); } pw.close(); Writing meta-data

14 Output file:

15 Matrix A & B saved in file quesiton.txt with meta-data:

16 Reading matrices using meta-data and storing into 2D array: File file=new File("question.txt"); Scanner in=new Scanner(file); int i, j, m1, n1, m2, n2; m1=in.nextInt(); n1=in.nextInt(); int mat1[][]=new int[m1][n1]; for (i=0;i

17 Writing result matrix with meta-data: File file=new File("question.txt"); PrintWriter pw=new PrintWriter("answer.txt"); pw.println(m1+" "+n2); for (i=0;i


Download ppt "Lecture 24. In last lecture we have discussed need and advantage of file handling in development of programs. File handling enhance scope of our programs."

Similar presentations


Ads by Google