代码之家  ›  专栏  ›  技术社区  ›  smatthewenglish

Java从IOUtils的InputStrem(特别是copyBytes)动态写入字符串数组

  •  -1
  • smatthewenglish  · 技术社区  · 7 年前

    the documentation from copyBytes (共) IOUtils

    复制字节:

    public static void copyBytes(InputStream in,
                                 OutputStream out,
                                 int buffSize,
                                 boolean close) throws IOException
    

    从一个流复制到另一个流。

    参数:

    in - InputStrem to read from
    out - OutputStream to write to
    buffSize - the size of the buffer
    close - whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause.
    

    抛出:

    IOException
    

    因此,考虑到这些信息,我得到了这样的数据结构:

    List<String> inputLinesObject = IOUtils.readLines(in, "UTF-8");
    

    复制字节 方法

    然而,以下是我调用 复制字节 方法:

    IOUtils.copyBytes(in, inputLinesObject, 4096, false);
    

    inputLinesObject ,我想把我的可扩展数组列表放在这里,它可以收集数据并将其转换为字符串格式-但我现在这样做的方式并不正确-我不知何故被卡住了-我看不到以字符串数组列表格式收集数据的正确方法( inputSteam 这是否意味着 byteArray ?).

    HDFS and-应该(尽管目前没有)将它们输出到字符串数组列表中-最终将使用 System.out.println .

    // this concatenates output to terminal from hdfs 
    public static void main(String[] args) throws IOException {
    
        // supply this as input
        String uri = args[0];
    
        // reading in from hdfs
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        FSDataInputStream in = null;
    
    
        // create arraylist for hdfs file to flow into
        //List<String> inputLinesObject = new ArrayList<String>();
        List<String> inputLinesObject = IOUtils.readLines(in, "UTF-8");
    
    
        // TODO: how to make this go to a file rather than to the System.out?
        try
        {
            in = fs.open(new Path(uri));
            // The way:
            IOUtils.copyBytes(in, inputLinesObject, 4096, false);
    
        }
        finally{
            IOUtils.closeStream(in);
        }
    
    1 回复  |  直到 7 年前
        1
  •  -1
  •   smatthewenglish    7 年前

    使用 ByteArrayOutputStream ,请参见此处:

        // supply this as input
        String uri = args[0];
    
        // reading in from hdfs
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        FSDataInputStream in = null;
    
    
        // create arraylist for hdfs file to flow into
        List<String> list = new ArrayList<String>(); // Initialize List
    
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream os = new DataOutputStream(baos);
    
    
        try
        {
    
            in = fs.open(new Path(uri));
            // The way:
            IOUtils.copyBytes(in, os, 4096, false);
    
        }
        finally{
            IOUtils.closeStream(in);
        }
    
        byte[] data = baos.toByteArray();
        String dataAsString = new String(data, "UTF-8"); // or whatever encoding 
    
        System.out.println(dataAsString);