Java 理解 getInputStream 和 getOutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22563986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:30:59  来源:igfitidea点击:

Understanding getInputStream and getOutputStream

javastream

提问by Junaid Shirwani

Here is a code

这是一个代码

import java.io.*;
import java.net.*;
public class Whois
{
    public static void main(String[] args)
        throws Exception
    {
        // TODO Auto-generated method stub
        int c;
        Socket s = new Socket("whois.internic.net",43);
        *InputStream in = s.getInputStream();
        *OutputStream out = s.getOutputStream();
        String str = (args.length == 0 ? "osborne.com" : args[0] ) + "\n";
        byte buf[] = str.getBytes();
        *out.write(buf);
        System.out.print("hey baby");
        while ((c=in.read()) != -1)
        {
            System.out.print((char) c);
        }
        s.close();
    }
}

I have marked the statements that i have problem understanding.I do not understand what OutputStream object outwill hold when it is assigned s.getOutputStream()and what is the need of passing bufto outby out.write(buf).

我已经标记了我理解有问题的语句。我不明白 OutputStream 对象out在分配时将保存s.getOutputStream()什么以及传递bufoutby的需要是什么out.write(buf)

I have learned Input and output Streams using files but i do not understand getinputstreamand outputstreams.I have googled it ,read it here on SO as well as from many different book and from oracle documents as well . please discuss it in detail .

我一直在使用的文件学到输入和输出流,但我不明白getinputstreamoutputstreams。我用Google搜索它,阅读这里的SO以及来自许多不同的书,从甲骨文文件以及。请详细讨论。

I know how to read from files and how to write to them.but here i do not understand what is the need of passing bufarray which holds only a string.what i mean to ask is that when in has the input stream of the socket why cant we directly just read from it ? What exactly is a sockets inputstreamand outputstream?

我知道如何从文件中读取以及如何写入它们。但在这里我不明白传递buf只包含一个字符串的数组有什么需要。我的意思是问什么时候有套接字的输入流为什么我们不能直接从中读取吗?到底什么是套接字inputstreamoutputstream

I found something here on SO here is the link "Java Networking: Explain InputStream and OutputStream in Socket" ,here an answer by DNA says

我在 SO 上找到了链接“ Java Networking: E​​xplain InputStream and OutputStream in Socket”,这里是 DNA 的回答

In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."

在 Java 中,要通过套接字发送数据,您需要从中获取一个 OutputStream (1),然后写入 OutputStream(您输出一些数据)。”

This is confusing me , when outputStream is used to send data via socket what was the need of out.write(buf) why do we need to send "google.com" to outputStream?

这让我很困惑,当 outputStream 用于通过套接字发送数据时,out.write(buf) 需要什么,为什么我们需要将“google.com”发送到 outputStream?

采纳答案by Divya

first thing you need to understand is what is STREAM

您需要了解的第一件事是什么是STREAM

A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

流可以定义为数据序列。InputStream 用于从源读取数据,OutputStream 用于将数据写入目标。

****Next is type of streams****

****接下来是流的类型****

 we have byte stream and character stream.

enter image description here

在此处输入图片说明

classes we have in Input Stream and output stream 

enter image description here

在此处输入图片说明

as the name suggests in simple termsinput stream is used to input the data and output stream is used to output the data

正如其名称暗示在简单的术语输入流被用于输入数据和输出流用于输出的数据

Java byte streamsare used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. also

Java字节流用于执行 8 位字节的输入和输出。虽然有很多与字节流相关的类,但最常用的类是 FileInputStream 和 FileOutputStream。还

Java Byte streamsare used to perform input and output of 8-bit bytes, where as Java Character streamsare used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

Java Byte 流用于执行 8 位字节的输入和输出,而 Java Character 流用于执行 16 位 unicode 的输入和输出。虽然有很多与字符流相关的类,但最常用的类是 FileReader 和 FileWriter。 虽然内部 FileReader 使用 FileInputStream 和 FileWriter 使用 FileOutputStream 但这里的主要区别是 FileReader 一次读取两个字节,FileWriter 一次写入两个字节一个时间。

For reference

以供参考

  1. What is InputStream & Output Stream? Why and when do we use them?

  2. java DataOutputStream getOutputStream() getInputStream()

  1. 什么是输入流和输出流?我们为什么以及何时使用它们?

  2. java DataOutputStream getOutputStream() getInputStream()

Example for getInputStream and getOutputStream

getInputStream 和 getOutputStream 的示例

  1. http://zerioh.tripod.com/ressources/sockets.html
  1. http://zerioh.tripod.com/ressources/sockets.html

New Linkhttp://docs.oracle.com/javase/tutorial/essential/io/buffers.html

新链接http://docs.oracle.com/javase/tutorial/essential/io/buffers.html

回答by Smit Shilu

Here OutputStreamis used to send data to other side in socket whenever you write out.write(buf)it will send buffer data in socket.

这里OutputStream用于将数据发送到套接字中的另一端,每当您编写out.write(buf)它时,它都会在套接字中发送缓冲区数据。

InputStreamis used to receive data from socket.

InputStream用于从套接字接收数据。

回答by David Koelle

InputStream inand OutputStream outwill hold references to two types of streams that you can either read data from or write data to. Don't expect them to hold values from the stream itself - instead, they hold the ability to work with the stream. When you create these objects, you're not sending/receiving any data - you're just getting the object that you can use to send/receive data.

InputStream in并且OutputStream out将保存对两种类型的流的引用,您可以从中读取数据或向其写入数据。不要指望它们保存来自流本身的值——相反,它们拥有处理流的能力。当您创建这些对象时,您不会发送/接收任何数据 - 您只是获取可用于发送/接收数据的对象。

out.write(buf)is sending the contents of bufover the Socket so that any readers of the socket (in your case, in) can receive that data. Whatever data is sent to outwill be seen on the other side of the Socket by an InputStream.

out.write(buf)正在buf通过 Socket发送 的内容,以便套接字的任何读取器(在您的情况下,in)都可以接收该数据。无论发送到什么数据outInputStream.