public String ReadTxtFile(String strFilePath) { String path = String.valueOf(strFilePath); String content = ""; //文件内容字符串 StringBuffer sb = new StringBuffer(); //打开文件 File file = new File(path); //如果path是传递过来的参数,可以做一个非目录的判断 if (file.isDirectory()) { } else { try { InputStream instream = new FileInputStream(file); if (instream != null) { String line=null; BufferedReader buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB2312")); //分行读取 line = buffreader.readLine(); if(line != null) onetxt = line+"\n"; while ((line = buffreader.readLine()) != null) { content += line + "\n"; } instream.close(); } } catch (java.io.FileNotFoundException e) { Log.d("TestFile", "The File doesn't not exist."); } catch (IOException e) { Log.d("TestFile", e.getMessage()); } } return content; }
直接使用这个方法就行
ReadTxtFile("/mnt/external_sd/");