java中用socket时怎么用2进制传输数据流?
在客户端用BufferedReader读非纯文本文件并用PrintWriter传送给socket流再在服务器端用BufferedReader接收时总会有损失..用二进制能...
在客户端用BufferedReader读非纯文本文件并用PrintWriter传送给socket流再在服务器端用BufferedReader接收时总会有损失..用二进制能不能解决?怎么用?
展开
3个回答
2013-08-11
展开全部
对二进制的文件处理的时候,应该使用FileInputStream和FileOutputStream import java.io.*;
public class LinkFile
{
public static void main(String[] args) throws IOException
{
linkBinaryFile("Idea.jpg");
}
private static void linkBinaryFile(String fileName) throws IOException
{
File imageFile = new File(fileName);
if(!imageFile.exists()&&!imageFile.canRead())
{
System.out.println("can not read the image or the image file doesn't exists");
System.exit(1);
}
long length = imageFile.length();
int ch = 0;
System.out.println(length);
byte[] buffer = new byte[(int)length/7];
InputStream image = new FileInputStream(imageFile);
File file = new File("hello.jpg");
if(!file.exists())
{
file.createNewFile();
}
FileOutputStream newFile = new FileOutputStream(file,true);
boolean go = true;
while(go)
{
System.out.println("please select how to read the file:\n"+
"1: read()\n2:read(byte[] buffer)\n3:read(byte[] buffer,int off,int len)\n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if(line.equals("1"))
{
while((ch = image.read())!=-1)
{
System.out.print(ch);
newFile.write(ch);
}
}
else if(line.equals("2"))
{
while((ch = image.read(buffer))!=-1)
{
System.out.println(ch);
newFile.write(buffer);
}
}
else if(line.equals("3"))
{
while((ch = image.read(buffer,10,500))!=-1)
{
System.out.println(ch);
newFile.write(buffer,10,500);
public class LinkFile
{
public static void main(String[] args) throws IOException
{
linkBinaryFile("Idea.jpg");
}
private static void linkBinaryFile(String fileName) throws IOException
{
File imageFile = new File(fileName);
if(!imageFile.exists()&&!imageFile.canRead())
{
System.out.println("can not read the image or the image file doesn't exists");
System.exit(1);
}
long length = imageFile.length();
int ch = 0;
System.out.println(length);
byte[] buffer = new byte[(int)length/7];
InputStream image = new FileInputStream(imageFile);
File file = new File("hello.jpg");
if(!file.exists())
{
file.createNewFile();
}
FileOutputStream newFile = new FileOutputStream(file,true);
boolean go = true;
while(go)
{
System.out.println("please select how to read the file:\n"+
"1: read()\n2:read(byte[] buffer)\n3:read(byte[] buffer,int off,int len)\n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if(line.equals("1"))
{
while((ch = image.read())!=-1)
{
System.out.print(ch);
newFile.write(ch);
}
}
else if(line.equals("2"))
{
while((ch = image.read(buffer))!=-1)
{
System.out.println(ch);
newFile.write(buffer);
}
}
else if(line.equals("3"))
{
while((ch = image.read(buffer,10,500))!=-1)
{
System.out.println(ch);
newFile.write(buffer,10,500);
2013-08-11
展开全部
Socket socket;OutputStream sout=socket.getOutputSteam();InputStream sin=socket.getInputStream(); client:FileInputStream input=new FileInputStream(" 文件");byte[] buf=new byte[1024];for(int size=0;size!=-1;size=input.read(buf)){ sout.write(buf,0,size); sout.flush();}input.close();sout.close(); server:StringBuffer sb=new StringBuffer();byte[] buf=new byte[1024];for(int size=0;size!=-1;size=sin.read(buf)){ sb.append(new String(buf,0,size));} sin.close(); System.out.println("显示接受的文本信息 :"+sb.toString());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-11
展开全部
用管道套管道 DataOutputStream 再 Bufffer一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询