使用FileInputStream和FileOutputStream的对象调用方法来创建
将数据写入手机内存
public class HandFile { private Context context; public HandFile(Context context) { this.context = context; } public void writeFileData(String filename,String message){ try { FileOutputStream fileOutputStream =context.openFileOutput(filename,context.MODE_PRIVATE); byte[] bytes=message.getBytes(); fileOutputStream.write(bytes); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String readFileData(String fileName){ String result=""; FileInputStream fileInputStream = null; try { fileInputStream = context.openFileInput(fileName); int length=fileInputStream.available(); byte[] bytes=new byte[length]; fileInputStream.read(bytes); result=new String(bytes); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } return result; } }自己创建 的HandFile类
public class MainActivity extends AppCompatActivity { private TextView mtv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String message="emmmm……我就是传说中的数据,嘻嘻嘻。我特么学android好累啊," + "可是又有什么办法呢,谁让我大一大二两年太放纵了呢,现在学点东西不至于找不到工作吧," + "考研也很累啊,下雪了,我的书还能到吗……"; HandFile handFile=new HandFile(this); handFile.writeFileData("first",message); String result=handFile.readFileData("first"); mtv= (TextView) findViewById(R.id.file); mtv.setText(result); } }将message的内容存入TextView。