Java Android从网站下载txt而不删除包装
我需要从网站下载一个.txt文件,问题是下载的文件与原始文件的换行方式不同。 文件: 下载的文件:Java Android从网站下载txt而不删除包装,java,android,download,inputstream,txtextcontrol,Java,Android,Download,Inputstream,Txtextcontrol,我需要从网站下载一个.txt文件,问题是下载的文件与原始文件的换行方式不同。 文件: 下载的文件: Word1Word2Word3 我用这种方法下载的不是我的: @Override protected String doInBackground(String... f_url) { int count; try { URL url = new URL(f_url[0]); URLConnection con
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);
}
然后根据需要输出行。我不在车站附近,在那里我可以测试这个,所以你可能需要做一些修改。@DerGolem我需要下载文件,然后将每一行保存在不同的数组元素中,那么我能做什么呢?创建一个json api来call@RobinDijkhof你能给我举个例子吗?@LachlanGoodhew Cook我不明白,只要给txt的网址,我可以下载文件没有问题,我只需要了解为什么我的txt文件没有包装
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
responseData.append(line);
}