Android学习之IO流

来源:互联网 发布:淘宝商城家具床 编辑:程序博客网 时间:2024/04/30 19:42

文件读取:

public String read(String filename) throws Exception{FileInputStream inStream = <span style="color:#006600;">context.openFileInput(filename)</span>;<span style="color:#6666cc;">ByteArrayOutputStream</span> outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while((len = inStream.read(buffer))!=-1){outStream.write(buffer,0,len);}byte[] data = outStream.toByteArray();inStream.close();outStream.close();return   new String(data);}
文件写入:

public void save(String filename, String fileContent) throws Exception {<span style="white-space:pre"></span>FileOutputStream outStream = <span style="color:#006600;">context.openFileOutput</span>(filename, <span style="color:#ff6666;">Context.MODE_PRIVATE</span>);outStream.write(fileContent.getBytes());outStream.close();}


不同模式的文件属性:

模式                                                 其他应用程序是否能访问              文件内容是否追加

Context.MODE_PRIVATE                               否                                              否

Context.MODE_APPEND                                否                                              是


Context.MODE_WORLD_WRITEABLE          只可读                                        否

Context.MODE_WORLD_READABLE            只可写                                        否


若要具有多个属性可用+号表示(每种模式代表一个整数常量)。






0 0
原创粉丝点击