看javadoc中java.io部分,写得很清楚,对你有好处。
解决方案 »
- java引用其他工程的问题
- Spring3MVC怎么获取上下文路径?
- sql语句出错
- FCKeditor的问题!!!!!!!!!!!
- JSP+SERVLET+JAVABEAN+STRUTS+HIBERNENAT+SPRING面试
- ie中显示excel的乱码问题!!!
- struts错误: Cannot find ActionMappings or ActionFormBeans collection
- jboss在eclipse中的问题,不知道怎么解决了
- 代码运行报错,求大神指点
- 比较4种数据库驱动性能
- 誰有Jreport 的下載地址及license Key , 給我一個,不勝感激!
- jsp连接Sql Server7.0/2000数据库
import java.net.*;
import java.util.*;
import java.text.*; InetAddress addresses = InetAddress.getLocalHost();
StringBuffer sb_file = new StringBuffer(pathstr);
sb_file.append("test");文件名称
sb_file.append(".txt");
FileOutputStream fos = new FileOutputStream(sb_file.toString(), true);
StringBuffer sb_context = new StringBuffer("\r\n");
sb_context.append("写注册表");
sb_context.append("; ");
sb_context.append(";\r\n");
追加数据到文件:
package com;
import java.io.*;
public class WriteAppend {
private String path;//文件路径
private String something;//追加的字符串变量
//初始化
public WriteAppend() {
path = null;
something = "Default message";
}
//设置文件路径
public void setPath(String apath) {
path = apath;
}
//得到文件路径
public String getPath() {
return path;
}
//设置要追加的字符串
public void setSomething(String asomething) {
something = asomething;
}
//得到要追加的字符串
public String getSomething() {
return something;
}
//追加字符串
public String writeSomething() {
try {
//创建文件path并写入something字符串,注意和写入篇的区别
FileWriter theFile = new FileWriter(path,true);
PrintWriter out = new PrintWriter(theFile);
out.print(something + "\n");
out.close();
//关闭文件并返回success字符串
theFile.close();
return "success!!";
} catch (IOException e) {
return e.toString();
}
}
}
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>追加数据</title>
</head>
<body>
<%--创建javabean并设置属性 --%>
<jsp:useBean id="writer" class="com.WriteAppend" scope="request">
<jsp:setProperty name="writer" property="path" value="c:\\writefile.txt" />
<jsp:setProperty name="writer" property="something" value="初始化something属性" />
</jsp:useBean>
<h3>追加数据</h3>
<p>
<%--设置要追加的字符串 --%>
<% writer.setSomething("这是追加的内容"); %>
<%--读取上面设置的字符串 --%>
<% out.print(writer.getSomething()); %>
<%--调用writer的writeSomething方法追加文件并返回成功或者出错信息 --%>
<% out.print(writer.writeSomething()); %>
</p>
</body>
</html>