问题描述:
由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用。但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生成,但是目前只学过JS,本命语言是面向对象的JAVA。所有用这两种语言都写了一个“获取某个文件夹下的所有图片属性批量生成css样式”。
1.java
1 import java.awt.Image; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 import javax.imageio.ImageIO; 8 9 public class Aa { 10 11 /** 12 * @param args 13 * @throws IOException 14 */ 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 String path = "E:\codes\MyeFitter\trunk\mobile\www\img\icon"; 18 //读取path文件下的整个文件 19 File[] files = new File(path).listFiles(); 20 File file = new File("e:/codes/MyeFitter/utils/llp/scriptbyjava.css"); 21 //如果文件存在,删除该文件 22 if (file.isFile() && file.exists()){ 23 file.delete(); 24 } 25 //创建文件 26 try { 27 file.createNewFile(); 28 } catch (IOException e1) { 29 // TODO Auto-generated catch block 30 e1.printStackTrace(); 31 } 32 for (int i = 0; i < files.length; i++) 33 { 34 String fileName = files[i].getName(); 35 String allPath = path + "\" + fileName; 36 String subFileName = fileName.substring(0, fileName.length() - 4); 37 try { 38 Image image = ImageIO.read(new File(allPath)); 39 40 String all = ".ml-" + subFileName + "{" + Math.round(image.getWidth(null)/2.0) + ";height:" + Math.round(image.getHeight(null)/2.0) + 41 ";background-image:url(../img/icon/" + fileName + 42 ");background-repeat:no-repeat;background-size:cover;background-position:center center;} "; 43 //向文件写入数据 44 FileWriter fw = new FileWriter(file, true); 45 BufferedWriter bw = new BufferedWriter(fw); 46 bw.write(all); 47 bw.flush(); 48 bw.close(); 49 fw.close(); 50 } catch (IOException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 55 } 56 } 57 58 }
2.js
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <script type="text/javascript"> 6 function init(s) 7 { 8 var fso=new ActiveXObject("Scripting.FileSystemObject"); 9 var f1=fso.CreateTextFile("c:\scriptbyjs.css",true); 10 f1.WriteLine(s); 11 f1.WriteBlankLines(1); 12 f1.Close(); 13 } 14 15 function init2(){ 16 var fso = new ActiveXObject("Scripting.FileSystemObject"); 17 // 获取目录下所有文件,对于该浏览器缓存目录,仅能获取到一个文件 18 path = 'E:\codes\MyeFitter\trunk\mobile\www\img\icon'; 19 var fldr = fso.GetFolder(path); 20 var ff = new Enumerator(fldr.Files); 21 var s = ''; 22 for(; !ff.atEnd(); ff.moveNext()){ 23 var fileName = ff.item().Name + ''; 24 fileName = fileName.toLowerCase(); 25 var allpath = path + "\" + fileName; 26 var image = new Image(); 27 var subFileName = fileName.substr(0,fileName.length-4); 28 image.src = allpath; 29 s += '.ml-' + subFileName + '{' + Math.round(image.width/2) + ';height:' + Math.round(image.height/2) + 30 ';background-image:url(../img/icon/' + fileName +');background-repeat:no-repeat;background-size:cover;background-position:center center;} ' 31 } 32 init(s); 33 } 34 </script> 35 </head> 36 <body onload="init2();"> 37 </body> 38 </html>
注意:
Java语言写的可以直接执行就会C盘目录下自动生成.css文件,并且下次执行会覆盖。然后JS语言写的,需要通过浏览器来运行,并且这个只能在ie上运行,因为在body上直接加载,需要允许插件,所以这点有点坑。