代码如下
String fileName = "SdcardFile-"+System.currentTimeMillis()+".txt";
File dir = new File("/sdcard/mysdcardtest/");
if(!dir.exists()){
dir.mkdir();
}
if(dir.exists()&&dir.canWrite()){
File newFile = new File(dir.getAbsolutePath()+"/"+fileName);
FileOutputStream fos = null;
try{
newFile.createNewFile();
if(newFile.exists()&&newFile.canWrite()){
fos = new FileOutputStream(newFile);
fos.write(randomNumbersString.getBytes());
TextView labelView = (TextView)findViewById(R.id.textView1);
labelView.setText(fileName+"文件写入SD卡");
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(fos!=null){
try{
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
如果有帮到您,打个赏呗