Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default change throws IOException

    hello everyone!

    I want move throws IOException from main method to other method.
    Example:
    Java Code:
    import java.io.*;
    
    public class passSample 
    {
        public static void main(String args[]) throws IOException
        {
            InputStreamReader isReader = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isReader);
            FileWriter myFile = new FileWriter("E:/test.txt");
            BufferedWriter bw = new BufferedWriter(myFile);
                         
            System.out.println("Nhap vao, go~ 010 de ket thuc.");
            String str = new String();
            while( !(str = br.readLine()).equalsIgnoreCase("010")   )
            {
                bw.write(str);            
                bw.newLine();            
            }       
            bw.flush();
            myFile.close();
        }
    }
    to

    Java Code:
    import java.io.*;
    
    public class passSample 
    {
        public static void abc()throws IOException
       {
              
            InputStreamReader isReader = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isReader);
            FileWriter myFile = new FileWriter("E:/test.txt");
            BufferedWriter bw = new BufferedWriter(myFile);
                         
            System.out.println("Nhap vao, go~ 010 de ket thuc.");
            String str = new String();
            while( !(str = br.readLine()).equalsIgnoreCase("010")   )
            {
                bw.write(str);            
                bw.newLine();            
            }       
            bw.flush();
            myFile.close();
       }
        public static void main(String args[])
        {
             abc();
        }
    }
    but not success. how i must

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,308
    Blog Entries
    7
    Rep Power
    20

    Default

    Put all your code in your abc() method in a try catch block and catch the IOException (and handle the exception); therefore you can remove the 'throws IOException' from your abc() method and therefore there is no need to anticipate for such an exception in your main() method.

    kind regards,

    Jos

  3. #3
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    160
    Rep Power
    5

    Default please check this

    Hai pls check this
    import java.io.*;

    public class passSample
    {
    static FileWriter myFile=null;
    public static void main(String args[])
    {
    toFile();

    }
    public static void toFile(){
    try {
    InputStreamReader isReader = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isReader);
    myFile= new FileWriter("E:/test.txt");
    BufferedWriter bw = new BufferedWriter(myFile);

    System.out.println("Nhap vao, go~ 010 de ket thuc.");
    String str = new String();
    while( !(str = br.readLine()).equalsIgnoreCase("010") )
    {
    bw.write(str);
    bw.newLine();
    }
    bw.flush();

    }
    catch (Exception ex) {
    System.out.println (ex);
    }
    finally {
    try {
    myFile.close();

    }
    catch (Exception ex) {
    }

    }


    }
    }

    thanks
    Last edited by javastuden; 11-20-2009 at 10:49 AM. Reason: forget ot remove ioExcep

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    798
    Rep Power
    6

    Default

    Hi,
    Some of the suggestions for ur code.
    1.U have to close BufferedWriter,BufferedReader along with the file.

    2.Before closing just check if it is null.

    3.Don't put empty catch block.Try to print s.o.p for easy debugging
    Ramya:cool:

  5. #5
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    @RamyaSivakanth: if don't put empty catch block them what you put there ?
    try {
    myFile.close();

    }
    catch (Exception ex) {
    }

    }

    I hope you help me
    :D I'm from vietnam - I hope you help me :D

  6. #6
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    for you: what we should use: Scanner or InputStreamReader to read and write file.

    thank
    :D I'm from vietnam - I hope you help me :D

  7. #7
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    160
    Rep Power
    5

    Default

    thanks remya for your valuable suggession and its good to put sop in catch block
    then y ur checking the reader is null that please explain

    if u use Scanner class then u has to know which type of data is comming eg
    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();
    or
    Scanner sc = new Scanner(new File("myNumbers"));
    while (sc.hasNextLong()) {
    long aLong = sc.nextLong();
    }

    means the programmer must know prevoiusly which type data will come either int string,long etc but for InputStreamReader any data will take as string then change to as per ur need

  8. #8
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    thank you, Scanner good
    :D I'm from vietnam - I hope you help me :D

Similar Threads

  1. Shell() throws SWTException
    By royalibrahim in forum SWT / JFace
    Replies: 1
    Last Post: 11-09-2009, 12:59 PM
  2. Execute() throws an NPE
    By mjz in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 02:25 AM
  3. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 01:07 AM
  4. Difference between Throws and Throw
    By Poonam in forum New To Java
    Replies: 7
    Last Post: 02-06-2008, 04:52 PM
  5. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •