Android创建txt文件并写入

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31471291/article/details/77048459


有的时候后台返回的json数据太多,在控制台log日志打印时,一次性输出不全,网上有介绍使用log工具类,这里不再赘述。

我们用到另一种方法,把请求的得到的json数据写入txt文件:

                        

                          String sdCardDir =Environment.getExternalStorageDirectory().getAbsolutePath();
                           File saveFile = new File(sdCardDir, "aaaa.txt");
                           FileOutputStream outStream = new FileOutputStream(saveFile);
                           outStream.write("json
数据".getBytes());
                           outStream.close();
  

或者:

   public static void getString(String str) {

       String filePath = null;

       boolean hasSDCard =Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

       if (hasSDCard) {

            filePath =Environment.getExternalStorageDirectory().toString() + File.separator +"hello.txt";

       } else

            filePath =Environment.getDownloadCacheDirectory().toString() + File.separator +"hello.txt";

 

       try {

            File file = new File(filePath);

            if (!file.exists()) {

                File dir = newFile(file.getParent());

                dir.mkdirs();

                file.createNewFile();

            }

            FileOutputStream outStream = newFileOutputStream(file);

            outStream.write(str.getBytes());

            outStream.close();

       } catch (Exception e) {

            e.printStackTrace();

       }

   }

展开阅读全文

没有更多推荐了,返回首页