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

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

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

问题

我正在尝试获取值:

  1. public char getTs() throws IOException {
  2. S3ObjectInputStream ts = null;
  3. int i;
  4. ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURL.toString());
  5. while ((i = ts.read()) != -1) {
  6. c = (char) i;
  7. LOGGER.info("req time {}", c);
  8. }
  9. return c;
  10. }

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

我的输出:
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 :

  1. public char getTs() throws IOException {
  2. S3ObjectInputStream ts = null;
  3. int i;
  4. ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURL.toString());
  5. while ((i = ts.read()) != -1) {
  6. c = (char) i;
  7. LOGGER.info("req time {} ", c);
  8. }
  9. return c;
  10. }

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

  1. My Output :req time 0
  2. req time 1
  3. req time -
  4. req time 0
  5. req time 1.... and so on

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

答案1

得分: 1

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

  1. var reader = new BufferedReader(new InputStreamReader(ts));
  2. 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:

  1. var reader = new BufferedReader(new InputStreamReader(ts));
  2. 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:

确定