1.搭建一个javaweb项目
2.前端用Jsp
3.做一个用户注册界面
姓名
性别
年龄
4.点击提交把页面上的数据写入到一个文件里(文件格式是json)追加不是覆盖
5.求详细代码教程
6.不用连接数据库,直接写入文件里即可
运行效果
* 将字符串追加到文件已有内容后面
*
* @param fileFullPath 文件完整地址:D:/test.txt
* @param content 需要写入的
public static void writeFile(String fileFullPath,String content) {
FileOutputStream fos = null;
try {
//true不覆盖已有内容
fos = new FileOutputStream(fileFullPath, true);
//写入
fos.write(content.getBytes());
// 写入一个换行
fos.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}`
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="java.io.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'other.jsp' starting page</title>
</head>
<body>
<%
File file=new File("C:\\Users\\X550V\\Desktop","cc.txt");
if(!file.exists()){
file.createNewFile();
}
//通过字符输入流读取文件信息
/* char c[]=new char[1];
Reader in=new FileReader(file);
try{
int n=-1;
while((n=in.read(c,0,1))!=-1){
String str=new String(c,0,1);
out.print(str);
}
in.close();
out.print("读取数据成功");
}
catch(Exception e){
out.print(e);
} */
//通过字符输出流写入文件信息
String str="容我三思";
char c[]=str.toCharArray();//String强转为Char型
try{
Writer o=new FileWriter(file,true);
o.write(c);
o.flush();
o.close();
out.print("写入数据成功");
}
catch(IOException e){
out.print(e);
}
%>
</body>
</html>