直接上代码:
- /**
- * Java 正则表达式驱动,用这种,独立于网络~
- * @param input 旧数据读入文件
- * @param output 新数据写出文件
- */
- public static void divideLogic(File input, File output) {
- String raw = StringFileBridge.file2String(input, "UTF-8");
- Pattern pattern = Pattern.compile("\\{\\s*\\d+\\s*,\\s{0,}\\d+\\s{0,}\\}");
- Matcher matcher = pattern.matcher(raw);
- StringBuffer sb = new StringBuffer();
- while (matcher.find()) {
- String trimed = matcher.group().replaceAll("[\\s*|\\{|\\}]", "");
- String[] strs = trimed.split(",");
- String s1, s2, handledItem;
- s1 = String.valueOf(Integer.parseInt(strs[0]) / 2);
- s2 = String.valueOf(Integer.parseInt(strs[1]) / 2);
- handledItem = "{" + s1 + "," + s2 + "}";
- matcher.appendReplacement(sb, handledItem);
- }
- matcher.appendTail(sb);
- StringFileBridge.string2File(sb.toString(), output);
- }
StringFileBridge.java
- package org.bruce.image.handler.divider;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.UnsupportedEncodingException;
- /**
- * 字符串与文件相互转换工具
- * @author leizhimin 2009-7-14 15:54:18
- */
- public class StringFileBridge {
- /**
- * 读取文件为一个内存字符串,保持文件原有的换行格式
- * @param file 文件对象
- * @param charset 文件字符集编码
- * @return 文件内容的字符串
- */
- public static String file2String(File file, String charset) {
- StringBuffer sb = new StringBuffer();
- try {
- FileInputStream fis = new FileInputStream(file);
- InputStreamReader isr = new InputStreamReader(fis, charset);
- BufferedReader br = new BufferedReader(isr);
- LineNumberReader reader = new LineNumberReader(br);
- String line;
- while ((line = reader.readLine()) != null) {
- sb.append(line).append(System.getProperty("line.separator"));
- }
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- public static String changeEncode(String unicodeStr, String charset) {
- String utf8Str = null;
- try {
- utf8Str = new String(unicodeStr.getBytes(charset));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return utf8Str;
- }
- /**
- * 将字符串存储为一个文件,当文件不存在时候,自动创建该文件,当文件已存在时候,重写文件的内容,特定情况下,还与操作系统的权限有关。
- * @param text 字符串
- * @param distFile 存储的目标文件
- * @return 当存储正确无误时返回true,否则返回false
- */
- public static boolean string2File(String text, File distFile) {
- if (!distFile.getParentFile().exists()) {
- distFile.getParentFile().mkdirs();
- }
- BufferedReader br = null;
- BufferedWriter bw = null;
- boolean flag = true;
- try {
- br = new BufferedReader(new StringReader(text));
- bw = new BufferedWriter(new FileWriter(distFile));
- char buf[] = new char[1024 * 64]; // 字符缓冲区
- int len;
- while ((len = br.read(buf)) != -1) {
- bw.write(buf, 0, len);
- }
- bw.flush();
- br.close();
- bw.close();
- } catch (IOException e) {
- flag = false;
- e.printStackTrace();
- System.out.println("将字符串写入文件发生异常!");
- }
- return flag;
- }
- /**
- * 文件转换为字符串
- * @param in 字节流
- * @param charset 文件的字符集
- * @return 文件内容
- */
- public static String stream2String(InputStream in, String charset) {
- StringBuffer sb = new StringBuffer();
- try {
- Reader reader = new InputStreamReader(in, charset);
- int length = 0;
- for (char[] c = new char[1024]; (length = reader.read(c)) != -1;) {
- sb.append(c, 0, length);
- }
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String x = file2String(new File("/Users/user/Desktop/123.java"), "GBK");
- System.out.println(x);
- boolean b = string2File(x, new File("/Users/user/Desktop/1234.java"));
- System.out.println(b);
- }
- }
输入文件:
blurCloud-hd.plist
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>frames</key>
- <dict>
- <key>blurCloud0.png</key>
- <dict>
- <key>frame</key>
- <string>{{0,8}, {177,92}}</string>
- <key>offset</key>
- <string>{0, 0}</string>
- </dict>
- <key>blurCloud1.png</key>
- <dict>
- <key>frame</key>
- <string>{{185,13}, {137,80}}</string>
- <key>offset</key>
- <string>{0, 0}</string>
- </dict>
- <key>blurCloud2.png</key>
- <dict>
- <key>frame</key>
- <string>{{340,1}, {172,112}}</string>
- <key>offset</key>
- <string>{0, 0}</string>
- </dict>
- </dict>
- <key>metadata</key>
- <dict>
- <key>textureFileName</key>
- <string>blurCloud-hd.pvr.ccz</string>
- <key>format</key>
- <integer>1</integer>
- <key>size</key>
- <string>{512, 313}</string>
- </dict>
- </dict>
- </plist>
输出文件:
blurCloud.plist
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>frames</key>
- <dict>
- <key>blurCloud0.png</key>
- <dict>
- <key>frame</key>
- <string>{{0,4}, {88,46}}</string>
- <key>offset</key>
- <string>{0,0}</string>
- </dict>
- <key>blurCloud1.png</key>
- <dict>
- <key>frame</key>
- <string>{{92,6}, {68,40}}</string>
- <key>offset</key>
- <string>{0,0}</string>
- </dict>
- <key>blurCloud2.png</key>
- <dict>
- <key>frame</key>
- <string>{{170,0}, {86,56}}</string>
- <key>offset</key>
- <string>{0,0}</string>
- </dict>
- </dict>
- <key>metadata</key>
- <dict>
- <key>textureFileName</key>
- <string>blurCloud-hd.pvr.ccz</string>
- <key>format</key>
- <integer>1</integer>
- <key>size</key>
- <string>{256,156}</string>
- </dict>
- </dict>
- </plist>