读写文件操作

读文件

```
 String FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/a.text" ;
    
 FileInputStream fis = new FileInputStream(FILE_PATH);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
        String readLine = "";
        StringBuffer sb = new StringBuffer();
        while ((readLine = br.readLine()) != null) {
            sb.append(readLine);
        }
        br.close();

```

写文件

```
 try {
        File file = new File(FILE_PATH);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        String jsonString = JSONUtils.getJsonString(musicInfos);
        writer.write(jsonString);
        writer.flush();
        showMsg("写入成功");
    } catch (Exception e) {
        e.printStackTrace();
        showMsg("写入失败");
    }

```

File类常用操作

```
String Name = File.getName();  //获得文件或文件夹的名称:  
String parentPath = File.getParent();  //获得文件或文件夹的父目录  
String path = File.getAbsoultePath();//绝对路经  
String path = File.getPath();//相对路经   
File.createNewFile();//建立文件    
File.mkDir(); //建立文件夹    
File.isDirectory(); //判断是文件或文件夹  
File[] files = File.listFiles();  //列出文件夹下的所有文件和文件夹名  
File.renameTo(dest);  //修改文件夹和文件名  
File.delete();  //删除文件夹或文件

你可能感兴趣的:(读写文件操作)