1. 读取文本文件的方法
2. //-------把文本文件读入 String, 字符串之间的分隔符为 '\------
3. //----FilePath 为文件全路径-------------
4. public String ReadTxtFileToString(String FilePath )
5. {
6. String content = ""; //文件内容字符串
7.
8. //获取文件编码
9. String codeformat = getCodeFormat( FilePath ).trim();
10. if ( codeformat.equals("") ) {
11. codeformat = "GBK";
12. }
13.
14. //打开文件
15. try {
16. File file = new File( FilePath );
17. FileInputStream fis = new FileInputStream( file );
18. BufferedReader reader = new BufferedReader(new
InputStreamReader(fis, codeformat));
19. String line;
20. //分行读取
21. while ( ( line = reader.readLine()) != null ) {
22. content += line + "";
23. }
24. fis.close();
25. reader.close();
26. } catch ( java.io.FileNotFoundException e ) {
27. Log.i( tag, FilePath + " 不存在。");
28. } catch (IOException e) {
29. Log.i( tag, "FileOperate 271 error= " +
e.getMessage() );
30. }
31. return content;
32. }
2.判断文本文件的编码格式--------------
public String getCodeFormat(String filepath) {
String codeformat = "";
File file = new File(filepath);
BufferedReader reader;
String text = "";
byte[] first3bytes = new byte[3];
FileInputStream fis = null;
BufferedInputStream in = null;