发布网友 发布时间:2022-04-21 22:18
共3个回答
热心网友 时间:2022-04-11 08:48
package sockettest;
import java.io.*;
import java.net.*;
import java.lang.Thread;
public class Client extends Thread{
private char[] buffer=new char[2048];
private Socket socket=null;
private String localIP="";
private String farIP="";
private int localPort=8765;
private int farPort=0;
private InputStream inKeyboard=System.in;
private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);
private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据
private InputStream in=null;
private FileInputStream inFile=null;
private InputStreamReader inRead=null;
private BufferedReader read=null;
private OutputStream out=null;
private FileOutputStream outFile=null;
private OutputStreamWriter outWrite=null;
private BufferedWriter write=null;
private int timeout=30000;
public void clearBuffer(){
for(int i=0;i<buffer.length;i++)
buffer[i]=0;
}
public Client()throws Exception {
socket=new Socket();
}
public Client(String ip,int port)throws Exception{
socket=new Socket(ip,port);
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void bind(String ip,int port)throws Exception{ //绑定工作
SocketAddress serverAddr=new InetSocketAddress(ip,port);
socket.bind(serverAddr);
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void setTimeout(int time)throws Exception{
timeout=time;
socket.setSoTimeout(timeout);
}
public void begin()throws Exception{ //开始输入输出的变更封装
in=socket.getInputStream();
inRead=new InputStreamReader(in);
read=new BufferedReader(inRead);
out=socket.getOutputStream();
outWrite=new OutputStreamWriter(out);
write=new BufferedWriter(outWrite);
}
public void writeFromKeyboard()throws Exception{ //使用键盘的输入写到套接字连接 :*每次128个字节
String s="";
s=getKeyboard.readLine();
write.write(s);
write.flush();
}
public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度
inFile=new FileInputStream(filePath);
InputStreamReader i=new InputStreamReader(inFile);
BufferedReader r=new BufferedReader(i);
r.skip(off);
char[] data=new char[len];
int num=r.read(data);
r.close();
write.write(data,0,num);
write.flush();
}
public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :*1024个字节
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
return s;
}
public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)
outFile=new FileOutputStream(filePath,on);
OutputStreamWriter o=new OutputStreamWriter(outFile);
BufferedWriter w=new BufferedWriter(o);
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
w.write(s);
w.flush();
}
public void closeLink()throws Exception{ //关闭当前套接字连接
socket.close();
}
public String getLocalIP()throws Exception{ //返回本地地址
return localIP;
}
public int getLocalPort()throws Exception{ //返回本地端口
return localPort;
}
public String getFarIP()throws Exception{ //返回远程主机地址
farIP=socket.getInetAddress().getHostAddress();
return farIP;
}
public int getFarPort()throws Exception{ //返回远程主机端口
farPort=socket.getPort();
return farPort;
}
public void setBufferSize(int size){
buffer=new char[size];
}
}
---------------------------------------------------
package sockettest;
import java.io.*;
import java.net.*;
import java.lang.Thread;
public class Server extends Thread{
private char[] buffer=new char[2048];
private ServerSocket server=null;
private Socket socket=null;
private String localIP="";
private String farIP="";
private int localPort=8765;
private int farPort=0;
private InputStream inKeyboard=System.in;
private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);
private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据
private InputStream in=null;
private FileInputStream inFile=null;
private InputStreamReader inRead=null;
private BufferedReader read=null;
private OutputStream out=null;
private FileOutputStream outFile=null;
private OutputStreamWriter outWrite=null;
private BufferedWriter write=null;
private int timeout=30000;
public void clearBuffer(){
for(int i=0;i<buffer.length;i++)
buffer[i]=0;
}
public Server(String ip,int port) throws Exception{ //参数:服务器地址 端口
SocketAddress serverAddr=new InetSocketAddress(ip,port);
server=new ServerSocket();
server.bind(serverAddr);
server.setSoTimeout(timeout);
}
public Server()throws Exception{
SocketAddress serverAddr=new InetSocketAddress("127.0.0.1",8765);
server=new ServerSocket();
server.bind(serverAddr);
server.setSoTimeout(timeout);
}
public void bind(String ip,int port)throws Exception{ //绑定工作
SocketAddress serverAddr=new InetSocketAddress(ip,port);
server.bind(serverAddr);
}
public void accept()throws Exception{ //开始侦听端口
socket= server.accept();
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void setTimeout(int time)throws Exception{
timeout=time;
server.setSoTimeout(timeout);
}
public void begin()throws Exception{ //开始输入输出的变更封装
in=socket.getInputStream();
inRead=new InputStreamReader(in);
read=new BufferedReader(inRead);
out=socket.getOutputStream();
outWrite=new OutputStreamWriter(out);
write=new BufferedWriter(outWrite);
}
public void writeFromKeyboard()throws Exception{ //使用键盘的输入写到套接字连接 :*每次128个字节
String s="";
s=getKeyboard.readLine();
write.write(s);
write.flush();
}
public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度
inFile=new FileInputStream(filePath);
InputStreamReader i=new InputStreamReader(inFile);
BufferedReader r=new BufferedReader(i);
r.skip(off);
char[] data=new char[len];
int num=r.read(data);
r.close();
write.write(data,0,num);
write.flush();
}
public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :*1024个字节
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
return s;
}
public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)
outFile=new FileOutputStream(filePath,on);
OutputStreamWriter o=new OutputStreamWriter(outFile);
BufferedWriter w=new BufferedWriter(o);
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
w.write(s);
w.flush();
}
public void closeLink()throws Exception{ //关闭当前套接字连接
socket.close();
}
public void closeServer()throws Exception{ //关闭套接字服务器
server.close();
}
public String getLocalIP()throws Exception{ //返回本地地址
return localIP;
}
public int getLocalPort()throws Exception{ //返回本地端口
return localPort;
}
public String getFarIP()throws Exception{ //返回远程主机地址
farIP=socket.getInetAddress().getHostAddress();
return farIP;
}
public int getFarPort()throws Exception{ //返回远程主机端口
farPort=socket.getPort();
return farPort;
}
public void setBufferSize(int size){
buffer=new char[size];
}
}
热心网友 时间:2022-04-11 10:06
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储。举例:
BufferedReader bre = null;
OutputStreamWriter pw = null;//定义一个流
try {
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
pw.write(str);
};
bre.close();//关闭流
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
热心网友 时间:2022-04-11 11:41
用流,就OK了. 多大也没关系.