java io流读取文件 如何使用IO流读取txt文件指定的文本内容,如txt文件里的内容:456456<--begin-->axlknxkln<--end-->2562,只要读取<--begin-->和<--end-->标记里面的内容axlknxkln,写到另一个文件里。麻烦帮忙使用java实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String s = ""; String mulu = "d:\\123.txt"; FileReader fr = null; String begin = "<--begin-->"; String end = "<--end-->"; try { fr = new FileReader(mulu); BufferedReader br = new BufferedReader(fr); while ((s = br.readLine()) != null) { System.out.println(s.subSequence( s.indexOf(begin) + begin.length(), s.indexOf(end))); } fr.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 使用字节流byte[] b = new byte[1024];不可以使用readLine()一行行的去读的。 不能用readLine() 太奇怪了 String s = ""; String mulu = "d:\\123.txt"; FileReader fr = null; String begin = "<--begin-->"; String end = "<--end-->"; try { File f = new File(mulu); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[1024]; while (fis.read(b) != -1) { s = s + new String(b); while (s.indexOf(begin) != -1) { s = s.substring(s.indexOf(begin) + begin.length()); if (s.indexOf(end) != -1) { System.out.println(s.substring(0, indexOf(end))); } } } fis.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String s = ""; String mulu = "d:\\123.txt"; FileReader fr = null; String begin = "<--begin-->"; String end = "<--end-->"; try { File f = new File(mulu); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[1024]; while (fis.read(b) != -1) { s = s + new String(b); while (s.indexOf(begin) != -1) { s = s.substring(s.indexOf(begin) + begin.length()); if (s.indexOf(end) != -1) { System.out.println(s.substring(0, s.indexOf(end))); } } } fis.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }代码都不能插入了? 奇怪了 java 俄罗斯方块的困扰 Scanner与FileInputStream的效率问题 100分! 挑战你的思维~~ 面向对象的经典问题 --- 如何设计这样一个类描述一辆汽车? ^_^ 请教这段是怎么工作的,, 小弟愚见,一个奇怪的问题 有谁用过datadirect的连接池,感觉性能如何? 如何遍历哈西表? 在插入时如何让另一个字段值为identity的值? 菜鸟手记-----学习java第一天 String 能不能转换为InputStream呢? 把c程序改造成java程序 需要多少时间? 新手求指导,关于线程和流
String mulu = "d:\\123.txt";
FileReader fr = null;
String begin = "<--begin-->";
String end = "<--end-->";
try {
fr = new FileReader(mulu);
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
System.out.println(s.subSequence(
s.indexOf(begin) + begin.length(), s.indexOf(end)));
}
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
不可以使用readLine()一行行的去读的。
String mulu = "d:\\123.txt";
FileReader fr = null;
String begin = "<--begin-->";
String end = "<--end-->";
try {
File f = new File(mulu);
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[1024];
while (fis.read(b) != -1) {
s = s + new String(b);
while (s.indexOf(begin) != -1) {
s = s.substring(s.indexOf(begin) + begin.length());
if (s.indexOf(end) != -1) {
System.out.println(s.substring(0, indexOf(end)));
}
}
}
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String mulu = "d:\\123.txt";
FileReader fr = null;
String begin = "<--begin-->";
String end = "<--end-->";
try {
File f = new File(mulu);
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[1024];
while (fis.read(b) != -1) {
s = s + new String(b);
while (s.indexOf(begin) != -1) {
s = s.substring(s.indexOf(begin) + begin.length());
if (s.indexOf(end) != -1) {
System.out.println(s.substring(0, s.indexOf(end)));
}
}
}
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代码都不能插入了? 奇怪了