先看看效果图吧,再看代码

 Android中从SD卡中获取歌词并与歌曲同步

 

转换文件的编码格式

 1 package com.xm;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedReader;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.IOException;
 8 import java.io.InputStreamReader;
 9 
10 /**
11  * 转换文件的编码格式
12  * 
13  * @author yangchuxi
14  * 
15  */
16 public class ConvertFileCode {
17     public String converfile(String filepath) {
18         File file = new File(filepath);
19         FileInputStream fis = null;
20         BufferedInputStream bis = null;
21         BufferedReader reader = null;
22         String text = "";
23         try {
24             fis = new FileInputStream(file);
25             bis = new BufferedInputStream(fis);
26             bis.mark(4);
27             byte[] first3bytes = new byte[3];
28             // System.out.println("");
29             // 找到文档的前三个字节并自动判断文档类型。
30             bis.read(first3bytes);
31             bis.reset();
32             if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {// utf-8
33 
34                 reader = new BufferedReader(new InputStreamReader(bis, "utf-8"));
35 
36             } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) {
37 
38                 reader = new BufferedReader(new InputStreamReader(bis, "unicode"));
39             } else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) {
40 
41                 reader = new BufferedReader(new InputStreamReader(bis, "utf-16be"));
42             } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) {
43 
44                 reader = new BufferedReader(new InputStreamReader(bis, "utf-16le"));
45             } else {
46 
47                 reader = new BufferedReader(new InputStreamReader(bis, "GBK"));
48             }
49             String str = reader.readLine();
50 
51             while (str != null) {
52 //                text = text + str + "/n";
53 //                str = reader.readLine();
54                 text = text + str + "/n";
55                 str = reader.readLine();
56                 if(str==null){
57                     break;
58                 }
59                 System.out.println(str);
60             }
61         } catch (Exception e) {
62             e.printStackTrace();
63         } finally {
64             if (fis != null) {
65                 try {
66                     fis.close();
67                 } catch (IOException e) {
68                     e.printStackTrace();
69                 }
70             }
71             if (bis != null) {
72                 try {
73                     bis.close();
74                 } catch (IOException e) {
75                     e.printStackTrace();
76                 }
77             }
78         }
79         return text;
80     }
81 }
代码

相关文章:

  • 2021-06-20
  • 2021-07-11
  • 2021-12-12
  • 2021-05-30
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-17
  • 2021-12-22
  • 2021-07-05
  • 2022-12-23
  • 2021-09-24
  • 2021-08-03
  • 2022-01-01
相关资源
相似解决方案