标签:style http java color os strong
上周末的时候,朋友约好去KTV,鉴于我这种不怎么听歌的孩子伤不起啊,灵机一动就把我的酷狗歌单导出来了,XML文件嘛,内容太多,我只想要歌名足已。于是写了一个java去输出歌名。
public void output(String filePath) throws FileNotFoundException{
Scanner scanner=new Scanner(new File(filePath));
String str=null;
while(scanner.hasNextLine()){
str=scanner.nextLine();
if(str.indexOf("FileName")>0){
System.out.println(str.substring(str.indexOf(">")+1, str.lastIndexOf("<")));
}
}
scanner.close();
}
<File>
<MediaFileType>0</MediaFileType>
<FileName>周杰伦 - 爱在西元前.mp3</FileName>
<FilePath>D:\music\</FilePath>
<FileSize>5623610</FileSize>
<Duration>234292</Duration>
<Hash>0589341ba15528a4c63e36c49a3c0e45</Hash>
<Lyric>E:\KuGou\Lyric\周杰伦 - 爱在西元前-0589341ba15528a4c63e36c49a3c0e45.krc</Lyric>
<Bitrate>192004</Bitrate>
<MandatoryBitrate>0</MandatoryBitrate>
</File>
public void output(String filePath) throws FileNotFoundException{
Scanner scanner=new Scanner(new File(filePath),"UTF-8");//Always explicitly pass a charset when working with text
String str=null;
while(scanner.hasNextLine()){
str=scanner.nextLine();
if(str.indexOf("FileName")>0)
System.out.println(str.substring(str.indexOf(">")+1, str.lastIndexOf("<")));
}
//It‘s better to check IOException when working with Scanner(PS:find UnmappableCharacterException took me more time,finally by debug to found )
IOException ioException = scanner.ioException();
if (ioException != null) {
ioException.printStackTrace();
}
scanner.close();
}
Scanner遇上UnmappableCharacterException,布布扣,bubuko.com
Scanner遇上UnmappableCharacterException
标签:style http java color os strong
原文地址:http://blog.csdn.net/alec_king/article/details/38026111