zoukankan      html  css  js  c++  java
  • android 操作文件

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            try{
                 write("prothro.sq","welcome to the hell");
                 String str = read("prothro.sq");
                 Log.v("sq", str);
                 
            }catch(IOException e){e.printStackTrace();}
           
        }
        
        
        
        
        String read(String fileName) throws IOException
        {
            FileInputStream fis = this.openFileInput(fileName);        // openFileInput(String FileName) 是android 取得 FileInputStream 的方法
            StringBuilder sb = new StringBuilder();        //相当于 StringBuffered
            
            byte[] buff = new byte[1024];    //存放数据的字节数组
            int hasRed = 0;    
                                        //像java 那样读取文件内容
            while((hasRed = fis.read(buff))>0)
            {
                sb.append(new String(buff,0,hasRed));
            }
            return sb.toString();
        }
        
        
        
        
        
        void write(String fileName,String content) throws IOException
        {
            FileOutputStream fos = this.openFileOutput(fileName, MODE_APPEND);
            PrintStream ps = new PrintStream(fos); //将 FileOutputStream 包装成 打印流
            ps.println(content);    //打印到文件中去
            ps.close();
        }
  • 相关阅读:
    awk
    django教材
    saltstack的安装过程
    OPENSTACK学习笔记(1)
    5G核心网架构
    内存采集
    分析CPU文件
    环境管理系统
    属性的两种定义方式
    Python 面向对象(初级篇)
  • 原文地址:https://www.cnblogs.com/laoquans/p/3066349.html
Copyright © 2011-2022 走看看