1. Systemi\o Exercises:
1) input several characters through the keyboard, and output through the display;
2) Define a static method Validornot () is used to verify the validity of the input characters, and if it contains non-English characters, the illegalstringexception exception is thrown;
3) through the Try/catch exception handling mechanism, handle the exception: if it contains non-English characters, give the corresponding prompt; If only English characters are included, the number of characters is counted and output.
1 PackageIooutput;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 Public classIotest {8 //because Validornot is a static method, the exception class should also be static or the exception cannot be thrown in the method9 Public Static classIllegalcharexceptionextendsexception{Ten One Publicillegalcharexception (String str) { A System.out.println (str); - } - } the //Throws/throw differences: Methods for throwing exceptions with S (many exceptions) House s table-specific exceptions - Public Static voidValidornot (String str)throwsexception{ - intCount=0; - for(intI=0;i<str.length (); i++) + { - CharT=str.charat (i);//analogy to the subscript of strings in C + + + if(t>= ' A ' &&t<= ' Z ' | | t>= ' A ' &&t<= ' Z ') A { atcount++; - Continue; - } - Else Throw NewIllegalcharexception ("Not Letters!");//note the object of the new instantiation exception class - } - System.out.println (count); in } - to Public Static voidMain (string[] args) { + bufferedreader BR; -String str=NULL; theSystem.out.println ("Please enter string"); *Br=NewBufferedReader (NewInputStreamReader (system.in));//Keyboard Input $ Try {Panax Notoginseng while(! (Str=br.readline ()). Equals ("End"))//A row of rows reads - { the Iotest.validornot (str); + } A the}Catch(IOException e) {//I must throw attention to IO anomaly + e.printstacktrace (); -}Catch(Exception e) { $ $ } - } -}
2. File reading and writing exercises
1) Read data from a text file (text1.txt) and copy the data to another text file (text2.txt);
2) The source file Text1.txt may contain Chinese characters, English characters, and number symbols, etc., please select the appropriate stream to complete the file read and write operations;
3) When copying files, remove non-English characters from the source file (text1.txt).
1 PackageIooutput;2 3 ImportJava.io.FileInputStream;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 Public classFiletest {9 Ten Public Static voidMain (string[] args) { One Try { A //The file path must be/cannot be \ -FileInputStream rf=NewFileInputStream ("F:/users/verlen11/workspace/iooutput/bin/text1.txt"); -FileOutputStream wf=NewFileOutputStream ("Text2.txt"); the intL=-1; - byteb[]=New byte[512]; - while((L=rf.read (b,0,512))!=-1)//Throw IO exception here//reads 512 characters at a time, L indicates the number of characters actually read, and 1 indicates that the read is complete. -Wf.write (b,0, L); + rf.close (); -Wf.close ();//Remember Close + A}Catch(IOException e) {//must throw exception IO exception including FileNotFoundException (FileInputStream must throw exception at e.printstacktrace (); - } - - } - } - in //understand the difference between a byte stream and a character stream
Io and exception handling exercises in Java