android 读取SD卡目录下文件

来源:互联网 发布:隔墙有耳偷听器淘宝 编辑:程序博客网 时间:2024/04/30 13:22

假如要读取SD卡-ceshi目录下的haha.txt文件中的内容,代码如下所示:

@Overridepublic void onCreate() {super.onCreate();readCatalog("haha.txt");}public void readCatalog(String fileName){String catalogText = "";File file = new File(Paths.getDataDirectoryPath() + fileName);try {FileInputStream input = new FileInputStream(file);BufferedInputStream buffer=new BufferedInputStream(input);BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, "utf-8"));String str = reader.readLine();while (str != null) {catalogText = catalogText + str + "\n";str = reader.readLine();}reader.close();   Log.e("读sd卡文件", catalogText);}catch (Exception e) {         e.printStackTrace();     } }public static String getDataDirectoryPath() {return getDataDirectory().getPath() + File.separatorChar;}public static File getDataDirectory() {File dir = new File(cardDirectory() + "data");if (!dir.exists()) dir.mkdirs();return dir;}public static String cardDirectory() {return Environment.getExternalStorageDirectory().getPath() + "/ceshi/";}


 

原创粉丝点击