zoukankan      html  css  js  c++  java
  • Android Saving Data(二)

            Saving File

    android读写文件的形式和普通的java IO的方式并没有什么不同,唯一有所限制的是当我们创建文件的时候不能够在像javaSE那样随意了。一般android读写文件有两种形式:

    1 File file = new File(context.getFilesDir(), filename);

    2 FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

    强烈推荐使用第一种方式,因为在第一种方式的情况下我们可以使用许多高级点的文件读写方法,这样不说能提高效率,但是能够减少错误概率。

    代码:

    public class MainActivity extends Activity {
        TextView view;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_layout);
            view=(TextView) findViewById(R.id.textView1);
            Button write=(Button) findViewById(R.id.button1);
            write.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    view.setText(getFilesDir().toString());
                    String filename = "myfile";
                    String string = "Hello world!";
                    FileOutputStream outputStream;
    
                    try {
                      outputStream = openFileOutput(filename, Context.MODE_APPEND);
                      PrintWriter pw=new PrintWriter(outputStream);
                      pw.println(string);
                      pw.close();
                      if(outputStream!=null)outputStream.close();
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                    
                }
            });
            Button read=(Button) findViewById(R.id.button2);
            read.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    
                    String filename="myfile";
                    try {
                        BufferedReader reader=new BufferedReader(new FileReader(new File(getFilesDir(), filename)));
                        String res="";
                        res=reader.readLine();
                        view.setText(res);
                        reader.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    View Code

    注意:android中内存和外存的读写没有什么不同,内存获取目录使用getFileDir(),而外存用:getExternalStoragePublicDirectory()用于在外存公共部分保存文件

    getExternalFilesDir()用于保存个人文件的时候使用。

    此外:

    getExternalStorageState() 查询外部存储状态;

    getFreeSpace()getTotalSpace()查出是否有足够的可用空间;

    getCacheDir() 定期创建的所有缓存文件。

  • 相关阅读:
    第37天新版动画系统和有限状态机
    第36天旧版动画系统
    第35天2D游戏相关
    第34天协同程序和异步加载
    第33天力、射线检测、球形检测和延迟函数
    第32天Line渲染器,物理系统和力
    第31天Camera组件和灯光组件
    第29天动态加载、对象池
    第28天3D数学
    第27天3D数学
  • 原文地址:https://www.cnblogs.com/lichao-normal/p/6158211.html
Copyright © 2011-2022 走看看