直接上代碼:
[java] /**
* 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);
}
/**
* 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
[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);
}
}
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
[html] <?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>
<?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>
輸出文件:
blueCloud.plist
[html] <?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>
<?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>
摘自 yang3wei的專欄