Java用分隔符读取大文本文件
我正在尝试读取以下形式的大文本文件:
I'm trying to read a large text file in the form of:
datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+
我想在文本文件中将此字符串作为一个大的Java字符串读取.这可能吗?我知道使用split方法.
I want to read this string in the text file as one big java String. Is this possible? I know the use of the split method.
它可以逐行读取它,但我真正需要的是在"+"号处分割此长文本字符串.之后,我想将其存储为数组,arraylist,列表,...
It worked to read it line by line, but what I really need is to split this long text-string at the '+' sign. Afterwards I want to store it as an array, arraylist, list,...
有人可以帮我吗?因为Internet上的所有信息都只是关于逐行读取文件的信息. 预先感谢!
Can anyone help me with this? Because every information on the internet is just about reading a file line by line. Thanks in advance!
您可以使用BufferedReader
或任何IO类读取文件.假设您在testing.txt
文件中具有该String,然后可以从文件中读取每一行用分隔符(+
)分割它.并遍历数组并打印.
You can read file using BufferedReader
or any IO-classes.suppose you have that String in testing.txt
file then by reading each line from file you can split it by separator (+
). and iterate over array and print.
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));//file name with path
while ((sCurrentLine = br.readLine()) != null) {
String[] strArr = sCurrentLine.split("\\+");
for(String str:strArr){
System.out.println(str);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}