2017-10-14 101 views
0

我正在寻找一种简单的方法来打开.txt文件,修改它并实时读取所应用的更改。 我创建了一个登录面板,其中有两个用户插入ID/pass的JTextField在Java中打开,修改和读取.txt文件更改的最简单方法是什么?

我想实现一个更改密码方法,所以我需要打开.txt文件,删除旧密码并写入新密码(或覆盖)。然后我必须再次读取文件以更新密码。

这是该小组的代码:

import static java.awt.Color.WHITE; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import linteati_ced.GUI.frame.MainFrame; 
import linteati_ced.HotArea; 
import static linteati_ced.utils.Constants.*; 
import linteati_ced.utils.Resources; 

public class LoginPanel extends JPanel { 

    private MainFrame mf; 

    private BufferedImage background; 

    private FileReader file; 
    private BufferedReader reader; 

    private JTextField userArea, passwordArea; 
    private String user, password; 

    private HotArea confirm; 
    private HotArea exit; 
    private String message; 

    private Thread dialogue; 
    private String code; 

    public LoginPanel(MainFrame mainFrame) throws FileNotFoundException, IOException { 
     this.setSize(PANEL_X, PANEL_Y); 
     this.setLayout(null); 

     this.mf = mainFrame; 

     this.background = Resources.getImage(BACKGROUND_LOGIN); 
     this.message = DEFAULT_MESSAGE; 

     this.confirm = new HotArea(1178, 922, 60, 60); 
     this.exit = new HotArea(1178, 25, 60, 60); 

     this.file = new FileReader(Resources.extract(LOGIN)); 
     this.reader = new BufferedReader(file); 

     this.userArea = new JTextField(""); 
     this.userArea.setBounds(600, 460, 200, 30); 
     this.userArea.setFont(new Font("Arial", 0, 24)); 
     this.passwordArea = new JTextField(""); 
     this.passwordArea.setBounds(600, 550, 200, 30); 
     this.passwordArea.setFont(new Font("Arial", 0, 24)); 

     this.add(this.userArea); 
     this.add(this.passwordArea); 

     try { 
      this.user = reader.readLine(); 
      this.password = reader.readLine(); 
      this.code = reader.readLine(); 
      System.err.println(this.user); 
      System.err.println(this.password); 
     } catch (IOException ex) { 
     } 

     file.close(); 

     this.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mousePressed(MouseEvent e) { 

       if (confirm.isClicked(e)) { 
        if (user.equals(userArea.getText()) && password.equals(passwordArea.getText())) { 
         mf.log_in(); 
        } else { 
         System.out.println("Error"); 
         message = ERROR_MESSAGE; 
         dialogue = new Thread(new MessageDialogue()); 
         dialogue.start(); 
         repaint(); 
        } 
       } 

       if (exit.isClicked(e)) { 
        System.exit(0); 
       } 
      } 
     }); 
    } 

    public String getCode() { 
     return this.code; 
    } 

    public void resetTextField() { 
     this.userArea.setText(""); 
     this.passwordArea.setText(""); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     g.drawImage(background, 0, 0, null); 
     g.setColor(WHITE); 
     g.setFont(new Font("Arial", 0, 22)); 
     g.drawString(message, 560, 660); 
    } 

    private class MessageDialogue implements Runnable { 

     @Override 
     public void run() { 
      try { 
       Thread.sleep(1500); 
      } catch (InterruptedException ex) { 
      } 
      message = DEFAULT_MESSAGE; 
      repaint(); 
     } 

    } 

} 

这些都是file.txt的内容: 用户 密码

+0

如果您要修改文件,则不需要重新读取它。您已经知道这些修改。也没有修改文件这样的事情。你可以附加到它,或者你可以完全覆盖它。 – Kayaman

+0

@Kayaman可以覆盖文件的某些部分,而无需完全覆盖它。它不可能删除或插入数据,只能改变它。 – puhlen

+0

进入file.txt我有2个字:一个用户的ID,一个用户的密码。 我需要用新密码删除/覆盖第二行。 –

回答

0

当我们阅读我们的文件,我们一般用字符串来存储的内容一个文件,但问题在于字符串是不可修改的,即它们不能被编辑,但是你想要做的事情可以用这种方式完成:

  1. 当w在一个文件中输入你的密码尝试写在一个单独的一个为简单起见 。
  2. 然后阅读该内容并将其存储在String变量中。

  3. 使用替换方法进行更改。

  4. 然后再次在您的文件上重写该修改过的字符串。要做到这一点

+0

如果上面的行话没有帮助,请随时索要示例代码。 –

+0

是非常好的一点......这就是为什么数据库是祝福的原因,但是如果你想将每个用户存储在单个文件中,那么你必须使用一个条件,例如读取文件直到达到特定的用户名......这可能通过使用扫描器的hasNext()方法完成,以及使用String类的contains()方法的if条件。 –

+0

你刚刚删除了你的评论-_- –

0

一种方法是从文件读取文本行,而且由于你使用空间划分,分割字符串成使用string.split(" ")数组。更新string[1]的密码,将字符串重新编码为username password,并使用`FileOutputString(...,false)将其写入文件以覆盖内容。

如果您打算离开该文件打开定期更新根据需要,你可以利用它RandomAccessFile维持指针到最后seek和简单地覆盖使用' '来掩盖前面的字符从以前的密码留下的密码。

无论哪种方式,这是我的建议,但正如其他人指出的那样,出于安全原因和便于维护,使用数据库要容易得多。在我工作的地方,我只能访问这样一个数据库,最近需要加密表中的密码。这可能是你的使用密码学的愿望。

0

在您的上下文:

Path path = Paths.get("... .txt"); 
List<String> = Files.readAllLines(path, StandardCharsets.UTF_8); 
user = lines.get(0); 
password = lines.get(1); 
code = lines.get(2); 

List<String> lines = new LinkedList<>(); 
Collections.addAll(lines, user, password, code); 
Files.write(path, lines, StandardCharsets.UTF_8); 

你也可以使用带有键值对的.properties,在XML形式的变体。

如果您以后想用新格式更改软件,可能数据中的版本号也不错。

相关问题