文件创建模式
文件创建模式 | 描述 |
---|---|
MODE_PRIVATE | 创建或替换文件 |
MODE_APPEND | 文件不存在则创建文件,存在的话文件尾部追加内容 |
FileOutputStream 类的某些公共方法
方法 | 描述 |
---|---|
void close() | 关闭调用流 |
void write(byte[] buffer, int offset, int byteCount) | 将来自缓冲区的内容写到文件中,写入内容的字数为,WbyteCount参数指定的字节数 |
void write(byte[] buffer) | 将整个缓冲区的内容写入文件 |
void write(int oneByte) | 将一个字节的内容写入到文件中 |
写文件案例
// 写文件
FileOutputStream openFileOutput = openFileOutput("myInternaldata.txt", MODE_PRIVATE);
openFileOutput.write("666".getBytes());
openFileOutput.close();
读文件案例
FileInputStream openFileInput = openFileInput("myInternaldata.txt");
byte[] byte1 = new byte[openFileInput.available()]; // available 可以拿到文件的长度
if(openFileInput.read(byte1) != -1){
String dada = new String(byte1);
textView1.setText(dada);
}
openFileInput.close();