我有一个文件夹里面全是音乐文件,我要用XML文件把他们的路径和名字全部整理出来,求一个类!或者EXE程序也可
这种格式的XML就可以不用复杂
<sound1>
<name>123</name>
<url>sound/111111.mp3</url>
</sound1>
<sound2></sound2>.....
这种格式的XML就可以不用复杂
<sound1>
<name>123</name>
<url>sound/111111.mp3</url>
</sound1>
<sound2></sound2>.....
解决方案 »
- HelloWorld有几种写法?请各位高手赐教谢谢了!
- JAVA Local到底怎么用啊??
- java关于四舍五入
- JAVA面试题集
- 本人刚学java,请高手提供一些文章和好的网址,和一些教程在哪里下的,最好是电子版的书.
- 从JTextField中获得的数据如何存为数据库中的Money类型数据
- 哪位大虾帮忙解决一下困扰我的问题!!!多谢!
- ServerSocket 和 Socket 相互发信息的问题
- 请feiyuegaoshan(飞跃) 来领分 (其他朋友别进)
- 这个数据库连接程序错在哪!!!分必给!!!
- Swing Jtree如何实现这样显示
- 请问有高手大侠,能帮帮我看一下怎样去修改一个发送图片的发送端和接收端吗,现在的问题是获取对方机IP后,发送过去的图片打开后不能预览,请问该怎样修改
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;public class Demo {
//xml保存路径
private static String path = "E:" + java.io.File.separator + "data.xml"; public static void main(String[] args) {
//文件所在路径
Map<Integer, String> map = readfile("D:/QMDownload/SoftMgr", null);
createXml(map);
System.out.println("ok");
} private static void createXml(Map<Integer, String> map) {
Element root, sound, name, url; root = new Element("music"); for (int i = 0; i < map.size(); i++) {
sound = new Element("sound");
sound.setAttribute("id", i + "");
name = new Element("name");
url = new Element("url");
name.setText(i+1+"");
url.setText(map.get(i));
sound.addContent(url);
sound.addContent(name);
root.addContent(sound);
}
Document doc = new Document(root); saveXML(doc); } /**
* 保存XML
*
* @param doc
*/
private static void saveXML(Document doc) {
try {
XMLOutputter XMLOut = new XMLOutputter();
Format f = Format.getPrettyFormat();
f.setEncoding("utf-8");
XMLOut.setFormat(f);
XMLOut.output(doc, new FileOutputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
*
* 读取某个文件夹下的所有文件夹和文件, 返回所有文件名
*
* @param filepath
* String
* @throws FileNotFoundException
* @throws IOException
* @return Map<Integer, String> pathMap
*
*/
public static Map<Integer, String> readfile(String filepath,
Map<Integer, String> pathMap) {
try {
if (pathMap == null) {
pathMap = new HashMap<Integer, String>();
} File file = new File(filepath);
// 文件
if (!file.isDirectory()) {
pathMap.put(pathMap.size(), file.getPath()); } else if (file.isDirectory()) { // 如果是目录, 遍历所有子目录取出所有文件名
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "/" + filelist[i]);
// System.out.println("文件名:" + readfile.getName());
if (!readfile.isDirectory()) {
pathMap.put(pathMap.size(), readfile.getPath()); } else if (readfile.isDirectory()) { // 子目录的目录
readfile(filepath + "/" + filelist[i], pathMap);
}
}
}
} catch (Exception e) {
}
return pathMap;
}
}
试一下,看看符合你的要求不