首页 > 其他 > 详细

利用PushbackReader读取文件中某个字符串之前的内容

时间:2015-09-05 22:18:43      阅读:182      评论:0      收藏:0      [点我收藏+]
package File;

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


/*读取文件中某个字符串之前的文件*/
//PushbackInputStream,PushbackReader应用


public class PushbackTest {
	public static void main(String[] args) {
		try(PushbackReader pr = new PushbackReader(new FileReader(
								"./src/File/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());
					
					if(targetIndex>32)
					{
						buf = new char[targetIndex];
					}
					pr.read(buf,0,targetIndex);
					System.out.println(new String(buf,0,targetIndex));
					System.exit(0);
				}
				else
				{
					System.out.println(lastContent);
					lastContent = content;
				}
			}
		}catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}

 

利用PushbackReader读取文件中某个字符串之前的内容

原文:http://www.cnblogs.com/masterlibin/p/4783996.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!