Java 从带有条件的文本文件中读取
我正在读取一个文本文件,条件是以*开头的单词将被忽略Java 从带有条件的文本文件中读取,java,arrays,string,file,bufferedreader,Java,Arrays,String,File,Bufferedreader,我正在读取一个文本文件,条件是以*开头的单词将被忽略 example: abc 1234 *text to be ignored 所以在这个例子中,我将在读取文本文件时忽略“要忽略的文本”,并且只在字符串数组中存储abc和1234 为此,我编写了以下代码。如何实现忽略以*开头的单词的条件 public static void read(String filename) { BufferedReader reader = null; try {
example:
abc 1234 *text to be ignored
所以在这个例子中,我将在读取文本文件时忽略“要忽略的文本”,并且只在字符串数组中存储abc和1234
为此,我编写了以下代码。如何实现忽略以*开头的单词的条件
public static void read(String filename) {
BufferedReader reader = null;
try {
String line;
reader = new BufferedReader (new FileReader(filename));
while ((line = reader.readLine()) != null) {
String[] functionName = line.split("\\s+");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
startWith(字符串文字)
如果字符串以给定字符串文字开头,则返回true
例如:
if(! word.startsWith("*")) {
// add to what ever you want
}
“1234”。以(“12”)开头代码>返回true
因此,你应该阅读所有单词,检查它是否开始,甚至包含*,如果是,那么忽略整个单词
示例:
if(! word.startsWith("*")) {
// add to what ever you want
}
或
您可以使用类似的substring()
尝试indexOf()
while ((line = reader.readLine()) != null) {
if(line.indexOf("*")>-1)
line=line.substring(0,line.indexOf("*"));
String[] functionName = line.split("\\s+");
}
上面的indexOf(“*”)
将为您提供*
的索引,然后您可以找到带有endIndex
的子字符串作为*
的索引,您可以通过执行子字符串(beginIndex,endIndex)
在while循环中执行类似的操作-
while ((line = reader.readLine()) != null) {
String[] functionName = line.split("\\s+");
String newLine = "";
for(String strg : functionName){
if(strg.startsWith("*")){
break;
}else{
newLine = strg + newLine;
}
}
}
你不知道你使用的是什么版本的Java,所以我假设Java8
注意:代码未经测试,但应进行一些修改
private static final Pattern SPACES = Pattern.compile("\\s+");
private static final Pattern STAR_TO_END = Pattern.compile("\\s*\\*.*");
public static String[] read(final String filename)
{
final Path path = Paths.get(filename);
try (
// UTF-8 by default; excellent
final Stream<String> lines = Files.line(path);
) {
return lines.map(line -> STAR_TO_END.matcher(line).replaceFirst(""))
.flatMap(SPACES::splitAsStream)
.collect(Collectors.toArray(String[]::new));
}
}
private static final Pattern SPACES=Pattern.compile(\\s+);
私有静态最终模式STAR\u TO\u END=Pattern.compile(“\\s*\\\*.”);
公共静态字符串[]读取(最终字符串文件名)
{
最终路径=Path.get(文件名);
试一试(
//默认情况下为UTF-8;非常好
最终流行=Files.line(路径);
) {
返回lines.map(line->STAR\u TO\u END.matcher(line).replaceFirst(“”)
.flatMap(空格::拆分流)
.collect(Collectors.toArray(String[]::new));
}
}
如果不想循环检查单词是否以*
开头,也可以在使用拆分之前删除其中所有带星号的单词
String str = "abc 1234 *text to be ignored";
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
// [abc, 1234, to, be, ignored]
str = "*abc *1234 *text to be *ignored";
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
// [to, be]
正则表达式分解
\\* - Literal match of asterisk
[^\\s]+ - Match anything but a space
\\s* - Capture any or no spaces at end of word
所以,如果你看到一个*
你想从这开始忽略一切吗?剩下的尾随空间呢?另外,您使用的Java版本是什么?我认为,在提供的所有答案中,最好的答案是“如果没有星号怎么办?”@fge OP ding不提这一点,但为了以防万一,只需输入一个if(line.indexOf(“*”)>-1)
我已经更新了答案否,.startsWith()不接受正则表达式作为参数,而是字符串文本。编辑了答案,感谢您让我知道,我不知道。