Java:写入OutputStream,然后将其用作InputStream(Java: Write to OutputStream and then use it as InputStream)
我想从远程存储中获取一个文件作为InputStream,而不将其保存到本地文件系统。 远程存储为Java API提供了一种方法,该方法接受OutputStream并将文件数据转储到其中。
void dump(OutputStream dest);
我想出的简单方法是创建一个临时文件,将数据转储到其中并重新打开它作为InputStream。 但是这种方法会创建一个临时文件。 有没有代理文件可以轻松实现相同的结果?
I want to get a file from a remote storage as an InputStream without saving it to the local File System. The remote storage provides a Java API with a method that takes an OutputStream and dumps the file data into it.
void dump(OutputStream dest);
The easy way I've come up with is to create a temporary file, dump the data into it and reopen it as an InputStream. But this approach creates a temporary file. Is there an easy way to achieve the same result without a proxy file?
原文:https://stackoverflow.com/questions/32606091
最满意答案
您的数据格式可能是JSON,但您只提供了一部分。 我稍微修改了一下:
{"0": { "key":"valueWithAnyCharInside", "key2":"valueWithAnyCharInside", "key3":"valueWithAnyCharInside" } }
现在你可以解析如下:
string json = ...; //your json goes here var serializer = new JavaScriptSerializer(); var parsed = serializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(json); //printing data parsed["0"].Select(pair => string.Format( "{0} - {1}", pair.Key, pair.Value)) .ToList() .ForEach(Console.WriteLine);
打印:
key - valueWithAnyCharInside key2 - valueWithAnyCharInside key3 - valueWithAnyCharInside
要获得强类型
List<Item>
使用下一个代码List<Item> items = parsed.Select(pair => new Item { Id = int.Parse(pair.Key), Data = pair.Value}) .ToList();
项目是:
class Item { public int Id { get; set; } public Dictionary<string, string> Data {get;set;} }
Your data format is probably a JSON, but you gave only a part of it. I've modified it slightly as:
{"0": { "key":"valueWithAnyCharInside", "key2":"valueWithAnyCharInside", "key3":"valueWithAnyCharInside" } }
now you can parse it as following:
string json = ...; //your json goes here var serializer = new JavaScriptSerializer(); var parsed = serializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(json); //printing data parsed["0"].Select(pair => string.Format( "{0} - {1}", pair.Key, pair.Value)) .ToList() .ForEach(Console.WriteLine);
prints:
key - valueWithAnyCharInside key2 - valueWithAnyCharInside key3 - valueWithAnyCharInside
To get strongly typed
List<Item>
use next codeList<Item> items = parsed.Select(pair => new Item { Id = int.Parse(pair.Key), Data = pair.Value}) .ToList();
Where Item is :
class Item { public int Id { get; set; } public Dictionary<string, string> Data {get;set;} }
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
优雅地解析C#枚举(Elegantly parsing C# Enums)[2023-11-05]
我经常为它做一个通用的帮手: public static T ParseEnum(string value) where T:struct { return (T)Enum.Parse(typeof(T), value); } 您可以将它与Jon Skeet的 Unstrained Melody (或其他任何后处理IL处理器)结合使用,以在枚举上获得适当的类型约束,但这是可选的。 那么你可以像这样使用它: var enumValue = ParseEnum (iUserTy ... -
昂贵的进口之前如何优雅地解析python中的argumens?(how to elegantly parse argumens in python before expensive imports?)[2021-10-31]
你可以有条件地导入,或者在try块中,或者在代码中的任何地方导入。 所以你可以做这样的事情: import cheaplib if __name__ == "__main__": args = parse_args() if expensive_arg in args: import expensivelib do_stuff(args) 或者更加清楚的是,只在将使用它的函数中导入lib。 def expensive_function(): import ... -
在你的情况下,由于数据是字典的项目或值,因此可以使用三元来获取values()或者仅取决于类型的迭代: def iterate(self,object_values): for value in object_values.values() if isinstance(object_values,dict) else object_values: self.do_something(value) 如果你传递一个tuple ,生成器或其他可迭代的元素,它会回落到“标准”迭代。 如果你 ...
-
像这样的东西? >>> s = "abc def, abcdef" >>> w = {"abc": "xxx", "def": "yyy"} >>> def replace(text, words): ... regex = r"\b(?:" + "|".join(re.escape(word) for word in words) + r")\b" ... reobj = re.compile(regex, re.I) ... return reobj.sub(lambda x:w ...
-
首先,获取对td节点的引用。 然后,遍历其子节点并查看它们中的哪些是字符串 : from bs4 import BeautifulSoup mysoup = BeautifulSoup('
hello ') td = mysoup.find('td') print [s for s in td.children if isinstance(s, basestring)] First, get a ref ... -
将文本文件解析为字典(Parse Text File Into Dictionary)[2021-09-18]
这是一个不错的小脑痒。 我认为这段代码可能会指出你正确的方向。 请记住,这会填充Dictionary,因此不会将值转换为int或类似内容。 另外,请原谅一塌糊涂(以及糟糕的命名惯例)。 根据我的思路,这是一个快速的写作。 Dictionary allTheThings = new Dictionary (); public void ReadIt() { // Open the file into a ... -
拥有一个可选值的字典通常不是一个好主意。 字典使用nil的赋值作为您要从字典中删除键/值对的指示。 此外,字典查找返回一个可选值,因此如果您的值是可选的,您将最终得到一个需要两次打开的双重选项。 您可以使用以下事实:分配nil只需指定值即可删除字典条目以构建[String : String]字典。 那些nil不会进入字典,所以你不必删除它们: struct A { var first: String? var second: String? var third: String? } ...
-
您的数据格式可能是JSON,但您只提供了一部分。 我稍微修改了一下: {"0": { "key":"valueWithAnyCharInside", "key2":"valueWithAnyCharInside", "key3":"valueWithAnyCharInside" } } 现在你可以解析如下: string json = ...; //your json goes here var serializer = new Java ...