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();
    }
原文地址:https://www.cnblogs.com/laoquans/p/3066349.html