基于java的小程序单位转换(px,rpx)


//替换文本
public class ItemFileReplace {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File f = new File("D:\\work\\democratic\\wintest-web");
		meth3(f);

	}

	/**
	 * 遍历删除文件夹
	 * @param file
	 */
	private static void deleteFile(File file) {
		if (file.exists()) {//判断文件是否存在
			if (file.isFile()) {//判断是否是文件
				file.delete();//删除文件
			} else if (file.isDirectory()) {//否则如果它是一个目录
				File[] files = file.listFiles();//声明目录下所有的文件 files[];
				for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件
					deleteFile(files[i]);//把每个文件用这个方法进行迭代
				}
				if(files.length == 0){
					file.delete();//删除空文件夹
				}
			}
		} else {
			System.out.println("所删除的文件不存在");
		}
	}
	//
	private static void meth3(File f) {
		// 方法:3:利用文件构造器
		File[] file =f.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				String fileName = pathname.getName().toLowerCase();
				if(fileName.endsWith(".wxss")){
					return true;
				}else if(fileName.endsWith(".wxml")){
					return true;
				}else if(pathname.isDirectory()){
					meth3(pathname);
				}
				return false;
			}
		});
		for (File t:file) {
			//没找到则不处理
			boolean isEdit = false;
			try{
				BufferedReader in = new BufferedReader(new FileReader(t));

			    String outPath =  	t.getPath().replace("D:","E:").replace("\\","\\\\");
				File filewriter = new File(outPath);
				if(!filewriter.exists()){
					filewriter.getParentFile().mkdirs();
				}
				filewriter.createNewFile();

				// write
				FileWriter fw = new FileWriter(filewriter);
			    BufferedWriter out =new BufferedWriter(fw);

				String s = null;
				while((s= in.readLine()) != null){
					boolean isPrint = true;
					Pattern p = Pattern.compile("(\\d*|\\d*(\\.\\d*))px");//正则表达式,取=和|之间的字符串,不包括=和|
					Matcher m = p.matcher(s);


					while(m.find()) {
						//第一步进来打印
						if(isPrint){
							System.out.println(s);
						}

						isPrint =false;
						String px = m.group(0).replace("px","");
						if(!px.equals("")){
							isEdit = true;
							Double iPx = Double.parseDouble(px);
							String rpx= "1";
							//为1不处理
							if(iPx!=1 && iPx != 2){
							 BigDecimal bPx = new BigDecimal(iPx);
							 BigDecimal company = new BigDecimal(2.34);
							 iPx = bPx.multiply(company).doubleValue();
							 DecimalFormat decimalFormat = new DecimalFormat("0.##");
							 String sD = decimalFormat.format(iPx);

							 rpx = sD;
							}
							s = s.replace(px+"px",rpx+"rpx");
							System.out.println("px="+px+" "+"rpx="+ rpx);//m.group(1)不包括这两个字符
						}
					}
					out.write(s);
					out.newLine();
				}
				//没找到则不处理
				if(isEdit){
					out.flush();
				}else{
					out.close();
					filewriter.delete();
					//deleteFile(filewriter.getParentFile());
				}
				in.close();
				out.close();
			}catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		
	}

	
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值