android 存储和读取指定的文件中txt文本

public void save(String inputText){
    FileOutputStream out=null;
    BufferedWriter writer = null;
    try{
        out = openFileOutput("filename", Context.MODE_PRIVATE);
        writer=new BufferedWriter(new OutputStreamWriter(out));
        writer.write(inputText);
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try{
            if(writer!=null){
                writer.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

点击Android Device Monitor 进入File Explorer  /data/data/包名/file下filename


public String load(){
    FileInputStream in=null;
    BufferedReader reader = null;
    StringBuilder content=new StringBuilder();
    try{
        in = openFileInput("filename");
        reader=new BufferedWriter(new OutputStreamWriter(in));
        String line = "";
        while ((line = reader.readLine())!=null){
            content.append(line);
        }
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try{
            if(reader!=null){
                reader.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    return content.toString();
}


你可能感兴趣的:(file存取数据)