各位专家,我是菜鸟。我冒昧的想问问,如何在jsp中实现对固定行的修改呢?
我可以用filereader和filewriter对文件进行行读取和写入。可如果我想对文件中的某一行进行修改,比如更新相对应的数据或干脆删除此行(元组),有什么好点的办法吗?
我不是连接数据库的,仅仅是实行本地操作。不是对向sql这样的数据库进行操作或管理的。所以什么delete语句对我不行。
千万别跟我说什么先取出文件的全部内容,赋值给临时变量,用找到要修改的修改,再重新输入到新的文件或干脆覆盖原文件。拜托,我只想修改其中一个元组,一行而已,不想修改全部文件。

解决方案 »

  1.   


    import java.io.*;
    import java.util.*;
    public class CheckAdminUser {    private String userName = "";
        private String userPassWord = "";
        private Properties http_props = new Properties();
        private InputStream infile = null;
        public CheckAdminUser() {}
        private String pt=getClass().getResource("adminInfo.txt").getPath();
        private void loadFile() {
            try {           // infile = getClass().getResourceAsStream("admininfo.txt");
               infile=new FileInputStream(new File(pt));
                http_props.load(infile);
                userName = http_props.getProperty("userName");
                userPassWord = http_props.getProperty("userPassword");
                infile.close();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }        public boolean checkUserOK(String cUserPassWord) {
               loadFile();
               if (cUserPassWord.equals(this.userPassWord)) {
                   return true;
               } else {
                   return false;
               }
        }
        public void setUserInfo(String userName, String userPwd) {
            loadFile();        try {
                FileOutputStream fos = new FileOutputStream(pt);
                http_props.setProperty("userName", userName);
                http_props.setProperty("userPassword", userPwd);
                http_props.store(fos, null);
                fos.flush();
                fos.close();
                infile.close();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
      /*  public static void main(String[] args) {
            CheckAdminUser cu = new CheckAdminUser();
            cu.setUserInfo("user", "pwd");
        }
    */}这是一个读和写记事本文件的程序,希望对你有 帮助。
    记事本里文字的样式是这样的:
    userName=admin
    userPassword=admin
      

  2.   

    补充一点:
    类和txt文件放在同一个目录下。
      

  3.   

    楼上的例子好像关系不大啊。
    RandomAccessFile类可以删除字符吗,好像只能write,read
    看来只能将要删的一段取出来再写回去了。