Import java io File Writer fw new File
- Slides: 25
כתיבה לקובץ 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 = 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 = 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. Of. Bounds. Exception חריגה ממערך : Null. Pointer. Exception null ניסיון לפעול על מערך בעל ערך 9 n n n
Exceptions סוגי Exception IOException Runtime. Exception IOException Index. Out. Of. Bounds. Exception 14
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 = 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. 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 { 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. 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 { 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 { 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
- Java applet swing
- Java
- Import java.awt.* import java.awt.event.*
- Java number
- Java import java.util.*
- Import util scanner
- Gcd java
- Java.util.random
- Import java.io.*
- Public class
- Java thread import
- Java.io.file
- Import numpy as np import matplotlib.pyplot as plt
- File-file yang dibuat oleh user pada jenis file di linux
- Boost tokenizer example
- Import java.util.stringtokenizer;
- Import javax swing
- Java applet class
- Import java.util."; public
- Import java.io.*;
- Tanslate
- G.draw
- Import java.applet.applet
- Applet adalah
- Import awt
- Javax.servlet