本文主要是介绍31 Android 从Sdcard 卡 读取数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 从Sdcard 卡 读取数据* @param fileName* @return*/public String readtextFromSdcard(String fileName){File root=Environment.getExternalStorageDirectory();FileInputStream inputStream=null;ByteArrayOutputStream outputStream=new ByteArrayOutputStream();String state=Environment.getExternalStorageState();if(state.equals(Environment.MEDIA_MOUNTED)){File file=new File(root.getAbsolutePath()+"/txt");File file2=new File(file, fileName);int len=0;byte[] data=new byte[1024];if(file2.exists()){try {inputStream=new FileInputStream(file2);while ((len=inputStream.read(data)) != -1) {outputStream.write(data, 0, len);}return new String(outputStream.toByteArray());} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{if(outputStream != null){try {outputStream.close();} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}}}return null;}
测试类中相关代码:
public void read() {FileService service = new FileService();String str = service.readtextFromSdcard("aa.txt");System.out.println("------>>>>>>>" + str);}
这篇关于31 Android 从Sdcard 卡 读取数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!