public static void main(String[] args) throws Exception {
File file = new File( "D:\\Program Files\\Tencent\\QQ\\Bin\\QQ.exe") \\提取图标的应用程序
OutputStream inStream = new FileOutputStream(new File("c:\\45.png"))\\图标保存地址
try {
BufferedImage www = (BufferedImage)((ImageIcon) toIcon(file)).getImage()
ImageIO.write(www, "png", inStream)
inStream.flush()
inStream.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public static Icon toIcon(File file) throws FileNotFoundException {
ShellFolder shellFolder = ShellFolder.getShellFolder(file)
Icon icon = new ImageIcon(shellFolder.getIcon(true))
return icon
}
java可使用FileSystemView和ShellFolder类获取文件的小图标和大图标,以下是详细代码:
import java.awt.FlowLayoutimport java.io.File
import java.io.FileNotFoundException
import javax.swing.Icon
import javax.swing.ImageIcon
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.filechooser.FileSystemView
public class GetFileIcon {
/**
* @param args
*/
public static void main( String[] args )
{
String filePath = "D:/sheet1.xlsx"
File f = new File( filePath )
JFrame frm = new JFrame()
frm.setSize( 300, 200 )
frm.setLocationRelativeTo( null )
frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
frm.setVisible( true )
frm.setLayout( new FlowLayout( 10, 10, FlowLayout.LEADING ) )
JLabel sl = new JLabel( "小图标" )
frm.add( sl )
JLabel bl = new JLabel( "大图标" )
frm.add( bl )
sl.setIcon( getSmallIcon( f ) )
bl.setIcon( getBigIcon( f ) )
}
/**
* 获取小图标
* @param f
* @return
*/
private static Icon getSmallIcon( File f )
{
if ( f != null && f.exists() )
{
FileSystemView fsv = FileSystemView.getFileSystemView()
return(fsv.getSystemIcon( f ) )
}
return(null)
}
/**
* 获取大图标
* @param f
* @return
*/
private static Icon getBigIcon( File f )
{
if ( f != null && f.exists() )
{
try {
sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder( f )
return(new ImageIcon( sf.getIcon( true ) ) )
} catch ( FileNotFoundException e ) {
/* TODO Auto-generated catch block */
e.printStackTrace()
}
}
return(null)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)