import java.net.*;
import java.io.*;
public class whois
{
public final static int port = 43;
public final static String hostname = "whois.internic.net";public static void main(String[] args)
{Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;//检查命令行参数
if (args.length <1)
{
System.out.println("\nUsage: java whois <command>");
System.out.println("Parameters:");
System.out.println(
"\tcommand = one or more Domain name, or other command.");
System.out.println("Example:");
System.out.println("\tjava whois sohu.com");
System.out.println("\tjava whois help");System.exit(1); //退出
}try {
//在TCP服务端口43(十进制)连接SRI-NIC服务主机
theSocket = new Socket(hostname, port, true);
ps = new PrintStream(theSocket.getOutputStream());
//发送用户提供的一个或多个命令
for (int i = 0; i < args.length; i++)
ps.print(args[i] + " ");
//以回车和换行(<CRLF>)结尾
ps.print("\r\n");//接受相应命令的返回信息
theWhoisStream = new DataInputStream(theSocket.getInputStream());
String s;
while ((s = theWhoisStream.readLine()) != null) {
System.out.println(s);
}//关闭DataInputStream和PrintWriter
theWhoisStream.close();
ps.close();
//关闭socket
theSocket.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}
这个是一个WHOIS协议的查找域名的
想把该JAVA程序的结果输出到TXT文件里怎么半

解决方案 »

  1.   


    .....
    BufferedWriter bw = new BufferedWriter(new FileWriter("C:/whois_text.txt"));
    while ((s = theWhoisStream.readLine()) != null) {
        System.out.println(s);
        bw.write(s);
        bw.newLine();   
    }
    bw.flush();
    bw.close();
    ....
      

  2.   

    import java.net.*;
    import java.io.*;
    public class whois {
    public final static int port = 43; public final static String hostname = "whois.internic.net"; public static void main(String[] args) { Socket theSocket;
    DataInputStream theWhoisStream;
    PrintStream ps;
    File f;
    FileOutputStream fos;
    BufferedWriter bw;
    // 检查命令行参数
    if (args.length < 1) {
    System.out.println("\nUsage: java whois  <command>");
    System.out.println("Parameters:");
    System.out
    .println("\tcommand = one or more Domain name, or other command.");
    System.out.println("Example:");
    System.out.println("\tjava whois sohu.com");
    System.out.println("\tjava whois help"); System.exit(1); // 退出
    }
    try {
    // 在TCP服务端口43(十进制)连接SRI-NIC服务主机
    theSocket = new Socket(hostname, port, true);
    ps = new PrintStream(theSocket.getOutputStream());
    // 发送用户提供的一个或多个命令
    for (int i = 0; i < args.length; i++)
    ps.print(args[i] + " ");
    // 以回车和换行( <CRLF>)结尾
    ps.print("\r\n"); // 接受相应命令的返回信息
    theWhoisStream = new DataInputStream(theSocket.getInputStream());
    String s;
    f=new File("f:\\temp.txt");
     fos =new FileOutputStream(f);
     bw=new BufferedWriter(new OutputStreamWriter(fos));
    while ((s = theWhoisStream.readLine()) != null) {
    //System.out.println(s);
    bw.write(s); }
    bw.flush();
    bw.close(); // 关闭DataInputStream和PrintWriter
    theWhoisStream.close();
    ps.close();
    // 关闭socket
    theSocket.close();
    } catch (IOException e) {
    System.err.println(e);
    }
    }
    }测试通过
      

  3.   

    再问个问题如果我要一次多输入几个查询的内容要怎么半啊
    例如java whois google.com 我接着还想在输入几个 163 。COM ;SOHU。COM怎么办啊 能一次输入几个一起查的吗
      

  4.   

    比较容易想到的是,用循环多次调用
    简易改造下lz的代码
    import java.net.*;
    import java.io.*;public class whois {
        public final static int port = 43;
        public final static String hostname = "whois.internic.net";    public void doSomething(String[] args){        Socket theSocket;
            DataInputStream theWhoisStream;
            PrintStream ps;        // 检查命令行参数
            if (args.length < 1) {
                System.out.println("\nUsage: java whois  <command>");
                System.out.println("Parameters:");
                System.out
                        .println("\tcommand = one or more Domain name, or other command.");
                System.out.println("Example:");
                System.out.println("\tjava whois sohu.com");
                System.out.println("\tjava whois help");            System.exit(1); // 退出
            }        try {
                // 在TCP服务端口43(十进制)连接SRI-NIC服务主机
                theSocket = new Socket(hostname, port, true);
                ps = new PrintStream(theSocket.getOutputStream());
                // 发送用户提供的一个或多个命令
                for (int i = 0; i < args.length; i++)
                    ps.print(args[i] + " ");
                // 以回车和换行( <CRLF>)结尾
                ps.print("\r\n");            // 接受相应命令的返回信息
                theWhoisStream = new DataInputStream(theSocket.getInputStream());
                String s;
                BufferedWriter bw = new BufferedWriter(new FileWriter(
                        "D:/story/ip.txt",true)); // 附加方式 append
                while ((s = theWhoisStream.readLine()) != null) {
                    System.out.println(s);
                    bw.write(s);
                    bw.newLine();
                }
                bw.flush();
                bw.close();
                // 关闭DataInputStream和PrintWriter
                theWhoisStream.close();
                ps.close();
                // 关闭socket
                theSocket.close();
            } catch (IOException e) {
                System.err.println(e);
            }        
        }
        
        public static void main(String[] args) {
            whois test = new whois();
            for(String s:args){
                test.doSomething(new String[]{s});
            }
        }
    }