【发布时间】:2015-01-08 20:07:39
【问题描述】:
我需要从网站下载一个 .txt 文件,问题是下载的文件不遵守与原始文件相同的换行。 文件:
Word1
Word2
Word3
文件下载:
Word1Word2Word3
我用这个方法下载(这不是我的):
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream( MegaMethods.FolderPath+"downloadedfile.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
【问题讨论】:
-
@DerGolem 我需要下载文件,然后将每一行保存在不同的数组元素中,我该怎么办?
-
创建json api调用
-
@RobinDijkhof 你能举个例子吗?
-
@LachlanGoodhew-Cook 我不明白,只要给出 txt URL 我可以毫无问题地下载文件,我只需要了解为什么我的 txt 文件没有包装
标签: java android download inputstream txtextcontrol