小说按章节切割

 public void cut(String srcFilePath,String dstDir,String code){
	        BufferedReader br=null;
	        BufferedWriter bw=null;
	        try {
	             br=new BufferedReader(new InputStreamReader(new FileInputStream(srcFilePath),code));  
	            String tempString=null;
	            StringBuffer sb=new StringBuffer();
	            Pattern p=Pattern.compile("第.*章");
	            Matcher m=null;
	            Integer index=0;
	            int i=0;
	            File file;
	            while((tempString=br.readLine())!=null){
	                tempString+="\r\n";
	                sb.append(tempString);
	            }
	            m=p.matcher(sb);
	            while(m.find()){
	            	 i++;
	                file=new File(dstDir + File.separator + i +".txt");
	                bw=new BufferedWriter(new FileWriter(file));
	                int start = m.start();
	                if(m.find()){
	                    index=m.start();
	                    bw.write(sb.toString(),start, index-start);
	                    bw.flush();
	                    m.region(index,sb.length());
	                }else{
	                    bw.write(sb.toString(),start, sb.length()-start);
	                }
	            }
	        } catch (FileNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        catch(IOException ie){
	            ie.printStackTrace();
	        }
	        finally{
	            try {
	                if(br!=null){
	                    br.close();
	                }
	                if(bw!=null){
	                    bw.close();
	                }
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        }
	        System.out.println("========END=======");
	    }


转载于:https://my.oschina.net/alanfans/blog/523939

你可能感兴趣的:(小说按章节切割)