码迷,mamicode.com
首页 > 编程语言 > 详细

java将独到的指定文件截取并写入到文件中

时间:2017-07-13 20:30:45      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:写入   ring   writer   string类   replace   throw   stringbu   部门   close   

将保存的某些文件选取其中想类似的部门使用String类中的方法将其截取并写入到指定文件中

public class Te3 {
void read(String src,String start,String end) throws Exception{
FileInputStream a = new FileInputStream(src);
byte[] b = new byte[1500];
StringBuffer s_1 = new StringBuffer();
int len = 0;
//读取文件
while(len != -1){
len = a.read(b);
String s = new String(b,"UTF-8");
s_1.append(s);//追加

}
a.close();

//截取主要部分
int i = s_1.indexOf(start);//定位
int j = s_1.indexOf(end);
String s_2 = s_1.substring(i, j+1);//截取
// System.out.println(s_2);
String str1 = "<p>";
String str2 = "</p>";
//替换
String s_3 = s_2.replace(str1," ");
String s_4 = s_3.replace(str2, " ");
// System.out.println(s_4);

//删除定位

StringBuffer s2 = new StringBuffer();
s2.append(s_4);//s2追加s_4
String str3 = "<";//指定定位的字符串
String Str4 = ">";
int i1 = s2.indexOf(str3);//指定字符的位置
int j1 = s2.indexOf(Str4);
s2.delete(i1, j1+1);//截取数加1,那么最后的<就可以截掉
System.out.println(s2);
FileWriter w = new FileWriter("E:/java/qwer/xxxx.txt");
String s3 = s2.toString();

w.flush();
w.close();




//



}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Te3 t = new Te3();
t.read("E:/java/qwer/火箭.html", "<p>  今", "博 </p>");
t.read("E:/java/qwer/无人机.html", "<p>  通", "博 </p>");
t.read("E:/java/qwer/洪涝.html", "<p>  央", "博 </p>");
t.read("E:/java/qwer/莫斯科.html", "<p>  央","琪 </p>");
t.read("E:/java/qwer/李克强.html", "<p>  原", "博 </p>");
}

}

java将独到的指定文件截取并写入到文件中

标签:写入   ring   writer   string类   replace   throw   stringbu   部门   close   

原文地址:http://www.cnblogs.com/w7589/p/7162202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!