hdu2066java,[Java] 自动生成visual studio项目文件

visual studio在添加源码的时候只能逐个文件进行添加,有时候很麻烦。于是做了下面这个自动生成visual studio项目文件的工具。

这个工具有什么用?等你哪天想用visual studio看linux kernel代码的时候就知道了。

ProjectCreator.javapackage wsq;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Date;import java.util.Properties;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.filechooser.FileFilter;public class ProjectCreator {public static void main(String[] args) throws IOException {final JFrame frame = new JFrame("ProjectCreator");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600, 400);frame.setLocation(200, 100);frame.getContentPane().setLayout(new java.awt.GridBagLayout());JLabel lb = new JLabel();lb.setText("Select project path:");final JTextField path = new JTextField(20);JButton btnBrows = new JButton("...");btnBrows.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JFileChooser dlg = new JFileChooser(path.getText());FileFilter filter = new FileFilter() {@Overridepublic String getDescription() {return null;}@Overridepublic boolean accept(File f) {return f.isDirectory();}};dlg.setFileFilter(filter);dlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (dlg.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {try {path.setText(dlg.getSelectedFile().getCanonicalPath());} catch (IOException e1) {e1.printStackTrace();}}}});JButton btnRun = new JButton();btnRun.setLocation(60, 60);btnRun.setText("Run");btnRun.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {try {// key functionnew ProjectProc().process(path.getText());JOptionPane.showMessageDialog(frame, "finished");} catch (Exception e) {JOptionPane.showMessageDialog(frame, e.toString());}}});frame.getContentPane().add(lb);frame.getContentPane().add(path);frame.getContentPane().add(btnBrows);frame.getContentPane().add(btnRun);frame.setVisible(true);final Properties prop = new Properties();try {prop.load(new FileReader(ProjectProc.getConfigFileName()));} catch (IOException e) {e.printStackTrace();}path.setText(prop.getProperty("path"));frame.addWindowListener(new WindowListener() {@Overridepublic void windowClosing(WindowEvent arg0) {try {prop.setProperty("path", path.getText());String file = ProjectProc.getConfigFileName();prop.store(new FileWriter(file), (new Date()).toString());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void windowActivated(WindowEvent e) {}@Overridepublic void windowClosed(WindowEvent e) {}@Overridepublic void windowDeactivated(WindowEvent e) {}@Overridepublic void windowDeiconified(WindowEvent e) {}@Overridepublic void windowIconified(WindowEvent e) {}@Overridepublic void windowOpened(WindowEvent e) {}});}}

ProjectProc.javapackage wsq;import java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Collections;import java.util.Comparator;import java.util.LinkedList;import java.util.UUID;public class ProjectProc {class FileComp implements Comparator {@Overridepublic int compare(File o1, File o2) {return o1.getPath().compareTo(o2.getPath());}}static String[] g_ayExt = { "cpp", "h", "cxx", "c", "java", "hpp", "hxx","cs", "php" };static LinkedList g_llExt = new LinkedList();public static String getConfigFileName() {File f = new File("myconfig.conf");String path = f.getAbsolutePath();return path;}private boolean isSourceFile(String path) {if (g_llExt.size() == 0) {for (String e : g_ayExt) {g_llExt.add(e);}}int idx = path.lastIndexOf('.');if (idx <= 0) {return false;}String ext = path.substring(idx + 1);if (g_llExt.contains(ext)) {return true;} else {return false;}}public void process(String path) throws Exception {BufferedReader br = null;FileWriter fw = null;try {String full = "";InputStream templ = getClass().getResourceAsStream("template.vcproj.txt");br = new BufferedReader(new InputStreamReader(templ));while (br.ready()) {full += br.readLine() + "\r\n";}File f = new File(path);path = f.getAbsolutePath();if (!path.endsWith(File.separator)) {path = path + File.separator;}String projName = path.substring(path.lastIndexOf(File.separator, path.length() - 2) + 1,path.length() - 1);// key functionString result = SearchDir(path);result = result.replace(path, "");full = full.replace("{{NAME}}", projName);full = full.replace("{{GUID}}", "{" + UUID.randomUUID().toString()+ "}");full = full.replace("{{FILES}}", result);String resFile = path + projName + ".vcproj";fw = new FileWriter(resFile);fw.write(full.toCharArray());fw.flush();} catch (Exception e) {e.printStackTrace();throw e;} finally {if (br != null) {br.close();}if (fw != null) {fw.close();}}}/** * @param path * 绝对路径 * @return string */private String SearchDir(String path) {if (!path.endsWith(File.separator)) {path = path + File.separator;}String name = path.substring(path.lastIndexOf(File.separator, path.length() - 2) + 1,path.length() - 1);String result = String.format("", name);File dir = new File(path);File[] files = dir.listFiles();if (files == null) {result += "";return result;}LinkedList list = new LinkedList();for (File file : files) {list.add(file);}FileComp c = new FileComp();Collections.sort(list, c);for (File file : list) {if (file.isHidden()) {// do nothing, ignoreSystem.out.println("file is hidden: " + file.getPath());continue;}if (file.getName().startsWith(".")) {// do nothing, ignoreSystem.out.println("file is start with '.': " + file.getPath());continue;}if (file.isDirectory()) {String sub = SearchDir(file.getPath());result += sub;} else {if (isSourceFile(file.getPath())) {String sub = String.format("\r\n",file.getPath());result += sub;}}}result += "";return result;}}

模板文件:template.vcproj.txt<?xml version="1.0" encoding="gb2312"?>{{FILES}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值