搜索文件夹下的图标 生成Json和CSS 格式文件

来源:互联网 时间:1970-01-01

You will never walk alone.


废话说完后,嗯... 由于有次想大量增加图标,然后要自己去写css? 这样当然太烦了。。程序员就应该偷懒。。所以就写了自动生成的东西~。~

还是把我声明的东西发一下把

 /** 保存json*/ private static JSONArray array = new JSONArray(); /** 用字符串直接拼凑css*/ private static StringBuffer cssStrBuf = new StringBuffer (); /** 图标绝对路径 json用*/ private final static String iconPath = "common/icon/"; /** 图标相对路径 css用*/ private final static String iconCssPath = "../icon/";
声明变量

下面就是读取文件夹下面的图标文件,保存json和css格式 

 1 /** 2 * 读取某个文件夹下的所有图标 3 */ 4 public static void readfile(String filepath) { 5 try { 6 7 JSONObject object = new JSONObject(); 8 File file = new File(filepath); 9 if (!file.isDirectory()) {10 object = new JSONObject();11 object.put("name", file.getName());12 object.put("thumb", iconPath+file.getParentFile().getName()+"/" + file.getName());13 object.put("url", iconPath+file.getParentFile().getName()+"/" + file.getName());14 object.put("size", file.length() + "B");15 array.add(object);16 17 cssStrBuf.append("."+file.getName().substring(0,file.getName().lastIndexOf("."))+"{ background-image: url("+iconCssPath+file.getName()+")!important;} ");18 19 } else if (file.isDirectory()) {//文件夹20 String[] filelist = file.list();21 for (int i = 0; i < filelist.length; i++) {22 File readfile = new File(filepath + File.separatorChar23 + filelist[i]);24 if (!readfile.isDirectory()) {//文件25 object = new JSONObject();26 object.put("name", readfile.getName());27 object.put("thumb", iconPath+readfile.getParentFile().getName()+"/" + readfile.getName());28 object.put("url", iconPath+readfile.getParentFile().getName()+"/" + readfile.getName());29 object.put("size", readfile.length() + "B");30 array.add(object);31 32 String fileName = readfile.getName().substring(0,readfile.getName().lastIndexOf("."));33 cssStrBuf.append("."+fileName+"{ background-image: url("+iconCssPath+readfile.getName()+")!important;} ");34 } else if (readfile.isDirectory()) {//子文件夹35 readfile(filepath + File.separatorChar + filelist[i]);36 }37 }38 39 }40 } catch (RuntimeException e) {41 // TODO Auto-generated catch block42 e.printStackTrace();43 }44 45 }
readfile

看完上面代码。。。无非就是读读文件。很简单东西。。所以没怎么注解了

写文件也上一个,虽然网上已经泛滥

 1 public boolean writeFile(String filePath, String content) { 2 File dirFile = new File(filePath); 3 4 if (!dirFile.exists()) { 5 dirFile.mkdir(); 6 } 7 8 try { 9 //new FileWriter(path + "t.txt", true) 这里加入true 可以不覆盖原有TXT文件内容 续写10 BufferedWriter bw1 = new BufferedWriter(new FileWriter(filePath,11 false));12 bw1.write(content);13 bw1.flush();14 bw1.close();15 return true;16 } catch (IOException e) {17 e.printStackTrace();18 return false;19 }20 }
写入文件

json和css文件经过格式化了。直接生成的没换行的原始格式

 1 { 2 "images" : [{ 3 "name" : "bug.png", 4 "thumb" : "common/icon/bug/bug.png", 5 "url" : "common/icon/bug/bug.png", 6 "size" : "774B" 7 }, { 8 "name" : "bug_add.png", 9 "thumb" : "common/icon/bug/bug_add.png",10 "url" : "common/icon/bug/bug_add.png",11 "size" : "806B"12 }, {13 "name" : "bug_delete.png",14 "thumb" : "common/icon/bug/bug_delete.png",15 "url" : "common/icon/bug/bug_delete.png",16 "size" : "836B"17 }, {18 "name" : "bug_edit.png",19 "thumb" : "common/icon/bug/bug_edit.png",20 "url" : "common/icon/bug/bug_edit.png",21 "size" : "873B"22 }, {23 "name" : "bug_error.png",24 "thumb" : "common/icon/bug/bug_error.png",25 "url" : "common/icon/bug/bug_error.png",26 "size" : "841B"27 }, {28 "name" : "bug_go.png",29 "thumb" : "common/icon/bug/bug_go.png",30 "url" : "common/icon/bug/bug_go.png",31 "size" : "831B"32 }, {33 "name" : "bug_link.png",34 "thumb" : "common/icon/bug/bug_link.png",35 "url" : "common/icon/bug/bug_link.png",36 "size" : "847B"37 }]38 }
json
 1 .bug { 2 background-image: url(../icon/bug.png) !important; 3 } 4 5 .bug_add { 6 background-image: url(../icon/bug_add.png) !important; 7 } 8 9 .bug_delete {10 background-image: url(../icon/bug_delete.png) !important;11 }12 13 .bug_edit {14 background-image: url(../icon/bug_edit.png) !important;15 }16 17 .bug_error {18 background-image: url(../icon/bug_error.png) !important;19 }20 21 .bug_go {22 background-image: url(../icon/bug_go.png) !important;23 }24 25 .bug_link {26 background-image: url(../icon/bug_link.png) !important;27 }
css

收工,还是保持个记录和分享自己的作品(虽然这个东西很烂)把。

 

 

相关阅读:
Top