java套接字编程检查域是否为真错误

java套接字编程检查域是否为真错误,java,sockets,https,network-connection,Java,Sockets,Https,Network Connection,我正在从文件中读取域,并检查域格式是否正确。如果域是正确的,则将其写入true.txt;如果域不是正确的,则将其写入error.txt。我的问题是,程序从文件中读取一些域后会抛出异常。我已在下面附上我的代码和错误: /********************************************************************************************/ BufferedReader br = null; FileWriter fw=n

我正在从文件中读取域,并检查域格式是否正确。如果域是正确的,则将其写入true.txt;如果域不是正确的,则将其写入error.txt。我的问题是,程序从文件中读取一些域后会抛出异常。我已在下面附上我的代码和错误:

/********************************************************************************************/


  BufferedReader br = null;
  FileWriter fw=null;
  FileWriter ft=null;

    String strLine = "";

    try {
        br = new BufferedReader( new FileReader("test.txt"));
        fw= new  FileWriter("wrongfile.txt");
            ft= new  FileWriter("truefile.txt");


        while( (strLine = br.readLine()) != null){
            System.out.println(strLine);
            URL u = new URL ("http://"+strLine);
   HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
   huc.setRequestMethod ("GET");  
   huc.setRequestMethod ("HEAD"); 
   huc.connect();

   int coded = huc.getResponseCode() ;
            System.out.println("code:"+coded);
  int  code=coded/100;
  if(code==3||code==4||code==5)
    {
        fw.write(strLine+":error:"+coded+"\n");
         System.out.println("domain is not ok:"+coded);
    }
   else
  {
       ft.write(strLine+"\n");
       System.out.println("domain is ok:");

   }

        }

    } catch (FileNotFoundException e) {
        System.err.println("File not found");

    } 
     br.close();
         fw.close();
         ft.close();
 }
以下是错误部分:

           Exception in thread "main" java.net.UnknownHostException: bushido.academy
           at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
           at java.net.Socket.connect(Socket.java:589)
           at java.net.Socket.connect(Socket.java:538)
           at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
     at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
   at sun.net.www.http.HttpClient.New(HttpClient.java:308)
   at sun.net.www.http.HttpClient.New(HttpClient.java:326)
   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
   at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at checker.Urlchecker.main(Urlchecker.java:50)
  C:\Users\Imran\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
线程“main”java.net.UnknownHostException中的异常:bushido.academy
位于java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
位于java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
位于java.net.socksocketimpl.connect(socksocketimpl.java:392)
位于java.net.Socket.connect(Socket.java:589)
位于java.net.Socket.connect(Socket.java:538)
位于sun.net.NetworkClient.doConnect(NetworkClient.java:180)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
http.HttpClient.(HttpClient.java:211)
http.HttpClient.New(HttpClient.java:308)
http.HttpClient.New(HttpClient.java:326)
位于sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
位于sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
位于checker.Urlchecker.main(Urlchecker.java:50)
C:\Users\Imran\AppData\Local\NetBeans\Cache\8.2\executor snippets\run.xml:53:Java返回:1

当您尝试连接到无效域时,不清楚您预期会发生什么,但会发生的是您将收到异常。您需要捕获异常并将该域写入
ErrorFile.txt
   public static void main(String[] args) throws IOException {
   int coded = 0;
   int counter=0;
   int num=10;

      Scanner in = new Scanner(System.in);
    System.out.print("Enter the file name:");
    String filename=in.nextLine();

  BufferedReader br = null;
  FileWriter fw=null;
  FileWriter ft=null;
  String f="truefile.txt";
    String strLine = "";

        br = new BufferedReader( new FileReader(filename));
        fw= new  FileWriter("wrongfile.txt");

            ft= new  FileWriter(f);


        while( (strLine = br.readLine()) != null){


    try {
            System.out.println(strLine);

            URL u = new URL ("http://"+strLine);
    HttpURLConnection huc =  ( HttpURLConnection )  
    u.openConnection () ; 





    //huc.setRequestMethod ("GET");  
       huc.setRequestMethod ("HEAD");

    coded = huc.getResponseCode() ;

  int  code=coded/100;

  if(code==4 || code==5 || coded==303)
    {
        fw.write(strLine+":error:"+coded+"\n"+"\n");
         System.out.println("domain is not ok error:"+coded);
    }

   else
  {
       ft.write(strLine+"\n"+"\n");
       System.out.println("domain is ok:");

   }
    counter++;
            System.out.println("total domain checked:"+counter);

            } 

    catch (FileNotFoundException e) {
        System.err.println("File not found");

    }
    catch (IOException e) {
        System.out.println("domain not exist fatal error");


    }

   catch(InterruptedException e)
   {
       System.out.println("timeout");
   }


        }
       br.close();
       ft.close();
       fw.close();



 }



}