欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

File存储——IO操作文件

openFileOutput、openFileInput

Context提供了如下两个方法来打开本应用程序的数据文件夹里面的文件IO流。

1.FileInputStream openFileInput(String name):打开应用程序中的数据文件夹下的name文件对应输入流

2.FileOutputStream openFileOutput(String name,int mode):打开应用程序的数据文件下的name文件对应的输出流

注意:

FileOutputStream openFileOutput(String name,int mode)中mode参数用于指定打开文件的模式,支持模式如下:

1.MODE_PRIVATE:改文件只能被当前程序读写;

2.MODE_APPEND:以追加的方式打开该文件,应用程序可以向该文件中追加内容;

3.MODE_WORLD_READABLE:该文件的内容可以被其他程序读取(只读);

4.MODE_WORLD_WRITEABLE:该文件的内容可以不其他程序读、写;

除此之外,Context还提供了如下两个方法来访问应用程序的数据文件夹.

1.getDir(String name,int mode):在应用程序的数据文件夹下获取或创建name对应的子目录;

2.File getFilesDir():获取该应用程序的数据文件夹的绝对路径;

3.String[] fileList():返回该应用程序的数据文件夹下的全部文件;

4.deleteFile(String):删除该应用程序的数据文件夹下指定的文件;

实例如下:实现读写应用程序数据文件夹内的文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/btnone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="write" />
 
    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="read" />
 
</LinearLayout>
 
代码实现==》
package com.example.myfile;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
 
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity
{
    final String FILE_NAME = "crazyit.bin";
    EditText Edit1;
    EditText Edit2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button btnOne = (Button) this.findViewById(R.id.btnone);
        Button btnTwo = (Button) this.findViewById(R.id.btnTwo);
        Edit1 = (EditText) this.findViewById(R.id.edit1);
        Edit2 = (EditText) this.findViewById(R.id.edit2);
        btnOne.setOnClickListener(new MyButtonClick());
        btnTwo.setOnClickListener(new MyButtonClick());
    }
 
    private class MyButtonClick implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
            case R.id.btnone:
                Log.i("swg", "write==" +Edit1.getText());
                write(Edit1.getText().toString());
                Edit1.setText("");
                break;
            case R.id.btnTwo:
                Log.i("swg", "read==" +read());
                Edit2.setText(read());
                break;
            }
        }
 
        private String read()
        {
            try
            {
                FileInputStream fis = openFileInput(FILE_NAME);
                byte[] buff = new byte[1024];
                int hasread = 0;
                StringBuilder sb = new StringBuilder("");
                while ((hasread = fis.read(buff)) > 0)
                {
                    sb.append(new String(buff, 0, hasread));
                }
                return sb.toString();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
 
        private void write(String content)
        {
            try
            {
                // 以追加模式打开文件输出流
                FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);
                // 将FileOutputStream包装成PrintStream
                PrintStream ps = new PrintStream(fos);
                // 输出文件内容
                ps.println(content);
                ps.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

运行效果:

 

posted on   sunwugang  阅读(337)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示