废话不多说了,直接给大家贴代码了,具体代码如下
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<Button
android:id= "@+id/save"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "保存数据(File)" />
<Button
android:id= "@+id/read"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "读取数据(File)" />
</LinearLayout>
package com.example.yanlei.wifi;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
private Button btnSave= null ;
private Button btnRead= null ;
private File file= null ;
private static final String FILENAME= "data.txt" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave=(Button) super .findViewById(R.id.save);
btnRead=(Button) super .findViewById(R.id.read);
btnSave.setOnClickListener( new OnClickListener(){
public void onClick(View v)
{
PrintStream ps= null ;
//判断外部存储卡是否存在
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!" , Toast.LENGTH_LONG).show();
return ;
}
//初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+ "genwoxue"
+File.separator
+FILENAME;
file= new File(path);
//如果当前文件的父文件夹不存在,则创建genwoxue文件夹
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
//写文件
try {
ps = new PrintStream( new FileOutputStream(file));
ps.println( "跟我学网址:www.genwoxue.com" );
ps.println( "" );
ps.println( "电子邮件:hello@genwoxue.com" );
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
ps.close();
}
Toast.makeText(getApplicationContext(), "保存成功!" , Toast.LENGTH_LONG).show();
}
});
btnRead.setOnClickListener( new OnClickListener(){
public void onClick(View v)
{
StringBuffer info= new StringBuffer();
//判断外部存储卡是否存在
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!" , Toast.LENGTH_LONG).show();
return ;
}
//初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+ "genwoxue"
+File.separator
+FILENAME;
file= new File(path);
if (!file.exists()){
Toast.makeText(getApplicationContext(), "文件不存在!" , Toast.LENGTH_LONG).show();
return ;
}
//读取文件内容
Scanner scan= null ;
try {
scan= new Scanner( new FileInputStream(file));
while (scan.hasNext()){
info.append(scan.next()).append( "☆☆☆\n" );
}
Toast.makeText(getApplicationContext(), info.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scan.close();
}
}
});
}
}
|
权限
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= "com.example.yanlei.wifi" >
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup= "true"
android:icon= "@mipmap/ic_launcher"
android:label= "@string/app_name"
android:theme= "@style/AppTheme" >
<activity
android:name= ".MainActivity"
android:label= "@string/app_name" >
<intent-filter>
<action android:name= "android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
|
以上所述是小编给大家介绍的Android 数据库文件存取至储存卡的方法,希望对大家有所帮助,本文写的不好还请各位大侠见谅!