本文共 2026 字,大约阅读时间需要 6 分钟。
加入读写权限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
代码
其中有数组和Arraylist两种方式取数据
- package readfile;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
-
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.Context;
- import android.net.Uri;
- import android.os.Environment;
- import android.provider.ContactsContract.RawContacts;
- import android.provider.ContactsContract.CommonDataKinds.Phone;
- import android.provider.ContactsContract.Contacts.Data;
- import android.widget.Toast;
-
- public class readfile {
- public Object readFromFile(Context context){
-
- if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
- String foldername = Environment.getExternalStorageDirectory().getPath()+ "/";
- File folder = new File(foldername);
- if (folder == null || !folder.exists()) {
- folder.mkdir();
- }
- File targetFile=new File("/sdcard/Tel.txt");
- String readedStr="";
- try{
- if(!targetFile.exists()){
- targetFile.createNewFile();
- return "No File error ";
- }else{
- InputStream in = new BufferedInputStream(new FileInputStream(targetFile));
- BufferedReader br= new BufferedReader(new InputStreamReader(in, "UTF-8"));
- String tmp;
- int x = 0;
-
- ArrayList<String> List = new ArrayList<String>();
- while((tmp=br.readLine())!=null){
- List.add(x, tmp) ;
-
- System.out.println("123+"+List);
-
- x++;
- }
- br.close();
- in.close();
- return List;
-
- }
- } catch (Exception e) {
- Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
- return e.toString();
- }
- }else{
- Toast.makeText(context,"未发现SD卡!",Toast.LENGTH_LONG).show();
- return "SD Card error";
- }
- }
- }
-
转载:http://blog.csdn.net/chaoyu168/article/details/48807275