Advertisement
lv1codeman

Android_create file

Jan 2nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. //方法一
  2. File file = new File(context.getFilesDir(), filename);
  3.  
  4. //方法二
  5. String filename = "myfile";
  6. String string = "Hello world!";
  7. FileOutputStream outputStream;
  8.  
  9. try {
  10.   outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  11.   outputStream.write(string.getBytes());
  12.   outputStream.close();
  13. } catch (Exception e) {
  14.   e.printStackTrace();
  15. }
  16.  
  17. //方法三:cache file
  18. public File getTempFile(Context context, String url) {
  19.     File file;
  20.     try {
  21.         String fileName = Uri.parse(url).getLastPathSegment();
  22.         file = File.createTempFile(fileName, null, context.getCacheDir());
  23.     } catch (IOException e) {
  24.         // Error while creating file
  25.     }
  26.     return file;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement