class System Err Test public static void mainString
Παράδειγμα class System. Err. Test { public static void main(String args[]){ System. err. println("Starting program"); for (int i = 0; i < 10; i ++){ System. out. println(i); } System. err. println("End of program"); } } Και τα δύο τυπώνουν στην οθόνη αλλά αν κάνουμε ανακατεύθυνση μόνο το System. out ανακατευθύνεται
File. Not. Found. Exception • Η δημιουργία του ρεύματος πετάει μια εξαίρεση File. Not. Found. Exception την οποία πρέπει να πιάσουμε • Η δημιουργία του ρεύματος είναι πάντα μέσα σε ένα try- catch block try { File. Output. Stream output. Stream = new File. Output. Stream(“stuff. txt”); } catch (File. Not. Found. Exception e) { System. out. println("Error opening the file stuff. txt. "); System. exit(0); }
import java. io. Print. Writer; import java. io. File. Output. Stream; import java. io. File. Not. Found. Exception; Ένα ολοκληρωμένο παράδειγμα public class Text. File. Output. Demo 1 { public static void main(String[] args) { File. Output. Stream output. Stream = null; try { output. Stream = new File. Output. Stream("stuff. txt"); } catch(File. Not. Found. Exception e) { System. out. println("Error opening the file stuff. txt. "); System. exit(0); } Print. Writer output. Writer = new Print. Writer(output. Stream); System. out. println("Writing to file. "); output. Writer. println("The quick brown fox"); output. Writer. println("jumped over the lazy dog. "); output. Writer. close( ); System. out. println("End of program. "); } }
import java. io. Print. Writer; import java. io. File. Output. Stream; import java. io. File. Not. Found. Exception; Πιο συνοπτικός κώδικας public class Text. File. Output. Demo 2 { public static void main(String[] args) Το αντικείμενο File. Output. Stream έτσι κι { αλλιώς δεν το χρησιμοποιούμε αλλού. Print. Writer output. Writer = null; Δημιουργούμε ένα ανώνυμο αντικείμενο. try { output. Writer = new Print. Writer(new File. Output. Stream("stuff. txt")); } catch(File. Not. Found. Exception e) { System. out. println("Error opening the file stuff. txt. "); System. exit(0); } System. out. println("Writing to file. "); output. Writer. println("The quick brown fox"); output. Writer. println("jumped over the lazy dog. "); output. Writer. close( ); System. out. println("End of program. "); } }
import java. util. Scanner; import java. io. File. Input. Stream; import java. io. File. Not. Found. Exception; Ένα παράδειγμα public class Text. File. Scanner. Demo { public static void main(String[] args) { Scanner input. Reader = null; try { Η συνοπτική εκδοχή του κώδικα input. Reader = new Scanner(new File. Input. Stream("morestuff. txt")); } catch(File. Not. Found. Exception e) { System. out. println("File morestuff. txt was not found"); System. out. println("or could not be opened. "); System. exit(0); } String line = input. Reader. next. Line( ); System. out. println("The line read from the file is: "); System. out. println(line); input. Stream. close( ); } }
import import java. util. Scanner; java. io. File. Input. Stream; java. io. File. Not. Found. Exception; java. io. Print. Writer; java. io. File. Output. Stream; Ένα παράδειγμα με διάβασμα και γράψιμο public class Read. Write. Demo { public static void main(String[] args ){ Scanner input. Stream = null; Print. Writer output. Stream = null; Διαβάζουμε από ένα αρχείο και γράφουμε τις γραμμές του αριθμημένες σε ένα νέο αρχείο. try { input. Stream = new Scanner(new File. Input. Stream("original. txt")); output. Stream = new Print. Writer(new File. Output. Stream("numbered. txt")); } catch(File. Not. Found. Exception e){ System. out. println("Problem opening files. "); System. exit(0); } int count = 0; while (input. Stream. has. Next. Line( )){ String line = input. Stream. next. Line( ); count++; output. Stream. println(count + " " + line); } input. Stream. close( ); output. Stream. close( ); } H has. Next. Line θα επιστρέψει false όταν φτάσουμε στο τέλος του αρχείου
import import java. util. Scanner; java. io. File. Input. Stream; java. io. File. Not. Found. Exception; java. io. Print. Writer; java. io. File. Output. Stream; Χρήση των εξαιρέσεων για έλεγχο public class Read. Write. Demo 2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); String input. Filename = keyboard. next(); String output. Filename = keyboard. next(); Scanner input. Stream = null; Print. Writer output. Stream = null; while (true) { try { input. Stream = new Scanner(new File. Input. Stream(input. Filename)); output. Stream = new Print. Writer(new File. Output. Stream(output. Filname)); break; } catch(File. Not. Found. Exception e) { System. out. println("Problem opening files. Enter names again: "); input. Filename = keyboard. next(); output. Filename = keyboard. next(); } } <υπόλοιπος κώδικας…> } }
STRING PROCESSING
Παράδειγμα: από το String: “Student: Bob Marley AM: 111” θέλουμε το όνομα του φοιτητή και το ΑΜ του class Split. Test 1{ public static void main(String args[]){ String s = "Student: Bob Marleyt. AM: 111"; System. out. println(s); Split πρώτα ως προς “t” String fields[] = s. split("t"); και μετά ως προς “: ” String student. Fields[] = fields[0]. split(": "); String student. Name = student. Fields[1]. trim(); Χρήση της trim String AMFields[] = fields[1]. split(": "); int student. AM = Integer. parse. Int(AMFields[1]. trim()); System. out. println(student. Name + "t" + student. AM); } }
Παράδειγμα class Replace. Test 1 { public static void main(String[] args){ String s 1 = "Is this a greek question? "; System. out. println("Before: " + s 1); s 1 = s 1. replace("? ", "; "); System. out. println("After: " + s 1); String s 2 = "This is not a question? "; System. out. println("Before: " + s 2); s 2 = s 2. replace("? ", ""); System. out. println("After: " + s 2); String s 3 = "20 -5 -2013"; System. out. println("Before: " + s 3); s 3 = s 3. replace("-", "/"); System. out. println("After: " + s 3); } } Αντικαθιστά το “? ” με “; ” Σβήνει το “? ” Αντικαθιστά όλα τα “-” με “/”
Παράδειγμα class Split. Test 2 { public static void main(String args[]){ String s 1 = "sentense 1tsentence 2"; String[] tokens = s 1. split("[t ]"); for (String t: tokens){ System. out. println(t); } tokens = s 1. split("\s"); for (String t: tokens){ System. out. println(t); } Split στο tab και το κενό Split σε οποιοδήποτε white space String s 2 = "To be or not to be? This is the question. The question we must face"; String[] sentences = s 2. split("[? . ]"); for (String s: sentences){ Split στο ερωτηματικό System. out. println(s. trim()); και την τελεία } } } Σβήνει τα κενά στην αρχή και το τέλος των προτάσεων
Παράδειγμα import java. util. String. Tokenizer; class String. Tokenizer. Test { public static void main(String args[]){ String s = "Line with tabt and space"; System. out. println(s); System. out. println("Split tokenization"); String[] tokens 1 = s. split("\s"); for (String t: tokens 1){ System. out. println("-"+t+"-"); } Split σε κενό και tab Δημιουργεί κενό token όταν βρει το “t ” System. out. println("String. Tokenizer tokenization"); String. Tokenizer tokens 2 = new String. Tokenizer(s); while (tokens 2. has. More. Tokens()){ System. out. println("-"+tokens 2. next. Token()+"-"); } } } Δεν δημιουργεί κενό token όταν βρει το “t ”
Παράδειγμα import java. util. String. Tokenizer; class String. Tokenizer. Test { public static void main(String args[]){ String s = "Line with tabt and space"; System. out. println(s); System. out. println("Split tokenization"); String[] tokens 1 = s. split("\s+"); for (String t: tokens 1){ System. out. println("-"+t+"-"); } Split σε τουλάχιστον ένα κενό ή tab Δεν δημιουργεί κενό token System. out. println("String. Tokenizer tokenization"); String. Tokenizer tokens 2 = new String. Tokenizer(s); while (tokens 2. has. More. Tokens()){ System. out. println("-"+tokens 2. next. Token()+"-"); } } }
import java. util. String. Tokenizer; class String. Tokenizer. Test 2 { public static void main(String args[]){ String s = "The first sentence. The second! Third? And, finally, the last one. "; System. out. println(s); String. Tokenizer tokens = new String. Tokenizer(s, ". ? !"); System. out. println(“Τokenization: "); while (tokens. has. More. Tokens()){ System. out. println(tokens. next. Token(). trim()); } } }
import java. lang. String. Builder; Θέλουμε να δημιουργήσουμε ένα String με τους αριθμούς από το 1 ως το Ν class String. Builder. Test { public static void main(String[] args){ int N = 100000; String s = ""; for (int i = 0; i < 100000; i ++){ s = s + " " +i; } System. out. println(s); String. Builder sb = new String. Builder(); for (int i = 0; i < 100000; i ++){ sb. append(" " +i); } System. out. println(sb. to. String()); } } Ο μπλε κώδικας είναι πολύ πιο γρήγορος από τον πράσινο Ο πράσινος αντιγράφει το String Ν φορές
import java. lang. String. Builder; class String. Builder. Test 2 { public static void main(String[] args){ String. Builder sb = new String. Builder(); for (int i = 0; i < 10; i ++){ Person p = new Person("Some Person", i); sb. append(p+"n"); } String s = sb. to. String(); Καλείται η μέθοδος to. String System. out. println(s); της Person και συνενώνεται στο τέλος του υπάρχοντος } String }
import import java. util. Scanner; java. io. File. Input. Stream; java. io. File. Not. Found. Exception; java. io. Print. Writer; java. io. File. Output. Stream; import java. util. Hash. Map; class Join { public static void main(String[] args){ Scanner name. Input. Stream = null; Scanner grades. Input. Stream = null; Print. Writer output. Stream = null; try { Άνοιγμα των αρχείων εισόδου για διάβασμα και του αρχείου εξόδου για γράψιμο name. Input. Stream = new Scanner( new File. Input. Stream("student. Names. txt")); grades. Input. Stream = new Scanner( new File. Input. Stream("student. Grades. txt")); output. Stream = new Print. Writer( new File. Output. Stream("student. Names. Grades. txt")); } catch(File. Not. Found. Exception e) { System. out. println("Problem opening files. "); System. exit(0); } Συνέχεια στην επόμενη
Συνέχεια από την προηγούμενη Hash. Map<Integer, String> names. Hash = new Hash. Map<Integer, String>(); while (name. Input. Stream. has. Next. Line( )) { String line = name. Input. Stream. next. Line( ); Διάβασε τα ζεύγη ΑΜ, όνομα String[] fields = line. split("t"); και βάλε τα σε ένα Hash. Map Integer AM = Integer. parse. Int(fields[0]); String name = fields[1]; με κλειδί το ΑΜ names. Hash. put(AM, name); } Υποθέτουμε ότι το κάθε ΑΜ εμφανίζεται μόνο μία φορά name. Input. Stream. close( ); Διάβασε τα ζεύγη ΑΜ, βαθμός while (grades. Input. Stream. has. Next. Line( )) και έλεγξε αν το ΑΜ { String line = grades. Input. Stream. next. Line( ); εμφανίζεται ως κλειδί στο Hash. Map. String[] fields = line. split("t"); Integer AM = Integer. parse. Int(fields[0]); String grade = fields[1]; Αν ναι τύπωσε ΑΜ, όνομα if (!name. Hash. contains. Key(AM)){ continue; } βαθμό στο αρχείο εξόδου String name = names. Hash. get(AM); output. Stream. println(AM+"t"+name+"t"+grade); } grades. Input. Stream. close(); output. Stream. close( ); } } και
import import java. util. Scanner; java. io. File. Input. Stream; java. io. File. Not. Found. Exception; java. io. Print. Writer; java. io. File. Output. Stream; import java. util. Hash. Map; Η μέθοδος πετάει εξαίρεση εφόσον δεν την χειρίζεται class Join 2 { private static Hash. Map<Integer, String> read. Student. Names(String filename) throws File. Not. Found. Exception{ Scanner name. Input. Stream = new Scanner(new File. Input. Stream(filename)); Hash. Map<Integer, String> names. Hash = new Hash. Map<Integer, String>(); while (name. Input. Stream. has. Next. Line( )) { String line = name. Input. Stream. next. Line( ); String[] fields = line. split("t"); Integer AM = Integer. parse. Int(fields[0]); String name = fields[1]; names. Hash. put(AM, name); } name. Input. Stream. close( ); return names. Hash; } Συνέχεια στην επόμενη
Συνέχεια από την προηγούμενη private static void join. And. Write( Hash. Map<Integer, String> names. Hash, String grades. Filename, String output. Filename) throws File. Not. Found. Exception{ Scanner grades. Input. Stream = new Scanner(new File. Input. Stream(grades. Filename)); Print. Writer output. Stream = new Print. Writer(new File. Output. Stream(output. Filename)); while (grades. Input. Stream. has. Next. Line( )){ String line = grades. Input. Stream. next. Line( ); String[] fields = line. split("t"); Integer AM = Integer. parse. Int(fields[0]); String grade = fields[1]; String name = names. Hash. get(AM); output. Stream. println(AM+"t"+name+"t"+grade); } grades. Input. Stream. close(); output. Stream. close( ); Συνέχεια στην επόμενη
Συνέχεια από την προηγούμενη public static void main(String[] args){ Hash. Map<Integer, String> names. Hash = null; try { names. Hash = read. Student. Names("student. Names. txt"); }catch(File. Not. Found. Exception e) { System. out. println("Problem opening student names file. "); System. exit(0); } Η κλήση των read. Student. Names try { και join. And. Write θα πρέπει να γίνει μέσα σε try-catch block join. And. Write(names. Hash, "student. Grades. txt", "student. Names. Grades. txt"); }catch(File. Not. Found. Exception e) { System. out. println("Problem opening files. "); System. exit(0); } } }
- Slides: 52