您的位置:首页 > 移动开发 > Android开发

android_往sd中写入文件

2017-03-27 14:41 148 查看
if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz.txt"); 

FileOutputStream fos 

= new FileOutputStream(file);

fos.write( "鬼吹灯这部小说来源于真实的咧比故事。。".getBytes() );

fos.flush();

fos.close();

fos = null;

} catch (IOException e) {

e.printStackTrace();

}

}

} else if (view.getId() == R.id.button2) {

String a = "";

if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz1.txt"); 

FileInputStream fis 

= new FileInputStream(file);

int count = fis.available();

byte[] bytes = new byte[count];

int len=0;

while((len=fis.read(bytes))!=-1){

System.out.println(len);

a=a+new String(bytes,0,len);

}

aa.setText(a);

fis.close();

fis = null;

} catch (IOException e) {

e.printStackTrace();

}

} }

else if (view.getId() == R.id.button3) {

String a = "";

if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz2.txt"); 

FileInputStream fis 

= new FileInputStream(file);

int count = fis.available();

byte[] bytes = new byte[count];

int len=0;

while((len=fis.read(bytes))!=-1){

System.out.println(len);

a=a+new String(bytes,0,len);

}

aa.setText(a);

fis.close();

fis = null;

} catch (IOException e) {

e.printStackTrace();

}

} }



if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz.txt"); 

FileOutputStream fos 

= new FileOutputStream(file);

fos.write( "鬼吹灯这部小说来源于真实的咧比故事。。".getBytes() );

fos.flush();

fos.close();

fos = null;

} catch (IOException e) {

e.printStackTrace();

}

}

} else if (view.getId() == R.id.button2) {

String a = "";

if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz1.txt"); 

FileInputStream fis 

= new FileInputStream(file);

int count = fis.available();

byte[] bytes = new byte[count];

int len=0;

while((len=fis.read(bytes))!=-1){

System.out.println(len);

a=a+new String(bytes,0,len);

}

aa.setText(a);

fis.close();

fis = null;

} catch (IOException e) {

e.printStackTrace();

}

} }

else if (view.getId() == R.id.button3) {

String a = "";

if( Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED) )

{

File dir = Environment.getExternalStorageDirectory();

try {

File file = new File(dir, "jiaoz2.txt"); 

FileInputStream fis 

= new FileInputStream(file);

int count = fis.available();

byte[] bytes = new byte[count];

int len=0;

while((len=fis.read(bytes))!=-1){

System.out.println(len);

a=a+new String(bytes,0,len);

}

aa.setText(a);

fis.close();

fis = null;

} catch (IOException e) {

e.printStackTrace();

}

} }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: