所以,我正在将文件数据提供给需要a的API,Reader我想要一种报告进度的方法.
看起来应该直截了当地编写一个FilterInputStream包装的实现,FileInputStream跟踪读取的字节数与总文件大小,并触发一些事件(或调用某些update()方法)来报告小数进度.
(或者,它可以报告绝对字节读取,而其他人可以进行数学计算 - 在其他流式传输情况下可能更常用.)
我知道我以前见过这个,我甚至可能以前做过,但我找不到代码,而且我很懒.有没有人得到它?或者有人可以提出更好的方法吗?
一年(有点)之后......
我在下面根据Adamski的答案实施了一个解决方案,并且它有效,但经过几个月的使用后我不推荐它.当您有大量更新时,解雇/处理不必要的进度事件会产生巨大的成本.基本的计数机制很好,但是对于那些关心进展调查的人来说要好得多,而不是把它推到他们身上.
(如果你知道总的大小,你可以尝试每隔1%的变化或者其他任何事情来发射一个事件,但这不值得麻烦.通常,你没有.)
我想创建一个仅限于文件中某个字节范围的InputStream,例如从0到100的字节.因此客户端代码应该在达到第100个字节时看到EOF.
我需要能够重复使用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) 我必须读取一个dict.txt文件,其中包含一行for line并将这些字符串添加到arraylist中.
我试过这个:
public ArrayList<String> myDict = new ArrayList<String>();
InputStream is = (getResources().openRawResource(R.raw.dict));
BufferedReader r = new BufferedReader(new InputStreamReader(is));
try {
while (r.readLine() != null) {
myDict.add(r.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但有些不对劲......
这是代码,但有错误:
bin = new ByteArrayInputStream(socket.getInputStream());
Run Code Online (Sandbox Code Playgroud)
是否可以从套接字接收byte[]使用ByteArrayInputStream?
我收到了一个错误 io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence
解决方案是以UTF-8读写文件.
我的代码是:
InputStream input = null;
OutputStream output = null;
OutputStreamWriter bufferedWriter = new OutputStreamWriter( output, "UTF8");
input = new URL(url).openStream();
output = new FileOutputStream("DirectionResponse.xml");
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
FileWriter fstream = new FileWriter("ppre_DirectionResponse.xml");
BufferedWriter out = new BufferedWriter(fstream);
Run Code Online (Sandbox Code Playgroud)
我正在读一个url并将其写入文件DirectionResponse.xml.然后读取DirectionResponse.xml并将其写为*ppre_DirecionResponse.xml*进行处理.
如何更改此选项以便以UTF-8完成读写?
我正在处理二进制流,需要有效地跳过我不感兴趣的一系列数据,以及一些将被处理的数据.
InputStream.skip(long) 在保证方面做得不多:
跳过并丢弃此输入流中的n个字节的数据.由于各种原因,跳过方法可能最终跳过一些较小数量的字节,可能为0.这可能是由许多条件中的任何一个引起的; 在跳过n个字节之前到达文件末尾只有一种可能性.返回跳过的实际字节数.
我需要知道发生了两件事之一:
很简单.但是,在此描述中提供的宽大意味着,例如,BufferedInputStream可以跳过几个字节并返回.当然,它告诉我它只是跳过了那几个,但不清楚为什么.
所以我的问题是:你是否能够InputStream.skip(long)以这样的方式使用你知道什么时候流结束或跳过成功?
我在应用程序中使用Volley库.
在onresponse listener中我需要InputStream作为响应
我怎么得到它?
我有这个InputStream:
InputStream inputStream = new ByteArrayInputStream(myString.getBytes(StandardCharsets.UTF_8));
Run Code Online (Sandbox Code Playgroud)
如何将其转换为ServletInputStream?
我试过了:
ServletInputStream servletInputStream = (ServletInputStream) inputStream;
Run Code Online (Sandbox Code Playgroud)
但是不行.
编辑:
我的方法是这样的:
private static class LowerCaseRequest extends HttpServletRequestWrapper {
public LowerCaseRequest(final HttpServletRequest request) throws IOException, ServletException {
super(request);
}
@Override
public ServletInputStream getInputStream() throws IOException {
ServletInputStream servletInputStream;
StringBuilder jb = new StringBuilder();
String line;
String toLowerCase = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(super.getInputStream()));
while ((line = reader.readLine()) != null) {
toLowerCase = jb.append(line).toString().toLowerCase();
}
InputStream inputStream = new ByteArrayInputStream(toLowerCase.getBytes(StandardCharsets.UTF_8));
servletInputStream = (ServletInputStream) inputStream;
return …Run Code Online (Sandbox Code Playgroud) 我有网络服务URL,它工作正常.它提供了JSON数据.当我使用HttpURLConnection时InputStream,我收到一个错误.喜欢
java.io.IOException:Connection上的意外流结束{comenius-api.sabacloud.com:443,proxy = DIRECT hostAddress = 12.130.57.1 cipherSuite = TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol = http/1.1}(recycle count = 0)
try{
URL url = new URL("https://comenius-api.sabacloud.com/v1/people/username="+username+":(internalWorkHistory)?type=internal&SabaCertificate="+ certificate);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
InputStream ist = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ist));
while((singleLine = reader.readLine())!= null){
data = data + singleLine;
Log.e("Response", data);
}
}catch(Exception e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题?