理解 getInputStream 和 getOutputStream [英] Understanding getInputStream and getOutputStream

查看:31
本文介绍了理解 getInputStream 和 getOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码

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] ) + "
";
        byte buf[] = str.getBytes();
        *out.write(buf);
        System.out.print("hey baby");
        while ((c=in.read()) != -1)
        {
            System.out.print((char) c);
        }
        s.close();
    }
}

我已经标记了我理解有问题的语句.我不明白什么 OutputStream 对象 out 在被分配 s.getOutputStream() 时会持有什么以及将 buf 传递给 的需要是什么?code>out by out.write(buf).

I have marked the statements that I have problems understanding. I do not understand what OutputStream object out will hold when it is assigned s.getOutputStream() and what is the need of passing buf to out by out.write(buf).

我已经使用文件学习了输入和输出流,但我不理解 getinputstreamoutputstreams.我在谷歌上搜索过它,在 SO 上以及从许多不同的书籍和 oracle 文档中阅读过它.请详细讨论.

I have learned Input and Output Streams using files but I do not understand getinputstream and outputstreams. I have googled it, read it here on SO as well as from many different books and from oracle documents as well. Please discuss it in detail.

我知道如何从文件中读取以及如何写入文件,但在这里我不明白传递仅包含一个字符串的 buf 数组的需要是什么.我想问的是,当有套接字的输入流时,为什么我们不能直接从中读取?究竟什么是套接字 inputstreamoutputstream?

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 buf array which holds only a string. What I mean to ask is that when in has the input stream of the socket why can't we directly just read from it? What exactly is a socket inputstream and outputstream?

我在这里找到了一些东西,这里是链接https://stackoverflow.com/questions/12715321/java-networking-explain-inputstream-and-outputstream-in-socket",这里是 DNA 的回答

I found something here on SO here is the link "https://stackoverflow.com/questions/12715321/java-networking-explain-inputstream-and-outputstream-in-socket", here an answer by DNA says

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

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

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

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?

推荐答案

首先你需要了解什么是STREAM

first thing you need to understand is what is STREAM

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

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.

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

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

 we have byte stream and character stream.

classes we have in Input Stream and output stream 

顾名思义,简单来说输入流用于输入数据,输出流用于输出数据

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

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

Java byte streams are 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位字节的输入和输出,而Java字符流用于执行16位Unicode的输入和输出.虽然有很多与字符流相关的类,但最常用的类是 FileReader 和 FileWriter. 虽然内部 FileReader 使用 FileInputStream 和 FileWriter 使用 FileOutputStream 但这里的主要区别是 FileReader 一次读取两个字节而 FileWriter 写入两个字节一次.

Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are 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.

供参考

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

java DataOutputStream getOutputStream() getInputStream()

getInputStream 和 getOutputStream 示例

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

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

这篇关于理解 getInputStream 和 getOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆