11
(String).. java.lang.stringbuffer. s String s = "abcd"; s = s + "e"; a b c d e a b c d e
,., "910359,, " "910359" " " " " (token) (token),, (delimiter).
java.util.stringtokenizer String s = "910359,, "; StringTokenizer t = new StringTokenizer(s, ","); t.nexttoken(); // "910359" t.nexttoken(); // " " t.nexttoken(); // " " t.nexttoken(); // NoSuchElementException
String s = "$13.46"; StringTokenizer t = new StringTokenizer(s, "$."); t.nexttoken(); // "13" t.nexttoken(); // "46" t.nexttoken(); // NoSuchElementException
class StringTokenizer new StringTokenizer (String text, String delim) text delim nexttoken(): String nexttoken(string new_delimiters): String hasmoretokens(): boolean, 0 nexttoken() counttokens(): int
(Files) (file): (character file): (binary file): 0 1 (open) (close).. (input) : (output) :
FileWriter.. PrintWriter FileWriter print, println PrintWriter ofile = new PrintWriter (new FileWriter("file.txt")); PrintWrit er print FileWriter file
, IO import java.io.*; public class Output1 { IOException. public static void main(string[] args) throws IOException { PrintWriter outfile = new PrintWriter(new FileWriter("test.txt")); outfile.println("hello to you"); outfile.print("how are"); outfile.println(" you?"); outfile.println(47+2); outfile.close();
FileReader.. BufferedReader FileReader readline BufferedReader ifile = new BufferedReader (new FileReader("file.txt")); BufferedRe ader readline FileReader file
, import java.io.*; import javax.swing.*; public class CopyFile { public static void main(string[] args) throws IOException { String f = JOptionPane.showInputDialog("Input filename, please: "); BufferedReader infile = new BufferedReader(new FileReader(f)); PrintWriter outfile = new PrintWriter(new FileWriter(f + ".out")); while (infile.ready()) { outfile.println(infile.readline()); infile.close(); outfile.close();
GUI
, import java.io.*; import javax.swing.*; public class CopyFile { public static void main(string[] args) throws IOException { JFileChooser chooser = new JFileChooser(); chooser.setdialogtitle("."); int result = chooser.showdialog(null, "Copy"); if(result = JFileChooser.APPROVE_OPTION) System.exit(0); String f = chooser.getselectedfile().tostring(); BufferedReader infile = new BufferedReader(new FileReader(f)); PrintWriter outfile = new PrintWriter(new FileWriter(f + ".out")); while (infile.ready()) { outfile.println(infile.readline()); infile.close(); outfile.close();
: System.out: System.out.println System.in: System.in InputStream. System.in BufferedReader BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String s = keyboard.readline();
, 31 20250 42 24500 18 18000 627750 1029000 324000 PayrollReader.,, PayrollWriter.,.
class PayrollReader Methods getnextrecord(): boolean.. nameof(): String. hoursof(): int. payrateof(): double close()..
mport java.io.*; import java.util.*; public class PayrollReader { private BufferedReader infile; private String EOF = ""; private String name; private int hours, payrate; public PayrollReader(String file_name) { infile = new BufferedReader(new FileReader(file_name)); public String nameof() { return name; public int hoursof() { return hours; public int payrateof() { return payrate; public void close() { infile.close();
public boolean getnextrecord() { if(infile.ready()) return false; String line = infile.readline(); StringTokenizer t = new StringTokenizer(line, " "); String s = t.nexttoken().trim(); if(s.equals(eof) t.counttokens() = 2) return false; name = s; hours = new Integer(t.nextToken().trim()).intValue(); payrate = new Integer(t.nextToken().trim()).intValue(); return true;
import javax.swing.*; public class Payroll { private static void processpayroll(string in, String out) { PayrollReader reader = new PayrollReader(in); PayrollWriter writer = new PayrollWriter(out); while(reader.getnextrecord()) { int pay = reader.hoursof() * reader.payrateof(); writer.printcheck(reader.nameof(), pay); reader.close(); writer.close(); public static void main(string[] args) { String in_name = JOptionPane.showInputDialog("Plead type input payroll name: "); String out_name = JOptionPane.showInputDialog("Plead type output payroll name: "); if(in_name = null && out_name = null) processpayroll(in_name, out_name);
getnextrecord??.?. false (log)
? infile.readline() IOException. false. t.counttoken() = 2
try, catch. catch try { ;... catch ( ) { ;...
public boolean getnextrecord() { if(infile.ready()) return false; String line = infile.readline(); StringTokenizer t = new StringTokenizer(line, " "); String s = t.nexttoken().trim(); if(s.equals(eof) t.counttokens() = 2) return false; name = s; hours = new Integer(t.nextToken().trim()).intValue(); payrate = new Integer(t.nextToken().trim()).intValue(); return true;
public boolean getnextrecord() { boolean result = false; try { if(infile.ready()) return false; String line = infile.readline();... if(s.equals(eof) && t.counttokens() == 2) {... result = true; catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); return result;
public boolean getnextrecord() { boolean result = false; try { if(infile.ready()) return false; String line = infile.readline();... if(s.equals(eof)) { if(t.counttokens() == 2) {... result = true; else { System.out.println("PayrollReader: bad record format: " + line + " Skipping record"); result = getnextrecord(); catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); return result;
public boolean getnextrecord() { boolean result = false; try {... if(s.equals(eof)) { if(t.counttokens() == 2) {... result = true; else throw new RuntimeException(line); catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); catch (RuntimeException e) { System.out.println("PayrollReader error: bad record format: " + e.getmessage() + " Skipping record"); result = getnextrecord(); return result;
., Exception in thread "main" java.io.filenotfoundexception: /Volumes/Pamela/ Users/oukseh/11.csv (Permission denied) at java.io.fileinputstream.open(native Method) at java.io.fileinputstream.<init>(fileinputstream.java:120) at java.io.fileinputstream.<init>(fileinputstream.java:79) at java.io.filereader.<init>(filereader.java:41) at CopyFile.main(CopyFile.java:11)
getmessage(): String tostring(): String. printstacktrace (),
ArithmeticException ArrayIndexOutOfBound sexception ClassCastException NullPointerException FileNotFoundExcept EOFException InterruptedIOExcept
catch catch(< > e) {... < >. Exception RuntimeException IOException
, private int readanintfrom(bufferedreader view) throws IOException { int num; try { System.out.print("Type an int: "); String s = view.readline(); num = new Integer(s).intValue(); catch (Exception e) { System.out.println("Error: " + e.getmessage() + " not an integer; try again."); num = readanintfrom(view); // restart return num; Exception RuntimeException RuntimeException NumberFormatException
throws.. Java RuntimeException..
public class ExceptionExample { public ExceptionExample() { public void f() { try { g(); catch (RuntimeException e) { System.out.println("caught at f"); System.out.println("f completes"); public void g() { try { PrintWriter outfile = new PrintWriter(new FileWriter("text.out")); try { outfile.println( h() ); catch (NullPointerException e) { System.out.println("null pointer caught at g"); catch (IOException e) { System.out.println("io error caught at g"); System.out.println("g completes"); private int h() { int[] r = new int[2]; return r[3]; f?
A, B. try {... catch (?) {... 1: throw new RuntimeException("A"); throw new RuntimeException("B"); try {... catch (RuntimeException e) { String m = e.getmessage(); if(m.equals("a"))... if(m.equals("b"))...
2: class MyExceptionA extends Exception { class MyExceptionB extends Exception { throw new MyExceptionA(); throw new MyExceptionB(); try {... catch (MyExceptionA e) {... catch (MyExceptionB e) {...