Android从网站下载txt而不删除换行

时间:2015-01-08 20:07:39

标签: java android download inputstream txtextcontrol

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

1 个答案:

答案 0 :(得分:0)

尝试使用BufferedReader以类似

的方式读取它
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;

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

然后根据需要输出行。我不能在靠近车站的地方进行测试,所以你可能需要做一些摆弄。