标签 io strem 下的文章

TCP通信实例


文件传输,用socket

android中也使用java的socket模型,即“插座”.

首先创建一个线程类复写run方法,ServerSocket是用于创建绑定到特定端口的服务器套接字。参数预定好端口。accept方法 侦听并接受到此套接字的连接。如果服务端口没有收到信息,就阻塞在这里不动,等待。

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyThread extends Thread{

    public void run() {
        // TODO Auto-generated method stub
        
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(4567);
            Socket socket=serverSocket.accept();
            InputStream inputStream=socket.getInputStream();
            byte [] buffer=new byte[1024*4];
            int temp=0;
            while((temp= inputStream.read(buffer))!=-1){
                String s=new String(buffer,0,temp);
               System.out.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

然后客户端:创建一个socket对象,并指定地址,门牌号。然后通过FileInputStream读取本地的一个文本文件。socket.getOutputStream()返回此套接字的输出流。(其实就是创建个输出通道吧==)。接下来就是IO操作啦。

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class TCPClient {
public static void main(String[] args) {
    try {
        Socket socket=new Socket("localhost",4567);
        InputStream inputStream=new FileInputStream("D://wu.txt");
        OutputStream outputStream=socket.getOutputStream();
        byte [] buffer=new byte[1024*4];
        int temp=0;
        while((temp=inputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,temp);
        }
        
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}
}

IO流例子编写




无标题

后缀是Stream是字节流,而后缀是ReaderWriter是字符流。
Jdk提供的流继承了四大类:InputStream(字节输入流)OutputStream(字节输出流),Reader(字符输入流),Writer(字符输出流)。

按字节来读取文件
  1. public class ReadFromFile {
  2.     /**
  3.      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  4.      */
  5.     public static void readFileByBytes(String fileName) {
  6.         File file = new File(fileName);
  7.         InputStream in = null;
  8.         try {
  9.             System.out.println("以字节为单位读取文件内容,一次读一个字节:");
  10.             // 一次读一个字节
  11.             in = new FileInputStream(file);
  12.             int tempbyte;
  13.             while ((tempbyte = in.read()) != -1) {
  14.                 System.out.print(tempbyte);
  15.             }
  16.             in.close();
  17.         } catch (IOException e) {
  18.             e.printStackTrace();
  19.             return;
  20.         }



按行来读取文件
  1. /**
  2.      * 以行为单位读取文件,常用于读面向行的格式化文件
  3.      */
  4.     public static void readFileByLines(String fileName) {
  5.         File file = new File(fileName);
  6.         BufferedReader reader = null;
  7.         try {
  8.             System.out.println("以行为单位读取文件内容,一次读一整行:");
  9.             reader = new BufferedReader(new FileReader(file));
  10.             String tempString = null;
  11.             int line = 1;
  12.             // 一次读入一行,直到读入null为文件结束
  13.             while ((tempString = reader.readLine()) != null) {
  14.                 // 显示行号
  15.                 System.out.println("line " + line + ": " + tempString);
  16.                 line++;
  17.             }
  18.             reader.close();
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.         } finally {
  22.             if (reader != null) {
  23.                 try {
  24.                     reader.close();
  25.                 } catch (IOException e1) {
  26.                 }
  27.             }
  28.         }
  29.     }



一个文件读到另一个文件
  1. public static void FiletoAnotherFile(String fileName){
  2. File file=new File(fileName);
  3. BufferedReader reader=null;
  4. BufferedWriter writer=null;
  5. try {
  6. writer=new BufferedWriter(new FileWriter("D://wu2.txt"));
  7. } catch (IOException e) {
  8. // TODO Auto-generated catch block
  9. e.printStackTrace();
  10. }
  11. try {
  12. reader=new BufferedReader(new FileReader(file));
  13. String tempString=null;
  14. while((tempString=reader.readLine())!=null){
  15. writer.append(tempString);
  16. writer.newLine();
  17. writer.flush();
  18. }
  19. } catch (FileNotFoundException e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }finally{
  26. try {
  27. reader.close();
  28. writer.close();
  29. System.out.println("write done");
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }






shijiebei 365bet manbetx 188bet xinshui caipiao 95zz tongbaoyule beplay 88bifa 18luck betway bwin hg0088 aomenjinshayulecheng ca88 shenbotaiyangcheng vwin w88 weide