【JSP开源代码栏目提醒】:网学会员,鉴于大家对JSP开源代码十分关注,论文会员在此为大家搜集整理了“ex9_18.jsp”一文,供大家参考学习!
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!--随机文件读写-->
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>JSP通用范例大全</title>
</head>
<body>
<center><font size=5>随机文件读写</font></center>
<br><hr><br>
<form name="chat" method="post" action="ex9_18.jsp">
<input type="text" name="content" value="">
<input type="submit" name="Submit" value="提交">
</form>
<br>
<%
request.setCharacterEncoding("GB2312");
String content=request.getParameter("content");
if(content!=null){
File file=new File(request.getRealPath("/")+"chat.txt");
if(!file.exists()){
file.createNewFile();
}
RandomAccessFile random=new RandomAccessFile(file,"rw");
random.seek(1000);
random.write(content.getBytes());
byte[] msg=new byte[100];
random.seek(1000);
int count=random.read(msg);
String str=new String(msg,0,count);
out.print("写入文件的内容是:"+str);
}
%>
</body>
</html>