如何在Java中从S3输入流中获取值

huangapple go评论80阅读模式
英文:

How to get the value from the s3 input stream in java

问题

我正在尝试获取值:

public char getTs() throws IOException {
    S3ObjectInputStream ts = null;
    int i;
    ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURL.toString());
    while ((i = ts.read()) != -1) {
        c = (char) i;
        LOGGER.info("req time {}", c);
    }
    return c;
}

我得到的值是以位为单位的,如何一次获取整个值?
是否适合使用数组?

我的输出:
req time 0
req time 1
req time -
req time 0
req time 1....等等

预期输出:
req time 01-01-2000 03:10:10

英文:

I am trying to get value here :

  public char getTs() throws IOException {
   S3ObjectInputStream ts = null;
    int i;
    ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURL.toString());
    while ((i = ts.read()) != -1) {
        c = (char) i;
        LOGGER.info("req time {} ", c);
    }
    return c;
}

The value I am getting is in bits, how to get the whole value at once?
Is it suitable to use an array?

My Output :req time 0
           req time 1
           req time -
           req time 0
           req time 1.... and so on

My expected output is :req time 01-01-2000 03:10:10

答案1

得分: 1

你可以将 S3ObjectInputStream 包装在 InputStreamReader 中,然后将 InputStreamReader 包装在 BufferedInputStream 中。这样,你可以逐行读取对象:

var reader = new BufferedReader(new InputStreamReader(ts));
var line = reader.readLine();

此外,还可以查看 Apache Commons IO,该库提供了额外方便的流、读取器和工具。

英文:

You could wrap the S3ObjectInputStream within an InputStreamReader and the InputStreamReader within a BufferedInputStream. That way you can read the object line by line:

var reader = new BufferedReader(new InputStreamReader(ts));
var line = reader.readLine();

Also check out Apache Commons IO which provide additional convenient streams and readers and utilities.

huangapple
  • 本文由 发表于 2020年10月8日 01:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定