http://www.gotapi.com/rubyrails
http://routes.groovie.org/manual.html#restful-services
吉日的【自言自语】 http://jiri.name
import java.io.StringReader;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
public class CheckUnicode {
/**
* 为了能够正常读取xml文件,把非法字符过滤掉
*
* @param value
* @return
*/
public static StringBuilder CheckUnicodeString(StringBuilder value) {
if (hasLuanMa(value)) {
return CheckUnicodeString2(value);
} else {
return value;
}
}
private static boolean hasLuanMa(StringBuilder value) {
boolean hasLuanMa = false;
if (null != value && value.length() > 0) {
for (int i = 0; i < value.length(); ++i) {
if (value.charAt(i) > 0xFFFD
|| (value.charAt(i) < 0x20 && value.charAt(i) != '\t'
&& value.charAt(i) != '\n' && value.charAt(i) != '\r')) {
hasLuanMa = true;
break;
}
}
return hasLuanMa;
} else {
return false;
}
}
private static StringBuilder CheckUnicodeString2(StringBuilder value) {
StringBuilder sb = new StringBuilder();
if (null != value && value.length() > 0) {
for (int i = 0; i < value.length(); ++i) {
if (value.charAt(i) > 0xFFFD
|| (value.charAt(i) < 0x20 && value.charAt(i) != '\t'
&& value.charAt(i) != '\n' && value.charAt(i) != '\r')) {
continue;
} else {
sb.append(value.charAt(i));
}
}
return sb;
} else {
return sb;
}
}
/**
* 传进来已经过滤过的xml字符串
*
* @param xml
* @return
*/
public static boolean loadXmlString(String xml) {
try {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("从xml文件读具体内容");
StringBuilder sbEnd = CheckUnicodeString(sb);
if (loadXmlString(sbEnd.toString())) {
System.out.println("OK");
} else
System.out.println("FAIL");
}
}
import java.io.File;
import java.io.FileInputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class NTest {
private StringBuilder content = new StringBuilder();
/**
* 空行正则里是\n\r
* 测试文件内容为
a
b
c
d
e
*/
private Pattern pattern = Pattern.compile("(.*?)\n\r", Pattern.DOTALL);
@Before
public void setUp() throws Exception {
FileInputStream fis= null;
try {
fis = new FileInputStream(new File("c:/test.txt"));
byte[] b = new byte[1024];
int len = -1;
while ((len = fis.read(b)) != -1) {
content.append(new String(b, 0, len));
}
} finally {
if(null != fis)
fis.close();
}
}
@After
public void tearDown() throws Exception {
content = null;
}
@Test
public void Ntest() {
Matcher matcher = pattern.matcher(content);
int g = matcher.groupCount();
System.out.println("groupCount="+g);
while(matcher.find()){
System.out.println("finded it");
for (int i = 0; i < g ;i++){
System.out.println(matcher.group(i));
}
break;
}
}
}
运行结果为:
groupCount=1
finded it
------
a
b