zoukankan      html  css  js  c++  java
  • android内存文件读写

    android内存文件读写:无需权限

    public class MainActivity extends Activity implements OnClickListener {
        private Button fileSave;
        private Button fileRead;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            fileSave = (Button) findViewById(R.id.button1);
            fileRead = (Button) findViewById(R.id.button2);
            fileSave.setOnClickListener(this);
            fileRead.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
            case R.id.button1:
                FileOutputStream fos = null;
                try {
                    fos = openFileOutput("out.txt", Context.MODE_PRIVATE);
                    fos.write("text...".getBytes());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                break;
            case R.id.button2:
                FileInputStream fis = null;
                try {
                    fis = openFileInput("out.txt");
                    byte[] b = new byte[1024];
                    int len = 0;
                    StringBuilder sb = new StringBuilder();
                    while ((len = fis.read(b)) != -1) {
                        String string = new String(b, 0, len);
                        sb.append(string);
                    }
                    Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            default:
                break;
            }
        }
    }
  • 相关阅读:
    什么是马甲APP?怎么用马甲APP导流
    OC与JS交互前言-b
    UIWebView1-b
    Mac双系统切换
    iOS之手势滑动返回功能
    Duplicate Symbol链接错的原因总结和解决方法-b
    #ifndef#define#endif的用法-b
    iOS Copy 和 MutableCopy的区别 深浅拷贝的区别-供参考
    解决CocoaPods在OS X 10.11出现问题-b
    django中cookies和session
  • 原文地址:https://www.cnblogs.com/mada0/p/4836337.html
Copyright © 2011-2022 走看看