public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 定义写sdcard的方法
* @param view
*/
public void btnwrite(View view){
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//实例化文件
File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
//实例化输出流
OutputStream os=new FileOutputStream(file);
//向文件写入数据
os.write("万薪就业,高薪就业".getBytes());
//提交
os.flush();
//关闭
os.close();
}else{
Toast.makeText(this, "sdcard卡没安装好", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 定义读sdcard方法
* @param view
*/
public void btnread(View view){
try {
File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
//实例化输入流
InputStream is=new FileInputStream(file);
//缓冲读
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//读一行
String s = br.readLine();
//关闭流
is.close();
br.close();
//提示
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 定义写sdcard的方法
* @param view
*/
public void btnwrite(View view){
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//实例化文件
File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
//实例化输出流
OutputStream os=new FileOutputStream(file);
//向文件写入数据
os.write("万薪就业,高薪就业".getBytes());
//提交
os.flush();
//关闭
os.close();
}else{
Toast.makeText(this, "sdcard卡没安装好", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 定义读sdcard方法
* @param view
*/
public void btnread(View view){
try {
File file = new File(Environment.getExternalStorageDirectory()+"/rikao10.txt");
//实例化输入流
InputStream is=new FileInputStream(file);
//缓冲读
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//读一行
String s = br.readLine();
//关闭流
is.close();
br.close();
//提示
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}