Io and exception handling exercises in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.