android手机操作SD的使用方法
写入SD卡
package com.example.openfileproject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String FILENAME="liyuanjing.txt";//文件名
private static final String DIR="liyuanjing";//文件夹名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD存在时
File file=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+DIR
+File.separator+FILENAME);
if(!file.getParentFile().exists()){//如果文件夹不存在
file.getParentFile().mkdirs();//创建文件夹
}
PrintStream out=null;//打印流对象用于输出
try {
out=new PrintStream(new FileOutputStream(file));//追加文件
out.println("http://weibo.com/liyuanjinglyj");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
out.close();//关闭打印流
}
}
}else{//使用Toast提示用户SD卡不存在
Toast.makeText(this, "SD卡不村在", Toast.LENGTH_LONG).show();
}
}
@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;
}
}
从SD卡读数据
package com.example.openfileproject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String FILENAME="liyuanjing.txt";//文件名
private static final String DIR="liyuanjing";//文件夹名
private TextView txt=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.txt=(TextView)super.findViewById(R.id.txt);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD存在时
File file=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+DIR
+File.separator+FILENAME);
if(!file.getParentFile().exists()){//如果文件夹不存在
file.getParentFile().mkdirs();//创建文件夹
}
Scanner scan=null;//扫描输入
try {
scan=new Scanner(new FileInputStream(file));//实例化Scanner
while(scan.hasNext()){//循环读取
this.txt.append(scan.next()+"\n");//设置文本
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(scan!=null){
scan.close();//关闭Scanner
}
}
}else{//使用Toast提示用户SD卡不存在
Toast.makeText(this, "SD卡不村在", Toast.LENGTH_LONG).show();
}
}
@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;
}
}
文章来源: liyuanjinglyj.blog.csdn.net,作者:李元静,版权归原作者所有,如需转载,请联系作者。
原文链接:liyuanjinglyj.blog.csdn.net/article/details/9219149
- 点赞
- 收藏
- 关注作者
评论(0)