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