我曾试过将变量newstext清空不过没解决问题,请大家指点下
解决方案 »
- 关于apache+tomcat负载均衡遇到的问题
- Tomcat报错,急需高手指点
- SSH架构的分页的代码(在线等)
- Make sure db.properties is in the CLASSPATH是怎么回事啊?怎么解决啊?
- JSTL <sql:query>
- 不知道这种做法怎么样?
- 各位高手出来帮帮小弟吧,这段程序我也不知道问题在哪?谢谢了!!!
- jsp中The server encountered an internal error () that prevented it from fulfilling this request.是什么错误
- Struts1 后台怎么获取不到前台的值
- 求救,java如何调取outlook会议室信息
- 请问:useBean 的id能动态给吗?
- 时间处理问题!急!
{
newstext=new StringBuffer();
}
来清空也试过其他办法来清空,虽然能得到正确的i的值但是到newstext=new StringBuffer(new String(buff1))还是在后面追加了前一文件的内容,请大家能否说详细点
public static String gettext()
{
StringBuffer sb = new StringBuffer();
try
{
FileInputStream news = new FileInputStream(path);
int i= news.read(buff1);
if (i != -1)
{
//newstext=new StringBuffer(new String(buff1));
sb=sb.append(new String(buff1));
}
news.close();
//String text = new String(newstext);
//return text;
return sb.toString();
}
catch (Exception e)
{
System.out.println(e);
}
return null;
}
* news
* @author
* @version 1.1
*/
import java.io.*;public class News {
private String path; private StringBuffer newstext = new StringBuffer(); byte buff1[]; public void setpath(String path) {
this.path = path;
} public String getpath() {
return path;
} public String gettext() {
try {
buff1 = new byte[1000];
FileInputStream news = new FileInputStream(path);
int i = news.read(buff1);
if (i != -1) {
newstext = new StringBuffer(new String(buff1));
}
news.close();
String text = new String(newstext);
return text;
} catch (Exception e) {
System.out.println(e);
}
return null;
} public static void main(String args[]) {
News news = new News();
news.setpath("c:\\1.txt");
System.out.println("开始:" + news.gettext());
news.setpath("c:\\2.txt");
System.out.println("结束:" + news.gettext());
}
}试试看,呵呵,我可以了