zoukankan      html  css  js  c++  java
  • Android数据储存之File

    openFileOutStream 和 openFileInStream

    FileInputStream fileInputStream = openFileInput(name);  打开应用下文件名称问name的输入流;

    获取应用下某个文件的内容:

    /**
    	 * 读
    	 * @return
    	 */
    	public String read(){
    		try {
    			//打开输入流
    			FileInputStream fileInputStream = openFileInput(name);
    			//创建byte数组
    			byte[] buffer = new byte[1034];
    			int i = 0;
    			//创建StringBuilder 对象
    			StringBuilder builder = new StringBuilder();
    			//循环读取fileInputStream中的字节
    			while ((i =fileInputStream.read(buffer)) > 0) {
    				builder.append(new String(buffer, 0, i));
    			}
    			//关闭输入流
    			fileInputStream.close();
    			//返回输入流中的字符串
    			return builder.toString();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    

     FileOutputStream fileOutputStream = openFileOutput(name, MODE_PRIVATE);打开本应用下名称为name的输出流

    第一个参数为文件名称;

    第二个参数为写入模式;

    写入模式常用有四种:

    MODE_ORIVATE  该文件只能被当前程序读写

    MODE_APPEND   已追加的方式打开文件,程序可以向文件中追加内容

    MODE_WORLD_READABLE 该文件的内容可以被其他程序读取

    MODE_WORLD_WEITEABLE 该文件的内容可以被其他程序读写

    示例:

    /**
    	 * 写
    	 * @param str 文件中的字符串(内容)
    	 */
    	public void write(String str){
    		try {
    			//获取输出流对象,已追加的方式打开输出流
    			FileOutputStream fileOutputStream = openFileOutput(name, MODE_PRIVATE);
    			//将输出流封装成PrintStream对象
    			PrintStream printStream = new PrintStream(fileOutputStream);
    			//输出写入内容
    			printStream.print(str);
    			//关闭输出流
    			printStream.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    

     Context提供访问应用程序数据文件夹方法如下:

    getDri(String name,int mode)  在应用程序数据文件加下创建或打开以name为名称的子目录

    File getFileDri()    获取应用程序的数据文件夹的绝对路径

    String[] fileList() 返回应用程序文件夹下全部文件

    deleteFile(String name) 删除名称为name的数据文件

  • 相关阅读:
    电子海图开发一百篇第五十五篇-电子江图传输规范 数据模型
    天气可视化,海浪,温度图层的绘制,温度热力图的可视化
    全球潮汐数据API使用方法,潮汐数据查询
    海洋气象数据可视化,以流场的方式显示风场图,海洋气象API使用
    g++ 编译module失败
    编译gcc error-*** LIBRARY_PATH shouldn‘t contain the current directory when *** building gcc.
    windows两种自启动的区别
    windows多线程加锁
    windows server 2012不显示此电脑
    pthread_cond_wait
  • 原文地址:https://www.cnblogs.com/shiguotao-com/p/5164368.html
Copyright © 2011-2022 走看看