gpt4 book ai didi

java - Android从网站下载txt而不去除包装

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:08 26 4
gpt4 key购买 nike

我需要从网站下载 .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;
}

最佳答案

尝试使用 BufferedReader 来读取它,例如

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;

StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
responseData.append(line);
}

然后根据需要输出行。我附近没有可以测试这个的车站,所以你可能需要做一些摆弄。

关于java - Android从网站下载txt而不去除包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com