读取文件为string

我需要在android中加载一个xml文件作为string,所以我可以加载到TBXML XMLparsing器库,并parsing它。 我现在读取文件为String的实现需要大约2秒,即使是一些非常小的一些KB文件。 有什么已知的快速方法,可以读取Java / Android中的string文件?


这是我现在的代码:

public static String readFileAsString(String filePath) { String result = ""; File file = new File(filePath); if ( file.exists() ) { //byte[] buffer = new byte[(int) new File(filePath).length()]; FileInputStream fis = null; try { //f = new BufferedInputStream(new FileInputStream(filePath)); //f.read(buffer); fis = new FileInputStream(file); char current; while (fis.available() > 0) { current = (char) fis.read(); result = result + String.valueOf(current); } } catch (Exception e) { Log.d("TourGuide", e.toString()); } finally { if (fis != null) try { fis.close(); } catch (IOException ignored) { } } //result = new String(buffer); } return result; } 

最终使用的代码如下:

 http://www.java2s.com/Code/Java/File-Input-Output/ConvertInputStreamtoString.htm public static String convertStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = convertStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } 

你可以使用org.apache.commons.io.IOUtils.toString(InputStream是,Charset chs)来做到这一点。

例如

 IOUtils.toString(context.getResources().openRawResource(<your_resource_id>), StandardCharsets.UTF_8) 

为了添加正确的库:

将以下内容添加到您的应用/ build.gradle文件中:

 dependencies { compile 'org.apache.directory.studio:org.apache.commons.io:2.4' } 

或者对于Maven回购看到 – > 这个链接

对于直接jar下载,请参阅 – > https://commons.apache.org/proper/commons-io/download_io.cgi

重新devise了源于 – > 接受的答案的方法集

@JaredRummler您的评论的答案:

读取文件为string

这不会在string的末尾添加一个额外的新行吗?

为了避免在最后添加一个换行符,您可以在第一个循环中使用一个布尔值,如代码示例中的Boolean firstLine

 public static String convertStreamToString(InputStream is) throws IOException { // http://www.java2s.com/Code/Java/File-Input-Output/ConvertInputStreamtoString.htm BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; Boolean firstLine = true; while ((line = reader.readLine()) != null) { if(firstLine){ sb.append(line); firstLine = false; } else { sb.append("\n").append(line); } } reader.close(); return sb.toString(); } public static String getStringFromFile (String filePath) throws IOException { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = convertStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; } 

有了文件,我们就可以提前知道这个尺寸,所以一下就读完了!

 String result; File file = ...; long length = file.length(); if (length < 1 || length > Integer.MAX_VALUE) { result = ""; Log.w(TAG, "File is empty or huge: " + file); } else { try (FileReader in = new FileReader(file)) { char[] content = new char[(int)length]; int numRead = in.read(content); if (numRead != length) { Log.e(TAG, "Incomplete read of " + file + ". Read chars " + numRead + " of " + length); } result = new String(content, 0, numRead); } catch (Exception ex) { Log.e(TAG, "Failure reading " + this.file, ex); result = ""; } } 

这是为我工作

 i use this path String FILENAME_PATH = "/mnt/sdcard/Download/Version"; public static String getStringFromFile (String filePath) throws Exception { File fl = new File(filePath); FileInputStream fin = new FileInputStream(fl); String ret = convertStreamToString(fin); //Make sure you close all streams. fin.close(); return ret; }