`

java 读取CD-ROM 光驱中文件

阅读更多
我编写了一个利用Java控制光驱的打开与关闭,并从光盘中拷贝文件到本地磁盘的一个工具类.
注意:此工具类只适合最基本的从CD\DVD上拷贝文件到本地,不适合其抓取音轨等复杂操作.
package com.multiable.Composite;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;

/**
 * A program to Open CD drive using VB script with the help of JAVA
 * @author mark.wang
 * 
 */

public class CDUtils {
	private CDUtils() {
	}

	// 利用VB script 打开光驱
	public static void open(String drive) {
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
					+ "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
					+ drive + "\") \n cd.Eject";

			fw.write(vbs);
			fw.close();

			Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 利用VB script 关闭光驱
	public static void close(String drive) {
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new FileWriter(file);

			// to close a CD, we need eject two times!
			String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
					+ "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
					+ drive + "\") \n cd.Eject \n cd.Eject ";

			fw.write(vbs);
			fw.close();

			Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获取所有光驱的盘符列表
	public static ArrayList<File> findCDWin32() {
		FileSystemView fsv = FileSystemView.getFileSystemView();

		File[] roots = fsv.getRoots();
		if (roots.length == 1) {
			roots = roots[0].listFiles()[0].listFiles();
		} else {
			System.out.println("I guess you're not on Windows");
			return null;
		}

		ArrayList<File> foundDrives = new ArrayList<File>();
		for (int i = 0; i < roots.length; i++) {
			if (fsv.isDrive(roots[i])) {
				if (fsv.getSystemTypeDescription(roots[i]).indexOf("CD") != -1) {
					foundDrives.add(roots[i]);
				}
			}
		}

		return foundDrives;
	}
	
	public static void main(String[] args) {

		String cdDrive = "";
		
		if(findCDWin32().size() > 0) {
			File file = findCDWin32().toArray(new File[0])[0];
			cdDrive = file.getPath();
		} else {
			return;
		}
		
		// open the cd-rom
		JOptionPane.showConfirmDialog((java.awt.Component) null, "Press OK to open CD", "CDUtils", 
				javax.swing.JOptionPane.DEFAULT_OPTION);

		CDUtils.open(cdDrive);
	}

}
分享到:
评论
2 楼 xiaohuafyle 2014-10-31  
fzfz_902 写道
在windows sever 2008上报错!

报的什么错呀?
1 楼 fzfz_902 2014-10-30  
在windows sever 2008上报错!

相关推荐

Global site tag (gtag.js) - Google Analytics