Java 将文本文件拆分为空行上的字符串
我想读取一个本地txt文件并读取该文件中的文本。之后,我想将整个文本拆分为字符串,如下面的示例所示 例如: 假设文件包含-Java 将文本文件拆分为空行上的字符串,java,regex,split,Java,Regex,Split,我想读取一个本地txt文件并读取该文件中的文本。之后,我想将整个文本拆分为字符串,如下面的示例所示 例如: 假设文件包含- abcdef ghijkl aededd ededed ededfe efefeef efefeff ...... ...... 我想将此文本拆分为字符串 s1 = abcdef+"\n"+ghijkl; s2 = aededd+"\n"+ededed
abcdef
ghijkl
aededd
ededed
ededfe
efefeef
efefeff
......
......
我想将此文本拆分为字符串
s1 = abcdef+"\n"+ghijkl;
s2 = aededd+"\n"+ededed;
s3 = ededfe+"\n"+efefeef+"\n"+efefeff;
........................
我的意思是我想在空行上拆分文本
我知道怎么读文件。我需要将文本拆分为字符串的帮助您可以通过以下方式将字符串拆分为数组
String.split();
如果你想买新的,那就买新的
String.split("\\n\\n");
更新*
如果我明白你的意思,那么约翰
那么你的代码基本上就是
BufferedReader in
= new BufferedReader(new FileReader("foo.txt"));
List<String> allStrings = new ArrayList<String>();
String str ="";
while(true)
{
String tmp = in.readLine();
if(tmp.isEmpty())
{
if(!str.isEmpty())
{
allStrings.add(str);
}
str= "";
}
else if(tmp==null)
{
break;
}
else
{
if(str.isEmpty())
{
str = tmp;
}
else
{
str += "\\n" + tmp;
}
}
}
BufferedReader中的
=新的BufferedReader(新的文件读取器(“foo.txt”);
List allStrings=new ArrayList();
字符串str=“”;
while(true)
{
字符串tmp=in.readLine();
if(tmp.isEmpty())
{
如果(!str.isEmpty())
{
allStrings.add(str);
}
str=“”;
}
else if(tmp==null)
{
打破
}
其他的
{
if(str.isEmpty())
{
str=tmp;
}
其他的
{
str+=“\\n”+tmp;
}
}
}
可能是您试图解析的内容
其中allStrings是所有字符串的列表。这可能取决于文件的编码方式,因此我可能会执行以下操作:
String.split("(\\n\\r|\\n|\\r){2}");
某些文本文件将换行符编码为“\n\r”,而其他文本文件可能只是“\n”。一行中有两行新行表示有一行空行。即使有用数据之间有两行以上的空行,下面的代码也可以工作
import java.util.regex.*;
// read your file and store it in a string named str_file_data
Pattern p = Pattern.compile("\\n[\\n]+"); /*if your text file has \r\n as the newline character then use Pattern p = Pattern.compile("\\r\\n[\\r\\n]+");*/
String[] result = p.split(str_file_data);
(我没有测试代码,因此可能会出现打字错误。)我建议使用更通用的regexp:
text.split("(?m)^\\s*$");
在这种情况下,它将在任何行尾约定上正常工作,并且也将只处理相同的空行和空白行 戈德温走上了正确的道路,但我认为我们可以把这项工作做得更好。在regx中使用“[]”是一个or,因此在他的示例中,如果您有一个\r\n,那么它将只是一个新行,而不是一个空行。正则表达式将在\r和\n上拆分它,我相信在示例中,我们要查找的是一个空行,它需要a\n\r\n\r\n、a\n\r\n、a\r\n\r\n、a\r\n\r或a\n\n或a\r\n 因此,首先我们要查找\n\r或\r\n两次,两者的任何组合都是可能的
String.split(((\\n\\r)|(\\r\\n)){2}));
下一步,我们需要查找\r但后面没有\n
String.split(\\r{2});
最后,让我们对\n执行相同的操作
String.split(\\n{2});
所有这些都应该是
String.split((\\n\\r)|(\\r\\n)){2}|(\\r){2}|(\\n){2}”)代码>
注意,这只适用于使用新行和字符返回的非常具体的示例。在ruby中,我认为您可以执行以下操作,这将包含更多的情况。我不知道Java中是否有类似的语言
.match($^$)
@Kevin代码运行良好,正如他提到的代码未经测试,以下是所需的3项更改:
1.应首先检查(tmp==null),否则将出现空指针异常
2.此代码省略了添加到ArrayList的最后一组行。为了确保添加最后一个,我们必须在while循环之后包含以下代码:if(!str.isEmpty()){allStrings.add(str);}
3.行str+=“\n”+tmp;如果\\n,则应改为使用\n。请看这个线程的结尾,我已经添加了完整的代码,以便它可以提供帮助
BufferedReader in
= new BufferedReader(new FileReader("foo.txt"));
List<String> allStrings = new ArrayList<String>();
String str ="";
List<String> allStrings = new ArrayList<String>();
String str ="";
while(true)
{
String tmp = in.readLine();
if(tmp==null)
{
break;
}else if(tmp.isEmpty())
{
if(!str.isEmpty())
{
allStrings.add(str);
}
str= "";
}else
{
if(str.isEmpty())
{
str = tmp;
}
else
{
str += "\n" + tmp;
}
}
}
if(!str.isEmpty())
{
allStrings.add(str);
}
BufferedReader中的
=新的BufferedReader(新的文件读取器(“foo.txt”);
List allStrings=new ArrayList();
字符串str=“”;
List allStrings=new ArrayList();
字符串str=“”;
while(true)
{
字符串tmp=in.readLine();
if(tmp==null)
{
打破
}else if(tmp.isEmpty())
{
如果(!str.isEmpty())
{
allStrings.add(str);
}
str=“”;
}否则
{
if(str.isEmpty())
{
str=tmp;
}
其他的
{
str+=“\n”+tmp;
}
}
}
如果(!str.isEmpty())
{
allStrings.add(str);
}
我认为双行的模式应该是\\n\\n。不在新行上我想在空行上拆分字符串,因为字符串可以是任意行数。请查看更新后的问题字符串可以是任意行数。这个答案会起作用,因为它只在找到两个新行(例如空行)时才会拆分。@johnsmith我已经更新了我的答案,但是请注意这是不可测试的。你敢肯定读取整个文件然后拆分这么长的字符串是个好主意吗?我建议你改为逐行阅读,通过\n
将它们附加到StringBuilder
中,直到读到空行。string.split(“\n\r”)
找到此答案此答案在\r\n
上错误拆分,因此不起作用。