如何用java分割这段文本
答案:2 悬赏:0 手机版
解决时间 2021-01-26 01:50
- 提问者网友:咪咪
- 2021-01-25 21:46
例如说这句话,if m>=n,把它分割成if , m , >= , n。应如何分割,正则表达式我又没时间学了,请高手帮忙
最佳答案
- 五星知识达人网友:鱼芗
- 2021-01-25 22:43
Pattern pattern = Pattern.compile("(\\w+)(\\W*)");
Matcher matcher = pattern.matcher("if m>=n");
while(matcher.find())
{
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
如果你不要空格符的话,判断一下matcher.group(2),是空格就不要输出就行。
希望能帮到你!
Matcher matcher = pattern.matcher("if m>=n");
while(matcher.find())
{
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
如果你不要空格符的话,判断一下matcher.group(2),是空格就不要输出就行。
希望能帮到你!
全部回答
- 1楼网友:躲不过心动
- 2021-01-25 23:04
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; public class filecutter { public static void cuttomorefile(string sourcefile, string targetdirectory, string prefix, int size) { //加载源文件 file source = new file(sourcefile); inputstream in = null; outputstream out = null; int len = 0; int fileindex = 1; //设置一次加载的大小 byte[] buffer = new byte[2048]; try { //把源文件读到inputstream中 in = new fileinputstream(source); //循环 while(true) { //分割后的文件流 out = new fileoutputstream(targetdirectory + file.separator + prefix + fileindex++ + ".txt"); for(int i = 0; i < size; i++) { //如果文件读取完就退回方法。 if((len = in.read(buffer)) != -1) { //写入分割后的文件 out.write(buffer, 0, len); }else { //执行finally内容后,退出方法 return; } } } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }finally { try { //关系流 in.close(); out.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯