请问java中如何从一个文件中的读取一个已确定存在的字符串后面的字符 比如说一个文件中以确定存在一个单词,我想读取该单词后面的字符,请问java中该如何操作? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String str="asdfcomasdf";//读取com后面的字符串str=str.subString(str.indexOf("com"),str.length()); String str="hello world,world peace,world";//读取com后面的字符串 String key = "world"; int pos = -key.length(); while((pos=str.indexOf("world", pos+key.length())) != -1){ if(pos+key.length()<str.length()) System.out.println(str.charAt(pos+key.length())); else if(pos+key.length()==str.length()) System.out.println("文件尾"); } public class Test { public static void main(String[] args) throws IOException { File file = new File("1.txt"); FileReader fr = new FileReader(file); char buf; boolean locate = false; String match = "world"; char[] pattern = match.toCharArray(); StringBuilder sb = new StringBuilder(); while(fr.ready()) { buf = (char)fr.read(); if(!locate && buf == pattern[0]) { locate = testChar(buf,fr, pattern,0); if(fr.ready()) buf = (char)fr.read(); else break; } if(locate) sb.append(buf); } System.out.println(sb.toString()); fr.close(); } public static boolean testChar(char now,FileReader fr,char[] pattern,int pos) throws IOException { if(pos == pattern.length - 1) return true; if(fr.ready()) { if(now == pattern[pos]) return testChar((char)fr.read(),fr, pattern, pos+1); else return false; } else { return false; } }}测试文件hello worldthis is my testhelloend 自己玩的话一行一行indexof就行了考虑多的话比如说单词可以跨行,又或者你要寻找的文件比较大确定的字符串又贼长可以再考虑下性能问题用kmp或类似的东西来搞ps:indexof我记得上直接找的 java 列表控件 不知道是否有开源的OA系统 初学JAVA,关于JDBC连接MYSQL的问题,请帮忙看看 关于面向对象的多态性问题 一个关于Observable的代码,求助 绝对高手进>++++>不利用oracle管理平台 也不能在cmd下输入命令 在应用程序里 如何实现数据库的备份与恢复 请问如何获取某个class对应的存储路径? 有关STATIC的问题 带包名 继承 error: cannot find symbol java如何实现利用选项卡进行表格操作 求解:Java里的接口(interface)的作用,什么情况下会用到接口 关于java子类继承父类的方法的细节问题
String str="asdfcomasdf";//读取com后面的字符串
str=str.subString(str.indexOf("com"),str.length());
String key = "world";
int pos = -key.length();
while((pos=str.indexOf("world", pos+key.length())) != -1){
if(pos+key.length()<str.length())
System.out.println(str.charAt(pos+key.length()));
else if(pos+key.length()==str.length())
System.out.println("文件尾");
}
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("1.txt");
FileReader fr = new FileReader(file);
char buf;
boolean locate = false;
String match = "world";
char[] pattern = match.toCharArray();
StringBuilder sb = new StringBuilder();
while(fr.ready()) {
buf = (char)fr.read();
if(!locate && buf == pattern[0]) {
locate = testChar(buf,fr, pattern,0);
if(fr.ready())
buf = (char)fr.read();
else
break;
}
if(locate)
sb.append(buf);
}
System.out.println(sb.toString());
fr.close();
}
public static boolean testChar(char now,FileReader fr,char[] pattern,int pos) throws IOException {
if(pos == pattern.length - 1)
return true;
if(fr.ready()) {
if(now == pattern[pos])
return testChar((char)fr.read(),fr, pattern, pos+1);
else
return false;
} else {
return false;
}
}
}
测试文件hello world
this is my test
hello
end
一行一行indexof就行了
考虑多的话
比如说单词可以跨行,又或者你要寻找的文件比较大
确定的字符串又贼长
可以再考虑下性能问题
用kmp或类似的东西来搞
ps:
indexof我记得上直接找的