About the use of Java streaming--Files and sockets

Source: Internet
Author: User

Personally, the stream is the medium in which data is exchanged between different devices in Java, such as the transmission of data in the socket, and the read and write operations of the files, all of which are implemented through streams.

Type of flow

From the processing data type, the stream is mainly divided into character stream (InputStream, outputstream) and byte stream (Reader, Writer), and from the functional point of view, it is mainly divided into input stream and output stream.

Read and write files
  1. File file =newFile(".\\test.txt");
  2. //参数true:表示在文件后边追加内容
  3. FileOutputStream fileOutputStream =newFileOutputStream(file,true);
  4. FileInputStream fileInputStream =newFileInputStream(file);
  5. //装饰者模式,使用带缓冲的输入流
  6. BufferedInputStream bufferedInputStream =newBufferedInputStream(fileInputStream);
  7. //使用byte,字节流读取文件中的内容
  8. byte[]b =newbyte[100];
  9. bufferedInputStream.read(b);
  10. Stringstring=newString(b,"UTF-8");
  11. System.out.println(string);
  12. //使用字符流读取文件中的内容
  13. BufferedReader bufferedReader =newBufferedReader(inputStreamReader);
  14. System.out.println(bufferedReader.readLine());
  15. //写入文件
  16. OutputStreamWriter outputStreamWriter =newOutputStreamWriter(fileOutputStream);
  17. outputStreamWriter.write("jjjjjjjjjjjjjjjjjjjd我们哈哈哈哈哈");
  18. BufferedOutputStream bufferedOutputStream =newBufferedOutputStream(fileOutputStream);
  19. bufferedOutputStream.flush();
  20. Stringstring="我们进顶顶顶顶顶顶顶顶顶大大大";
  21. byte[]b =string.getBytes("UTF8");
  22. bufferedOutputStream.write(b);
  23. bufferedOutputStream.close();
Socket using Server
  1. publicclassTcpServer{
  2. publicstaticvoid main(String[] args)throwsIOException{
  3. // TODO Auto-generated method stub
  4. String getString;
  5. String sendString;
  6. ServerSocket socket =newServerSocket(6789);
  7. while(true){
  8. Socket connection = socket.accept();
  9. BufferedReader reader =newBufferedReader(newInputStreamReader(connection.getInputStream()));
  10. DataOutputStream outputStream =newDataOutputStream(connection.getOutputStream());
  11. getString = reader.readLine();
  12. sendString = getString.toUpperCase()+"\n";
  13. outputStream.writeBytes(sendString);
  14. }
  15. }
  16. }
Client
  1. publicclassTcpClient{
  2. publicstaticvoid main(String[] args)throwsUnknownHostException,IOException{
  3. String sendString;
  4. String getString;
  5. System.out.println("start connection");
  6. Socket socket =newSocket("127.0.0.1",6789);
  7. System.out.println("connect successfully");
  8. DataOutputStream outputStream =newDataOutputStream(socket.getOutputStream());
  9. BufferedReader fromServer =newBufferedReader(newInputStreamReader(socket.getInputStream()));
  10. sendString ="send to server from tcpclient";
  11. outputStream.writeBytes(sendString +"\n");
  12. getString = fromServer.readLine();
  13. InputStreamin= socket.getInputStream();
  14. byte[] b =newbyte[100];
  15. in.read(b);
  16. Stringstring=newString(b,"UTF-8");
  17. System.out.println(b);
  18. socket.close();
  19. }
  20. }



From for notes (Wiz)



About the use of Java streaming--Files and sockets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.