我目前正在使用InpuStream从我的服务器获取JSON响应.
我需要做两件事:
在逐个使用这两种方法时,这完全没有问题.
使用GSON进行解析:
Gson gson = new Gson();
Reader reader = new InputStreamReader (myInputStream);
Result result = gson.FrmJson(reader, Result.class)
Run Code Online (Sandbox Code Playgroud)
并将复制到SDCard
FileOutputStream f (...) f.write (buffer)
Run Code Online (Sandbox Code Playgroud)
它们都经过了测试.
问题是一旦解析完成,我想写入SDCard并且它会中断.我知道我的InputStream已关闭,这就是问题所在.
这里有一些接近我的问题:如何缓存InputStream以供多次使用
有没有办法改进该解决方案并提供我们可以使用的东西?
我需要能够重复使用java.io.InputStream多次,并且我认为以下代码可以工作,但它只能在第一次使用.
public class Clazz
{
private java.io.InputStream dbInputStream, firstDBInputStream;
private ArrayTable db;
public Clazz(java.io.InputStream defDB)
{
this.firstDBInputStream = defDB;
this.dbInputStream = defDB;
if (db == null)
throw new java.io.FileNotFoundException("Could not find the database at " + db);
if (dbInputStream.markSupported())
dbInputStream.mark(Integer.MAX_VALUE);
loadDatabaseToArrayTable();
}
public final void loadDatabaseToArrayTable() throws java.io.IOException
{
this.dbInputStream = firstDBInputStream;
if (dbInputStream.markSupported())
dbInputStream.reset();
java.util.Scanner fileScanner = new java.util.Scanner(dbInputStream);
String CSV = "";
for (int i = 0; fileScanner.hasNextLine(); i++)
CSV += fileScanner.nextLine() + "\n";
db …Run Code Online (Sandbox Code Playgroud) 参考stackoverflow问题,据说InputStream可以使用或通过使用来多次读取mark()和reset()提供.InputStreamPushbackInputStream
在所有这些情况下,流的内容存储在字节数组中(即,文件的原始内容存储在主存储器中)并重复使用多次.
当文件大小超过内存大小时会发生什么?我认为这可能会铺平道路OutOfMemoryException.
有没有更好的方法多次读取流内容而不在本地存储流内容(即;在主存储器中)?
知道这个,请帮帮我.提前致谢.
我正在用 Java 编写一个 Android 应用程序,该应用程序使用 Android 配件 API 与 USB 配件进行通信。
在谷歌文档并没有给出实际读取和写入数据,只是如何打开附件进行通信的例子。
它还说明了两件事:
我实现了这样的阅读:
/** boiler plate from example docs: **/
fileDescriptor = usbManager.openAccessory(accessory);
if (fileDescriptor != null) {
FileDescriptor fd = fileDescriptor.getFileDescriptor();
inputStream = new FileInputStream(fd);
readBuffer = new byte[16384];
// ... some more code that is not relevant for reading data ... //
}
/** my code in separate thread: **/
int read;
while ((read = …Run Code Online (Sandbox Code Playgroud) 我只是用
IOUtils.copy(myInputStream, myOutputStream);
Run Code Online (Sandbox Code Playgroud)
我在调用IOUtils.copy之前看到输入流可以读取而不是之后.
flux.available()
(int) 1368181 (before)
(int) 0 (after)
Run Code Online (Sandbox Code Playgroud)
我看到了这方面的一些解释后,我看我可以复制 bytes从我InputStream的ByteArrayInputStream,然后用mark(0)和read(),以多次读取的输入流.
这是得到的代码(正在运行).我发现这段代码非常冗长,如果有更好的解决方案,我想要这样做.
ByteArrayInputStream fluxResetable = new ByteArrayInputStream(IOUtils.toByteArray(myInputStream));
fluxResetable.mark(0);
IOUtils.copy(fluxResetable, myOutputStream);
fluxResetable.reset();
Run Code Online (Sandbox Code Playgroud) 我有一个InputStream作为参数,当我第一次读取它时它工作得很好,但是,读取相同的InputStream不起作用。我就是无法mark()上班reset()。有人知道如何重置这个吗?我正在读取 .txt 文件。该文件包含不会重新出现的敌方对象的生成值,因为输入流标记(?)位于末尾,我猜?
readTxt(InputStream resource){
//resource is a .txt as ResourceStream
arrayList = new BufferedReader(new InputStreamReader(resource,
StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)