Import java io File Writer fw new File

  • Slides: 25
Download presentation

 כתיבה לקובץ Import java. io. *; . . . File. Writer fw =

כתיבה לקובץ Import java. io. *; . . . File. Writer fw = new File. Writer("out. txt"); Print. Writer pw = new Print. Writer(fw); pw. println("First line to write!"); pw. println("Second line!"); pw. close(); fw. close(); 5

 קריאה מקובץ Import java. io. *; . . . File. Reader fr =

קריאה מקובץ Import java. io. *; . . . File. Reader fr = new File. Reader("out. txt"); Buffered. Reader br = new Buffered. Reader(fr); String s 1 = br. read. Line(); while(s 1!=null) { System. out. println(s 1); s 1 = br. read. Line(); } br. close(); fr. close(); 6

 תוכנית שכותבת תוכנית Scanner sc = new Scanner(System. in); File. Writer fw =

תוכנית שכותבת תוכנית Scanner sc = new Scanner(System. in); File. Writer fw = new File. Writer("My. Name. java"); Print. Writer pw = new Print. Writer(fw); System. out. print("Please enter name: "); String name = sc. next(); pw. println("public class My. Name {"); pw. println("t"+"public static void main(String args[]) {"); pw. println("tt"+"System. out. println(""+name+""); pw. println("t"+"}"); pw. println("}"); pw. close(); fw. close(); 7

Exceptions- בעצם כבר נתקלנו ב : Arithmetic. Exception ניסיון חלוקה באפס : Index. Out.

Exceptions- בעצם כבר נתקלנו ב : Arithmetic. Exception ניסיון חלוקה באפס : Index. Out. Of. Bounds. Exception חריגה ממערך : Null. Pointer. Exception null ניסיון לפעול על מערך בעל ערך 9 n n n

Exceptions סוגי Exception IOException Runtime. Exception IOException Index. Out. Of. Bounds. Exception 14

Exceptions סוגי Exception IOException Runtime. Exception IOException Index. Out. Of. Bounds. Exception 14

Exception זריקת public static int. Value(char c) { final String conversion = "0123456789 ABCDEF";

Exception זריקת public static int. Value(char c) { final String conversion = "0123456789 ABCDEF"; int ans = conversion. index. Of(c); if (ans == -1) throw new Runtime. Exception(“int. Value got an incorrect character: ”+c); return ans; } 15

Exception תפיסת public static int. Value(String s) {. . . for (int i =

Exception תפיסת public static int. Value(String s) {. . . for (int i = s. length()-1; i >= first; i = i-1) { try { value = value + int. Value(s. char. At(i)) * power; power = power * base; } catch (Runtime. Exception e) { System. out. println(e. get. Message()+" - skipping the character"); } } 17

 הקוד הבא לא מתקמפל File. Writer fw = new File. Writer("out. txt"); Print.

הקוד הבא לא מתקמפל File. Writer fw = new File. Writer("out. txt"); Print. Writer pw = new Print. Writer(fw); pw. println("First line to write!"); pw. println("Second line!"); pw. close(); fw. close(); Exceptions- מחייב התייחסות ל IO קוד של 19 n

catch- ו try- שימוש ב public static void write. To. File() { try {

catch- ו try- שימוש ב public static void write. To. File() { try { File. Writer fw = new File. Writer("out. txt"); Print. Writer pw = new Print. Writer(fw); pw. println("First line to write!"); pw. println("Second line!"); pw. close(); fw. close(); } catch(IOException e) { System. out. print("Error while writing: " + e); } } 21

 הלאה Exception העברת public static void write. To. File() throws IOException { File.

הלאה Exception העברת public static void write. To. File() throws IOException { File. Writer fw = new File. Writer("out. txt"); Print. Writer pw = new Print. Writer(fw); pw. println("First line to write!"); pw. println("Second line!"); pw. close(); fw. close(); } 22

 חישוב ממוצע ציונים מקובץ public static int calc. Average(String file. Name) throws IOException

חישוב ממוצע ציונים מקובץ public static int calc. Average(String file. Name) throws IOException { int num. Of. Grades = 0, sum = 0; File. Reader fr = new File. Reader(file. Name); Buffered. Reader br = new Buffered. Reader(fr); String s 1 = br. read. Line(); while(s 1!=null) { sum = sum + int. Value(s 1); num. Of. Grades = num. Of. Grades + 1; s 1 = br. read. Line(); } br. close(); fr. close(); return sum/num. Of. Grades; } 23

 תפיסת חריגות בפונקציה הקוראת public static void main(String[] args) { int average; try

תפיסת חריגות בפונקציה הקוראת public static void main(String[] args) { int average; try { average = calc. Average("grades 2. txt"); System. out. println("The average is "+average); } catch (IOException e 1){ System. out. println("Problem with the file"); } catch (Arithmetic. Exception e 2){ System. out. println("The average is 0"); } } 25