日期:2014-05-17  浏览次数:20730 次

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.Http