Android文本读写

时间:2023-12-16 23:18:14

//写文件操作
   public void writeFileData(String fileName, String message){
        try{
            FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
            byte[] bytes = message.getBytes();
            fout.write(bytes);
            fout.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//读文件操作
    public String readFileData(String fileName){
        String res = "";
        try {
            FileInputStream fin = openFileInput(fileName);
            int length = fin.available();
            byte[] buffer = new byte[length];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }