SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir

读写sd卡中的文件按照如下步骤:1调用Environment的getExternalStorageState()方法判断手机上是否插入了sd卡,并且应用程序具有读写SD卡的能力

//如果手机已经插入了SD卡,且具有读写sd卡的能力,下面的语句将会返回true

Environment.getExternalStorageState().equals(Envronment.MEDIA_MOUNTED)

2)调用environment的getExternalStorageDIrectory()方法获取外部存储器,也就是SD卡的目录

3)使用FileInputStream、FileOUtputStream FileReader或者FileWriter来读写sd卡中的文件

注册权限

<--!在SD卡中创建与删除文件权限-->


<--!向SD卡写入数据的权限--->

<--!读取数据的权限--->

实例代码:

有两个EditText 和两个Button组件,text_Write用于写入数据,Button(write)组件用于读取text_Write中的数据并写入/storage/emulated/0/object.txt 文件中;

Button(read)组件用于从SD卡的/storage/emulated/0/object.txt中读取数据,并显示在text_Read组件中。




    
    
    
主程序:
package com.example.sdcard03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText read_Text;
	private EditText write_Text;
	private Button read;
	private Button write;
	private String fileName = "/object.txt";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		read = (Button) findViewById(R.id.read);
		write = (Button) findViewById(R.id.write);
		read_Text = (EditText) findViewById(R.id.text_Read);
		write_Text = (EditText) findViewById(R.id.text_Write);
		read.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String s = readFromSdcard();
				read_Text.setText(s);

			}
		});
		// ------------------------
		write.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String s2 = write_Text.getText().toString();
				writeToSdcard(s2);

			}

		});

	}

	public String readFromSdcard() {

		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			try {
				File sdPath = Environment.getExternalStorageDirectory();
				System.out.println(sdPath.toString());

				FileInputStream fis = new FileInputStream(sdPath
						.getCanonicalFile().toString() + fileName);
				System.out.println(sdPath.getCanonicalFile().toString());
				BufferedReader br = new BufferedReader(new InputStreamReader(
						fis));
				StringBuilder sb = new StringBuilder("");
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line);

				}
				fis.close();
				br.close();
				return sb.toString();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;

	}

	// -------------------------------------------------------
	public void writeToSdcard(String s) {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File sdCardDir = Environment.getExternalStorageDirectory();
			try {
				File file = new File(sdCardDir.getCanonicalPath() + fileName);
				FileOutputStream fos = new FileOutputStream(file);
				
				fos.write(s.getBytes());
				fos.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		else {
			Toast.makeText(MainActivity.this, "sd卡异常", Toast.LENGTH_LONG)
					.show();
		}

	}
}

几点注意:1 不要忘记注册权限

2,

File sdPath = Environment.getExternalStorageDirectory();中getExternalStorageDirectory()方法返回的是什么?
sdPath.getCanonicalFile().toString()方法返回的是什么?
通过上面两行蓝色输出语句可以在logcat中看到返回的都是/storage/emulated/0   注意在0后面没有“/”反斜杠,所以我们在最初声明字符串常量filName时
private String fileName = "/object.txt";object前有反斜杠,也就是代表了路径
/storage/emulated/0/object.txt 。如果我们最初在声明fileName时不加反斜杠就变成了/storage/emulated/0object.txt,这个路径是不合法的,不存在程序会报错




03-15 15:07:39.440: W/System.err(26730): java.io.FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or directory)


 
  
 
  
 
  
 
  
 
  


 
  

 
  

你可能感兴趣的:(Android,数据读写)