继Java IO——简介,下面是一个实例代码,功能:向文本文件中添加指定用户、查找是否含有指定用户和删除指定用户。
Java Web项目,请先配置好tomcat~
1. index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>操作文本文件</title>
</head>
<body>
Hello,2018~~~
</body>
</html>
2. servlet
/*
功能:对文本文件username添加指定用户、查找是否含有指定用户和删除指定用户。
*/
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet(name = "fileServlet", urlPatterns = "/fileServlet")
public class myServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取参数
String func = request.getParameter("func");
String username = request.getParameter("username");
//usernameFile文件路径
String usernamePath = "E:\\readWriteFile\\username.txt";
//反馈标记
String addUser = "";
String containUser = "";
String removeUser = "";
if(func!=null && !"".equals(func) && username!=null && !"".equals(username) ) {
//执行增加、检查或删除操作
if ("add".equals(func)){
addUser = doAdd(username, usernamePath);
}else if("check".equals(func)){
containUser = doCheck(username, usernamePath);
}else if("remove".equals(func)){
removeUser = doRemove(username, usernamePath);
}else{
System.out.println("func输入不合法:add/check/remove");
}
}
request.setAttribute("addUser",addUser);
request.setAttribute("containUser",containUser);
request.setAttribute("removeUser",removeUser);
request.getRequestDispatcher("feedback.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
//把username添加到username文件中
private String doAdd(String username, String usernamePath){
String addUser = "";
try {
File usernameFile = new File(usernamePath);
if (usernameFile.isFile() && usernameFile.exists()) { //判断文件是否存在
//读文件
InputStreamReader read = null;
read = new InputStreamReader(new FileInputStream(usernameFile));
BufferedReader br = new BufferedReader(read);
String usernameText;
while ((usernameText = br.readLine()) != null) {
username = usernameText + "\r" +username; //添加要写入的内容
}
br.close();
read.close();
//写文件
OutputStreamWriter write = null;
write = new OutputStreamWriter(new FileOutputStream(usernameFile));
BufferedWriter bw = new BufferedWriter(write);
bw.write(username);
bw.close();
write.close();
addUser = "addSuccess";
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return addUser;
}
//检查username文件中是否含有username
private String doCheck(String username, String usernamePath){
String containUser = "";
try {
File usernameFile = new File(usernamePath);
if (usernameFile.isFile() && usernameFile.exists()) { //判断文件是否存在
//读文件
InputStreamReader read = null;
read = new InputStreamReader(new FileInputStream(usernameFile));
BufferedReader br = new BufferedReader(read);
String usernameText;
while ((usernameText = br.readLine()) != null) {
if(usernameText.contains(username)){ //检查文本
containUser = "contain";
}else{
containUser = "noContain";
}
}
br.close();
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return containUser;
}
//把username从username文件中删除
private String doRemove(String username, String usernamePath){
String removeUser = "removeFailure";
try {
File usernameFile = new File(usernamePath);
if (usernameFile.isFile() && usernameFile.exists()) { //判断文件是否存在
//读文件
InputStreamReader read = null;
read = new InputStreamReader(new FileInputStream(usernameFile));
BufferedReader br = new BufferedReader(read);
String usernameText;
String text = "";
while ((usernameText = br.readLine()) != null) {
if(usernameText.contains(username)){
usernameText.replaceAll(username,""); //把要删除的内容替换成空字符串
removeUser = "removeSuccess";
}else {
text = "".equals(text)==true?usernameText: text + "\r"+ usernameText;
}
}
br.close();
read.close();
//写文件
OutputStreamWriter write = null;
write = new OutputStreamWriter(new FileOutputStream(usernameFile));
BufferedWriter bw = new BufferedWriter(write);
bw.write(text);
bw.close();
write.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return removeUser;
}
}
3. 测试界面feedback.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
String addUser = request.getAttribute("addUser").toString();
String containUser = request.getAttribute("containUser").toString();
String removeUser = request.getAttribute("removeUser").toString();
if (addUser!=null && !"".equals(addUser)){
%>
<p>addUser-----:<%=addUser%></p>
<%
}
if (containUser!=null && !"".equals(containUser)){
%>
<p>containUser-----:<%=containUser%></p>
<%
}
if (removeUser!=null && !"".equals(removeUser)){
%>
<p>removeUser-----:<%=removeUser%></p>
<%
}
%>
</body>
</html>
4. 总结:
1.servlet使用注解,很方便~
2.在浏览器中输入:localhost:8080/servlet的urlPatterns?func=add&&username=xiaoxiao,可以看到相应文件的变化和测试界面的反馈信息,当func=check或remove时会执行相应的查找和删除操作~
3.操作文本文件:
- 写入:读取文本、追加字符串、重新写入
- 查找:读取文本、查找判断
- 删除:读取文本、找到指定字符串、置空、写入内容重构、重新写入