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文件里怎么半
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文件里怎么半
解决方案 »
- Syntax error, parameterized types are only available if source level is 1.5
- 怎么去掉一个txt文件中的中文??
- 使用ZipEntry在压缩文件的时候如何新建目录?
- 请问是否有人对做java业务平台感兴趣?
- read和write的问题
- 谁有电子工业的《Java2 API 大全》电子版,50分不够的话可以再加!!
- 一个udp的问题
- 迷茫~~~没什么进步,介绍和学习学习经验请进——this.append(散分)
- 如何将JTable指定的某个单元格的颜色改为红色?
- 网站编辑的收入??
- 谁知道哪里有《爪哇夜未眠》的下载地址?? 很想看看这本书! 谢谢了!
- 父类调用了子类的方法???
.....
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();
....
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);
}
}
}测试通过
例如java whois google.com 我接着还想在输入几个 163 。COM ;SOHU。COM怎么办啊 能一次输入几个一起查的吗
简易改造下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});
}
}
}