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  3 23 32 16  4 19 15 28 34  2 23 39  3 15 18 19

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1371185/8/slides/slide_5.jpg", "name": " 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-11 25-South Africa2-Feb-07714Zimbabwe16-Sep-11 122Kenya4-Sep-07513Zimbabwe18-Sep-11 181Scotland12-Sep-07131Sri Lanka25-Nov-11 101Sri Lanka17-Sep-07252Bangladesh29-Nov-11 151Australia18-Sep-07232England23-Feb-12 232Bangladesh20-Sep-0701England25-Feb-12 320New Zealand22-Sep-0700England27-Feb-12 10India24-Sep-0700Sri Lanka1-Jun-12 DNB1Bangladesh1-May-10240Sri Lanka3-Jun-12 121Australia2-May-10172Australia5-Sep-12 180England6-May-10450Australia7-Sep-12 80New Zealand8-May-1090Australia10-Sep-12 10South Africa10-May-10430New Zealand23-Sep-12 DNB0Australia14-May-10450Bangladesh25-Sep-12 140England7-Sep-10152South Africa28-Sep-12 131South Africa26-Oct-10150India30-Sep-12 141South Africa27-Oct-1042Australia2-Oct-12 241New Zealand26-Dec-10421Sri Lanka4-Oct-12 46-New Zealand28-Dec-10610India25-Dec-12 34-New Zealand30-Dec-10550India28-Dec-12 http://stats.espncricinfo.com/ci/engine/player/41434.html?class=3;orderby=start;template=results;type=allround;view=match

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1371185/8/slides/slide_9.jpg", "name": "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: 40 46 25 12 18 10 15 23 32 1 12 18 8 1 14 13 14 24 46 34 3 71 51 13 25 23 0 0 0 24 17 45 9 43 45 15 15 4 42 61 55 38 2 1 1 1 2 0 0 1 1 0 0 0 0 0 1 1 1 0 4 3 1 2 2 1 0 0 0 2 0 0 0 0 2 0 2 1 0 0

15 Matrix A & B saved in file quesiton.txt with meta-data: 2 3 9 7 3 6 4 8 3 4 2 5 1 6 3 7 2 4 4 1 5 3

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1371185/8/slides/slide_15.jpg", "name": "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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1371185/8/slides/slide_16.jpg", "name": "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

18 Matrix result stored in file answer.txt with meta- data: 2 4 51 97 38 91 56 66 54 76 Check my multiplication code is correct or not, I have not verified


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