txt文件,用于保存手机链接

时间:2014-02-26 11:09:33

标签: android

我有一个txt文件的链接。我想通过按下按钮将其保存到手机中。但我没有得到。请帮助!

  void writeFile() throws IOException {
        File myFile = new File("/sdcard/mysdfile.txt");
        myFile.createNewFile();
        HttpURLConnection httpURLConnection = null;
        assert httpURLConnection != null;
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append("my text");
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
    }

仅代替“我的文字”我需要“%D0%96%D0%B8%D0%B7%D0%BD%D1%8C%D0%BE%D0%B4%D0%B8%D0%BD% D0%BE%D0%BA%D0%BE%D0%B3%D0%BE_%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D1%86%D0%B0。%D0%A0%D0%B0%D1%81%D1%81%D0%BA%D0%B0%D0%B7%D1%8B.txt“> HTTP://www.e-reading.bz/ txt.php / 1002963 /%D0%9C%D0%B8%D1%88%D0%B8%D0%BD -_%D0%96%D0%B8%D0%B7%D0%BD%D1 %8C_%D0%BE%D0%B4%D0%B8%D0%BD%D0%BE%D0%BA%D0%BE%D0%B3%D0%BE_%D0%B4%D0%B5%D1%80 %D0%B5%D0%B2%D1%86%D0%B0 ._%D0%A0%D0%B0%D1%81%D1%81%D0%BA%D0%B0%D0%B7%D1%8B .TXT“

1 个答案:

答案 0 :(得分:0)

我的解决方案

 URL url = new URL(my link); //you can write here any link
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        FileOutputStream fos = new FileOutputStream("/sdcard/mysdfile.txt");
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();






<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>