62,614
社区成员
发帖
与我相关
我的任务
分享
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.channels.FileChannel;
public class Test{
public static void main(String[] args)throws Exception{
String content = loadXMLFile("test.xml");
String regex = "(?m)(?!\\s)(?=/>)";
System.out.println("原来内容:");
System.out.println(content);
System.out.println("------------------------------------");
content = content.replaceAll(regex," ");
System.out.println("处理后内容:");
System.out.println(content);
}
private static String loadXMLFile(String source)throws Exception{
Path path = Paths.get(source);
FileChannel channel = (FileChannel)(Files.newByteChannel(path,READ));
int size = (int)(channel.size());
ByteBuffer buf = ByteBuffer.allocate(size);
channel.read(buf);
buf.flip();
Charset charset = Charset.defaultCharset();
CharBuffer charBuf = charset.decode(buf);
return charBuf.toString();
}
}
public class Test{
public static void main(String[] args){
String content = "<column name=\"ID\" alias=\"id\" typeValue=\"-5\" typeName=\"Long\" desc=\"主键ID\" isKey=\"1\"/>";
content = content.replaceAll("(?=/>)"," ");
System.out.println(content);
}
}