Basic Document Info And Yield.


73 views
Uploaded on:
Description
Bart. Simpson. 123456. This methodology is cumbersome in light of the fact that: It obliges information of all the ... Understudy aStudent = new Student(
Transcripts
Slide 1

Straightforward File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java Writing and perusing articles to and from document (by executing the Serializable interface)

Slide 2

Storing Information On Files Types of records Text records Binary records

Slide 3

110000 110001 "0" "1" 110001110010 2 3186 10 Text Files Text records Every 8 bits speaks to a character e.g., "0" = 48, "1" = 49 Binary records Includes every single other sort of records (it\'s a catch all term for non-twofold records) e.g., Interpret the bit design as a 16 bit unsigned short

Slide 4

(You Should Know Why You Can Get Garbage In Text Editors) ÿøÿà JFIF ÿû C $.\' ",#(7),01444\'9=82<.342ÿû C 2!!22222222222222222222222222222222222222222222222222ÿà " ÿä G !1AQa"2q\'¡#3B±ÁRÑáð$SbrñCs‚46c\'5%&t¢²ÿÄ ÿä * !1A"2Qa#3qBÿÚ ? òöwá»?xïMÐïäž;[¯7{ÀÀ8Û¸Á Žª;TÑ ñ‹ìt«›{ëˆgó7E`ñ$Ͷ6a\'ÊBFNOLãœUöuâ+O xóMÖïãžKko7z@ ¹Ý À$¬;ÑŸjÞðwŒ\'Ív+ÝbõmžA$M§EÚÑ:d=²rËÁÇçŒn‚Œ[èÏi¾ ñ¯§Ã}c§ùÏæþLѤ—>PÌžTlÁåÚ:윎£ßîGˆ¿²?µ?³ÿ áÿ øýžt~wá÷mó¼þg—ŸâÛŒsœsZø.4cÀ¾Óu¤Ô£:9»–?â|椻Ùá†Àü}x9©ëVº¯‡l¬Ž­âCk£¦›ø K\'ι_2C»â+³œ»ÒÞx.\'™Zº<âox\' ,j/a€Ù.¡±.¡yØľR±}œòvàsœ`ÔîOˆ²?µ?³ÿ áÿ øížt~wá÷mó¼þg—ŸâÛŒsœs^¤|C¤ý;ÏþÐÿ wþTýç÷¾o—߯µeüW¨øoYÓ4æ»]Qu3G‹K‚„b&\'2vÊÒNݬI@€çÃ­H愝"¥¥ËmMOÁ>"Ò4鯯\'ÿ

Slide 5

BufferedReader "A" 00100000 "N" 01001110 scorch stream 01000001 " byte stream FileReader "A " String Reading Text Input From A File 01000001 01001110 01000001 :

Slide 6

01000001 FileWriter PrintWriter 01001110 " "N" "A" 00100000 byte stream singe stream Writing Text Output To A File Primitives, Strings, Objects 1 "A " File 01000001 01001110 01000001 : 1 By articles we obviously mean references to objects

Slide 7

An Example Of Simple Input And Output The full case can be found in Unix in the index:/home/233/cases/fileIO/example1

Slide 8

Class IntegerWrapper open class IntegerWrapper { private int num; open IntegerWrapper () { num = (int) (Math.random() * 100); } open void setNum (int newValue) { num = newValue; } open int getNum () { return num; } }

Slide 9

Class SimpleIO import java.io.*; open class SimpleIO { open static void fundamental (String [] argv) { IntegerWrapper iw1 = new IntegerWrapper (); IntegerWrapper iw2 = new IntegerWrapper (); String filename = "data"; PrintWriter pw; FileWriter fw; BufferedReader br; FileReader fr;

Slide 10

Class SimpleIO (2) attempt { fw = new FileWriter (filename); pw = new PrintWriter (fw); System.out.println("Written to document: " + iw1.getNum()); pw.println(iw1.getNum()); System.out.println("Written to record: " + iw2.getNum()); pw.println(iw2.getNum()); fw.close();

Slide 11

Class SimpleIO (3) fr = new FileReader(filename); br = new BufferedReader(fr); System.out.println("Read from record: " + br.readLine()); System.out.println("Read from document: " + br.readLine()); fr.close(); }

Slide 12

Class SimpleIO (4) get (IOException e) { e.printStackTrace(); }

Slide 13

Reading Until The End-Of-File Is Reached String filename = "data"; BufferedReader br = invalid; FileReader fr = invalid; String temp = invalid; attempt { fr = new FileReader(filename); br = new BufferedReader(fr); temp = br.readLine (); while (temp != invalid) { : temp = br.readLine (); } :

Slide 14

Checking For More Specific Error Types String filename = invalid; BufferedReader br; FileReader fr; boolean fileError = genuine; while (fileError == genuine) { attempt { System.out.print("Enter name of information document: "); filename = Console.in.readWord(); fr = new FileReader(filename); br = new BufferedReader(fr); : fr.close (); fileError = false; }

Slide 15

Checking For More Specific Error Types (2) get (FileNotFoundException e) { System.out.println("File called " + filename + " not in the current directory"); } get (IOException e) { System.out.println("General record input blunder occured."); e.printStackTrace(); }

Slide 16

data.txt Writing Objects Out To File: "The Hard Way" The full illustration can be found in Unix in the registry:/home/233/cases/fileIO/example2 Each field is composed out to a document independently Student object: String firstName String lastName int id Bart Simpson 123456 This methodology is clumsy in light of the fact that: It requires learning of the considerable number of qualities of the class. On the off chance that qualities are not straightforward sorts or classes which can\'t be specifically composed to record the non-writable trait must be separated and kept in touch with document on a field-by premise. A few ascribes may must be parsed or changed over.

Slide 17

The Driver Class open class Driver { open static void principle (String [] args) { last String FILENAME = "data.txt"; PrintWriter pw; FileWriter fw; BufferedReader br; FileReader fr; Student aStudent = new Student("Bart", "Simpson", 123456); int tempNum; String tempLine;

Slide 18

The Driver Class (2) attempt { fw = new FileWriter (FILENAME); pw = new PrintWriter (fw); pw.println(aStudent.getFirstName()); pw.println(aStudent.getLastName()); pw.println(aStudent.getId()); fw.close(); fr = new FileReader(FILENAME); br = new BufferedReader(fr); aStudent.setFirstName(br.readLine()); aStudent.setLastName(br.readLine()); tempLine = br.readLine(); aStudent.setId(Integer.parseInt(tempLine)); fr.close(); System.out.println(aStudent); }

Slide 19

The Driver Class (3) get (FileNotFoundException e) { e.printStackTrace(); } get (IOException e) { e.printStackTrace(); } get (NumberFormatException e) { e.printStackTrace(); }

Slide 20

Class Student open class Student { private String firstName; private String lastName; private int id; open Student () { firstName = "no name"; lastName = "no name"; id = - 1; }

Slide 21

Class Student (2) open Student (String aFirstName, String aLastName, int anId) { firstName = aFirstName; lastName = aLastName; id = anId; } open String getFirstName () { return firstName; } open String getLastName () { return lastName; } open int getId () { return id; }

Slide 22

Class Student (3) open void setFirstName (String name) { firstName = name; } open void setLastName (String name) { lastName = name; } open void setId (int anId) { id = anId; } open String toString () { String s = new String (); s = s + "First name: " + firstName + "\n" + "Last name: " + lastName + "\n" + "ID No: " + id + "\n"; return s; }

Slide 23

data.txt Object is "serialized" (given a serial number) on the (yield) stream Bart Simpson 123456 Writing Objects Out To File: A Better Way The full illustration can be found in Unix in the catalog:/home/233/cases/fileIO/example3 Write every one of the information for the class at the same time Student object: String firstName String lastName int id Objects of a class can be serialized when the class actualizes the Serializable interface

Slide 24

The Driver Class open class Driver { open static void primary (String [] args) { last String FILENAME = "data.txt"; attempt {/Write item to document. ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream(FILENAME)); Student aStudent = new Student("Bart", "Simpson", 123456); out.writeObject(aStudent); out.close(); aStudent = invalid;

Slide 25

The Driver Class (2) ObjectInputStream in = new ObjectInputStream (new FileInputStream(FILENAME)); aStudent = (Student) in.readObject(); System.out.println(aStudent); } get (FileNotFoundException e) { e.printStackTrace(); } get (IOException e) { e.printStackTrace(); } get (ClassNotFoundException e) { e.printStackTrace(); }

Slide 26

The Student Class: Key Difference open class Student executes Serializa

Recommended
View more...