Java 读取文件并在查找分隔符时拆分其内容
我的文件内容如下:Java 读取文件并在查找分隔符时拆分其内容,java,Java,我的文件内容如下: nellkb:company_dc rdfs:label "dC" "WASHINGTON" , "Washington" ; skos:prefLabel "www.wikipedia.com" . nellkb:politicsblog_quami_ekta rdfs:label "Quami Ekta" ; skos:prefLabel "Quami Ekta" . nellkb:female_ramendra_kumar rdfs:label "
nellkb:company_dc
rdfs:label "dC" "WASHINGTON" , "Washington" ;
skos:prefLabel "www.wikipedia.com" .
nellkb:politicsblog_quami_ekta
rdfs:label "Quami Ekta" ;
skos:prefLabel "Quami Ekta" .
nellkb:female_ramendra_kumar
rdfs:label "Ramendra Kumar" ;
skos:prefLabel "Ramendra Kumar" .
我需要在分隔符“.”处拆分文件,并将之前的内容保存为字符串。我该怎么做?我尝试了以下方法,但不起作用
try {
String sCurrentLine = null;
int i = 0;
br = new BufferedReader(new FileReader(rdfInstanceFile));
while ((sCurrentLine = br.readLine()) != null) {
splitted = sCurrentLine.split(".");
}
} catch (IOException e) {
e.printStackTrace();
}
替换
splitted = sCurrentLine.split(".");
与
编辑
它会工作。首先将文件内容读入字符串,拆分字符串并保存在字符串数组中
try {
String sCurrentLine = "";
StringBuilder content = new StringBuilder();
String splitted[]= null;
int i = 0;
br = new BufferedReader(new FileReader(rdfInstanceFile));
while ((sCurrentLine = br.readLine()) != null) {
content.append(sCurrentLine) ;
}
splitted = content.toString().split("\\.");
} catch (IOException e) {
e.printStackTrace();
}
使用该类。这个场景非常适合它。您所需要做的就是指定“\\.”分隔符
没有必要构建一个字符串,然后将其拆分
导入java.io.InputStream;
导入java.util.Scanner;
公共类扫描文件{
公共静态无效字符串[]args{
试一试{
InputStream is=ScanFile.class.getClassLoader.getResourceAsStreamresources/foo.txt;
扫描仪扫描=新扫描仪;
scan.useDelimiter\\.[\r\n]+;//在点处标记。后跟CR/LF。
int i=1;
而scan.hasNext{
字符串行=scan.next.trim;
System.out.printfLine%d%n---%n%n%s%n%n,i++,行;
}
扫描。关闭;
接近;
}捕获异常e{
e、 打印跟踪;
}
}
}
输出
补充资料
公共扫描程序useDelimiterString模式
将此扫描仪的定界模式设置为由指定字符串构造的模式
此方法的调用形式为useDelimiterpattern,其行为方式与调用useDelimiterpattern.compilepattern的方式完全相同
调用reset方法会将扫描仪的分隔符设置为默认值
参数:
模式-指定定界模式的字符串
返回:
这个扫描仪
Scanner构造函数接受6种不同类型的对象:File、InputStream、Path、Readable、ReadableByteChannel和String
高级解决方案
不能将其设置为扫描仪对象上的分隔符吗?不确定拆分的是什么,但在while循环的每次迭代中,其值都将被替换。如果这是不需要的,考虑使用一个ARARYLIST。不起作用..再试一次您应该使用StringBuilder而不是字符串连接。这不起作用-他正在使用readLine,但他需要在输出中将3行放在一起。整个方法都失败了。如果我们在文件中有一个源数据,比如www.google.fr,该怎么办?谢谢你的文件名是www.google.fr,或者你必须从link获取它??它工作了。然而,在我的文件中,我可以有数据来源,比如www.google.com。在这种情况下,我们在useDelimiter中使用什么?如果我们想存储这些数据以便以后使用,我们将使用什么?我想不是一个Arraylist吧?将这些行存储在StringGreat类型的列表中!再次感谢!!
String sCurrentLine = null;
int i = 0;
br = new BufferedReader(new FileReader(rdfInstanceFile));
StringBuilder content = new StringBuilder();
while ((sCurrentLine = br.readLine()) != null) {
content.append(sCurrentLine);
}
splitted = content.toString().split("\\.");
try {
String sCurrentLine = "";
StringBuilder content = new StringBuilder();
String splitted[]= null;
int i = 0;
br = new BufferedReader(new FileReader(rdfInstanceFile));
while ((sCurrentLine = br.readLine()) != null) {
content.append(sCurrentLine) ;
}
splitted = content.toString().split("\\.");
} catch (IOException e) {
e.printStackTrace();
}
Line #1
-------
nellkb:company_dc
rdfs:label "dC" "WASHINGTON" , "Washington" ;
skos:prefLabel "WASHINGTON"
Line #2
-------
nellkb:politicsblog_quami_ekta
rdfs:label "Quami Ekta" ;
skos:prefLabel "Quami Ekta"
Line #3
-------
nellkb:female_ramendra_kumar
rdfs:label "Ramendra Kumar" ;
skos:prefLabel "Ramendra Kumar"
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(File source)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(File source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(InputStream source)
// Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(InputStream source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(Path source)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(Path source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified source.
Scanner(Readable source)
// Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(ReadableByteChannel source)
// Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(ReadableByteChannel source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified string.
Scanner(String source)
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ScanFile {
private static ClassLoader loader = ScanFile.class.getClassLoader();
private static interface LineProcessor {
void process(String line);
}
private static interface Reader<T> {
T read(String resource, String delimiter) throws IOException;
void flush();
}
private abstract static class FileScanner<T> implements Reader<T> {
private LineProcessor processor;
public void setProcessor(LineProcessor processor) {
this.processor = processor;
}
public T read(Scanner scan, String delimiter, boolean close) throws IOException {
scan.useDelimiter(delimiter);
while (scan.hasNext()) {
processor.process(scan.next().trim());
}
if (close) {
scan.close();
}
return null;
}
public T read(InputStream is, String delimiter, boolean close) throws IOException {
T t = read(new Scanner(is), delimiter, true);
if (close) {
is.close();
}
return t;
}
public T read(String resource, String delimiter) throws IOException {
return read(loader.getResourceAsStream("resources/" + resource), delimiter, true);
}
}
public static class FileTokenizer extends FileScanner<List<String>> {
private List<String> tokens;
public List<String> getTokens() {
return tokens;
}
public FileTokenizer() {
super();
tokens = new ArrayList<String>();
setProcessor(new LineProcessor() {
@Override
public void process(String token) {
tokens.add(token);
}
});
}
public List<String> read(Scanner scan, String delimiter, boolean close) throws IOException {
super.read(scan, delimiter, close);
return tokens;
}
@Override
public void flush() {
tokens.clear();
}
}
public static void main(String[] args) {
try {
FileTokenizer scanner = new FileTokenizer();
List<String> items = scanner.read("foo.txt", "\\.[\r\n]+");
for (int i = 0; i < items.size(); i++) {
System.out.printf("Line #%d%n-------%n%n%s%n%n", i + 1, items.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}