如果我用这种方式在PDF周围编写代码,则说它无效并且无法打开,但是如果我将它们交换并在A之前加上B,则可以正常工作吗?为什么会这样,我必须怎么做才能使其正常工作? TIA
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//A
String line = "";
StringBuilder builder = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
//B
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File outputFile = new File(directory, "goo.pdf");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int len1 = 0;//init length
while (true) {
try {
if (!((len1 = in.read(buffer)) != -1)) break;
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.write(buffer, 0, len1);
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
InputStream只能读取一次。
在'A'中,读取流并将内容放入StringBuilder。
在“ B”中,读取流(现在为空)并将其通过管道传输到文件。
首先使用A,输出文件将始终为空。
只需删除A,因为这里没有为您做任何事情。