我想把该JAVA程序的结果输出到TXT文件里怎么半 求教 谢谢

jacksto 2008-04-10 09:32:40
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文件里怎么半
...全文
2144 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinyong3771912 2011-07-07
  • 打赏
  • 举报
回复
真的很不错呢!
jacksto 2008-04-10
  • 打赏
  • 举报
回复
顶楼上的 哈哈
nihuajie05 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bootupnow 的回复:]
引用 6 楼 jacksto 的回复:
你们都是好人 谢谢


CSDNer都是活雷锋,hoho~
[/Quote]
你说的好!!!
bootupnow 2008-04-10
  • 打赏
  • 举报
回复
比较容易想到的是,用循环多次调用
简易改造下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});
}
}
}
jacksto 2008-04-10
  • 打赏
  • 举报
回复
再问个问题如果我要一次多输入几个查询的内容要怎么半啊
例如java whois google.com 我接着还想在输入几个 163 。COM ;SOHU。COM怎么办啊 能一次输入几个一起查的吗
bootupnow 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jacksto 的回复:]
你们都是好人 谢谢

[/Quote]
CSDNer都是活雷锋,hoho~
jacksto 2008-04-10
  • 打赏
  • 举报
回复
你们都是好人 谢谢
J_Factory 2008-04-10
  • 打赏
  • 举报
回复
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);
}
}
}

测试通过
hmsuccess 2008-04-10
  • 打赏
  • 举报
回复
同上,
jacksto 2008-04-10
  • 打赏
  • 举报
回复
谢谢各位高手
bootupnow 2008-04-10
  • 打赏
  • 举报
回复

.....
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();
....
bootupnow 2008-04-10
  • 打赏
  • 举报
回复
套一个输出流到文件就可以了。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧