读取与截取文件

	public static void main(String[] args) {
		xdd1();
	    	File file=new File("4.txt");                                                                      //关联文件
	 	try {
			FileInputStream f=new FileInputStream("4.txt");                                    //读取关联文件
			int l=(int)file.length();                                                                            //获取文件长度
			byte [] b=new byte[l];                                                                           //新建一个和文件内容长度的数组
			f.read(b);

    		String str=new String(b     ,"GBK");                                                          //转化编码
//    	    System.out.println(new String(b));                                                            //原文件输出
			int c=str.indexOf("<title>印度损招直抵中国咽喉");                                     //截取文件开头
   	    	int c1=str.indexOf("北京出手狠教训");                                                       //截取文件结尾
			String str1=str.substring(c, c1+5);                                                             //给截取的内容赋值为”str1“
		    System.out.println(str1);                                                                            //输出截取内容
//			int d=str.indexOf("<p>  原标题");                                                          //截取文件开头
//    		int d1=str.indexOf("平如何改善。</p>");                                                    //截取文件结尾
//			String str2=str.substring(d, d1+5);                                                             //给截取的内容赋值为”str2“
//          System.out.print(str2);                                                                              //输出截取内容

			
		     } catch (Exception e) {
			
		  	e.printStackTrace();
		  }
		
	}

	public static void xdd1() {
		File file=new File("1.txt");                                                                           //关联文件
		try {
			FileInputStream f=new FileInputStream("1.txt");                                    //读取关联文件
			int l=(int)file.length();                                                                            //获取文件长度
			byte [] b=new byte[l];                                                                           //新建一个和文件内容长度的数组
			f.read(b);

			String str=new String(b     ,"UTF-8");                                                       //转化编码
			int c=str.indexOf("<title>香港各界");                                                        //截取文件开头
    		int c1=str.indexOf("反响热烈|");                                                                //截取文件结尾
			String str1=str.substring(c, c1+4);                                                             //给截取的内容赋值为”str1“
		    System.out.println(str1);                                                                            //输出截取内容
			int d=str.indexOf("<p>央视网消息");                                                         //截取文件开头
    		int d1=str.indexOf("让民众对香港的未来充满信心。</p>");                         //截取文件结尾
			String str2=str.substring(d, d1+14);                                                           //给截取的内容赋值为”str2“
		    System.out.print(str2);                                                                               //输出截取内容

			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}

 

posted @ 2017-07-11 19:39  妤轩  阅读(134)  评论(0编辑  收藏  举报