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

Java利用PushbackReader实现返回对文本中的指定字符串之前的内容

时间:2019-11-17 13:08:53      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:import   src   sre   dex   rar   class   try   new   string   

import java.io.FileReader;
import java.io.PushbackReader;

public class PushbackTest {
    public static void main(String[] args) {
        try(PushbackReader pr = new PushbackReader(new FileReader("src/com/ming/test/PushbackTest.java"),64)){
            char [] buf = new char[32];
            String lastContent = "";
            int hasRead = 0;
            while((hasRead = pr.read(buf)) > 0){
                String content = new String(buf,0,hasRead);
                int targetIndex = 0;
                if((targetIndex = (lastContent + content).indexOf("new PushbackReader")) > 0){
                    pr.unread((lastContent + content).toCharArray());
                    char tmp[] = new char[targetIndex];
                    pr.read(tmp,0,targetIndex);
                    System.out.print(new String(tmp,0,targetIndex));
                    System.exit(0);
                }else{
                    System.out.print(lastContent);
                    lastContent = content;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
输出
package com.ming.test;

import java.io.FileReader;
import java.io.PushbackReader;

public class PushbackTest {
    public static void main(String[] args) {
        try(PushbackReader pr = 

 

Java利用PushbackReader实现返回对文本中的指定字符串之前的内容

标签:import   src   sre   dex   rar   class   try   new   string   

原文地址:https://www.cnblogs.com/chiweiming/p/11875753.html

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