当前位置: 代码迷 >> Java Web开发 >> java读取txt的有关问题,
  详细解决方案

java读取txt的有关问题,

热度:209   发布时间:2016-04-17 00:50:17.0
java读取txt的问题,急!!!!!!!!!!
我用java操作txt,需要在文本中记录用户名和密码,一行对应一个用户信息。
以下代码是注册部分,在servlet中每次注册都会重复的把信息添加到txt中,而类中就没问题,请问各位这是怎么回事,急啊。。。
java类的代码如下
Java code
package com.feijoy.wap.tools;import java.io.*;public class Txt {    public static BufferedReader bufread;    //指定文件路径和名称    private static String path = "F:/AllUserInfo.txt";    private static  File filename = new File(path);    private static String readStr ="";        /**     * 创建文本文件.     * @throws IOException      *      */    public static void creatTxtFile() throws IOException{        if (!filename.exists()) {            filename.createNewFile();        }    }    /**     * 删除文本文件.     * @throws IOException      *      */    public static void deleteTxtFile() throws IOException{        if (filename.exists()) {            filename.delete();        }    }    /**     * 读取文本文件.     *      */    public static String readTxtFile(){        String read;        FileReader fileread;        try {            fileread = new FileReader(filename);            bufread = new BufferedReader(fileread);            try {                while ((read = bufread.readLine()) != null) {                    readStr = readStr + read+ "\r\n";                }            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }        return readStr;    }    public static String readTxtInfo(){        String read;        FileReader fileread;        File filename = new File("F:/info.txt");        try {            fileread = new FileReader(filename);            bufread = new BufferedReader(fileread);            try {                while ((read = bufread.readLine()) != null) {                    readStr = readStr + read+ "\r\n";                }            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }        return readStr;    }        /**     * 写文件.     *      */    public static void writeTxtFile(String newStr) throws IOException{        //先读取原有文件内容,然后进行写入操作        String filein = newStr + "\r\n";        RandomAccessFile mm = null;        try {            mm = new RandomAccessFile(filename, "rw");            mm.writeBytes(filein);        } catch (IOException e1) {            e1.printStackTrace();        } finally {            if (mm != null) {                try {                    mm.close();                } catch (IOException e2) {                    e2.printStackTrace();                }            }        }    }     /**     * main方法测试     * @param s     * @throws IOException     */    public static void main(String[] s) throws IOException {        Txt.creatTxtFile();        String rstr = Txt.readTxtFile();        String str = rstr + "aaa,456";        Txt.writeTxtFile(str);        }}

servlet代码如下
Java code
package com.feijoy.wap.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.feijoy.wap.tools.Txt;public class RegServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        String regName = request.getParameter("r_name");        String regPwd = request.getParameter("r_pwd");        String info = "";        boolean flag = false;        String allUserInfo = Txt.readTxtFile();        Txt.deleteTxtFile();        if(regName != null && regPwd != null){            if("".equals(regName) || "".equals(regPwd)){                info = "用户名或密码不能为空";                flag = false;            }else{                String userInfo[] = allUserInfo.split("\n");                ArrayList al =new ArrayList();                for(int i=0;i<userInfo.length;i++){                    String userName[] = userInfo[i].split(",");                    al.add(userName[0]);                }                for(int i=0;i<al.size();i++){                    String uname = (String)al.get(i);                    if(regName.equals(uname)){                        flag = false;                        info = "用户名重复,请重新输入用户名";                        break;                    }else{                        flag = true;                    }                }            }        }else{            flag = false;            info = "请正常访问";        }        if(true){            //写入txt注册信息            System.out.println("----------------------------------");            System.out.println("原有用户信息 "+allUserInfo);            String userInfo = regName + "," + regPwd;            System.out.println("新用户信息 "+userInfo);            String newInfo = allUserInfo + userInfo;            Txt.creatTxtFile();            Txt.writeTxtFile(newInfo);                        info = "注册成功!";            request.setAttribute("message", info);            request.getRequestDispatcher("/user/info.jsp").forward(request, response);        }else{            //转到错误页面            request.setAttribute("message", info);            request.getRequestDispatcher("/user/info.jsp").forward(request, response);        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doGet(request, response);    }}
  相关解决方案