Java 从 IOUtils 的 InputStrem 动态写入字符串数组,特别是 copyBytes
Java writing dynamically to a String Array from InputStrem of a IOUtils, specifically copyBytes
读完 the documentation from copyBytes
(属于 IOUtils
),我们可以在这里看到它的参数:
copyBytes:
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");
^我希望这是一个可扩展的字符串数组列表,我可以用我正在使用 copyBytes
方法读取的文件中的数据填充它。
但是,这是我调用 copyBytes
方法时使用的代码:
IOUtils.copyBytes(in, inputLinesObject, 4096, false);
你看到 inputLinesObject
的地方就是我想放置我的可扩展数组列表的地方,它可以收集该数据并将其转换为字符串格式 - 但我现在这样做的方式是不是正确的方法 - 我不知何故被困住了 - 我看不到以字符串数组列表的格式收集数据的正确方法(此时它是什么?因为它来自an inputSteam
是否使它成为 byteArray
?)。
这是完整的程序 - 它从 HDFS
中读取文件并且 - 应该(虽然目前不是)将它们输出到字符串数组列表 - 最后将使用 [= 登录到控制台25=].
// 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);
}
使用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);
读完 the documentation from copyBytes
(属于 IOUtils
),我们可以在这里看到它的参数:
copyBytes:
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");
^我希望这是一个可扩展的字符串数组列表,我可以用我正在使用 copyBytes
方法读取的文件中的数据填充它。
但是,这是我调用 copyBytes
方法时使用的代码:
IOUtils.copyBytes(in, inputLinesObject, 4096, false);
你看到 inputLinesObject
的地方就是我想放置我的可扩展数组列表的地方,它可以收集该数据并将其转换为字符串格式 - 但我现在这样做的方式是不是正确的方法 - 我不知何故被困住了 - 我看不到以字符串数组列表的格式收集数据的正确方法(此时它是什么?因为它来自an inputSteam
是否使它成为 byteArray
?)。
这是完整的程序 - 它从 HDFS
中读取文件并且 - 应该(虽然目前不是)将它们输出到字符串数组列表 - 最后将使用 [= 登录到控制台25=].
// 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);
}
使用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);