Android 创建文件,删除文件,加载本地txt文件,string转txt文件,创建文件夹,读取文件夹,open failed: ENOENT
生活随笔
收集整理的這篇文章主要介紹了
Android 创建文件,删除文件,加载本地txt文件,string转txt文件,创建文件夹,读取文件夹,open failed: ENOENT
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、創(chuàng)建文件
String fileName = "my.txt"; File fs = new File(Environment.getExternalStorageDirectory() + "/msc/" + fileName);安卓10內(nèi)存文件?
String downFileDir = context.getFilesDir().getAbsolutePath(); File dest = new File(downFileDir, tempName);?
msc可要可不要? ?或者是"/"
創(chuàng)建圖片文件
final File file = File.createTempFile(UUID.randomUUID().toString() + "", ".jpg");2、刪除文件
String ss = "/mnt/sdcard/my.txt"; // String ss = "/storage/emulated/0/Pictures/Screenshots/Screenshot_20190814-141238.jpg";File file = new File(ss);if (file.exists()){file.delete();}?/mnt/sdcard/? ? ?等于? ?/storage/emulated/0/
3、string轉(zhuǎn)為本地txt文件
String fileName = "my.txt"; File fs = new File(Environment.getExternalStorageDirectory()+"/" + fileName); if (fs.exists()){fs.delete(); } String charset = "UTF-8"; // 寫(xiě)字符換轉(zhuǎn)成字節(jié)流 try {FileOutputStream outputStream = new FileOutputStream(fs);OutputStreamWriter writer = new OutputStreamWriter(outputStream, charset);try {writer.write("這是要保存的中文字符aaabbbb大范圍sdfsdfsf");} finally {writer.close();}} catch (IOException e) {e.printStackTrace(); }文件夾不存在時(shí)——open failed: ENOENT (No such file or directory)。解決方法----創(chuàng)建文件夾即可
if (fs.exists()){fs.delete(); }else {fs.getParentFile().mkdirs(); }4、讀取本地txt文件,轉(zhuǎn)為string
String fname = "/mnt/sdcard/my.txt"; private String loadTXTFromSDFile(String fname) {String result=null;try {File f=new File(fname);int length=(int)f.length();byte[] buff=new byte[length];FileInputStream fin=new FileInputStream(f);fin.read(buff);fin.close();result=new String(buff,"UTF-8");}catch (Exception e){e.printStackTrace();}return result; }5、創(chuàng)建文件夾
// 文件存儲(chǔ) private File updateDir = null; // 創(chuàng)建文件 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {updateDir = new File(Environment.getExternalStorageDirectory(), "data/cn.dlc.xiaoyao/msc2");if (!updateDir.exists()) {updateDir.mkdirs();LogPlus.i("lgq","bbh-ss-video_savePath--333- ");} }6、讀取文件夾內(nèi)容?
File sceneFile = new File(Environment.getExternalStorageDirectory(), "data/cn.dlc.xiaoyao/msc1"); LogPlus.i("lgq","bbh-ss-video_savePath--- "+sceneFile.exists());File[] files = sceneFile.listFiles();if (null != files) {for (int i = 0; i < files.length; i++) {LogPlus.e("lgq文件夾下的文件:" + files[i].getName()+"......"+files[i].getPath()+"..."+getAudioFileVoiceTime(files[i].getPath()));}}總結(jié)
以上是生活随笔為你收集整理的Android 创建文件,删除文件,加载本地txt文件,string转txt文件,创建文件夹,读取文件夹,open failed: ENOENT的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Arcgis实现路线等线方向箭头表示
- 下一篇: Android中1像素Activity进