【问题标题】:How can I write the text from JTextArea to a file except certain words stored as strings?除了某些存储为字符串的单词之外,如何将文本从 JTextArea 写入文件?
【发布时间】:2019-05-24 11:32:11
【问题描述】:

基本上,我构建了一个将用户输入输入到文本区域的界面。按下 Control + C 后,用户将输入保存到 file.txt。

界面默认有两个文本:一个是程序启动时显示(“请在此处输入您的文本!”),另一个是用户按Control + C键成功保存文本时(“文本添加成功! ")。

问题是通过使用 Control + C 键,用户甚至可以保存我上面提到的那两个默认文本。我正在使用 keyListener 来实现我的目标,它工作得很好。另外,我写了一个“If”语句来排除这两个默认文本,但它不起作用。

txtArea.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        }
    @Override
    public void keyPressed(KeyEvent e) {
            String notThisTxt = txtArea.getText();
            String text1 = "Please enter your text in here!";
            String text2 = "Text added successfully!";

            if(!notThisTxt.contentEquals(text1) || !notThisTxt.contentEquals(text2)) {
            if ((e.getKeyCode() == KeyEvent.VK_C) && e.isControlDown()) {
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                    FileWriter fw = null;
                    String someSpace = " ";
                    BufferedWriter br = null;
            try {
                    fw = new FileWriter(file.getAbsoluteFile(), true);
                    br = new BufferedWriter(fw);

                    br.write(someSpace);
                    txtArea.write(br);  
                }catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }finally {
                }try {
                        br.close();
                        fw.close();
                }catch(IOException e2) {
                        System.out.println("The programm doesn't want to close!" + e2);
                    }
                }
                    txtArea.setText(txtAdded);
                }
                else{
                       System.out.println("test");
                    }      
                }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });

我希望从这段代码中获取用户可能输入的所有文本并将其保存为 file.txt,但包含默认文本的 2 个字符串除外。

【问题讨论】:

  • if (!notThisTxt.contentEquals(text1) || !notThisTxt.contentEquals(text2)) 将始终评估为真,因为一个字符串不可能同时等于两个不同的字符串。 notThisTxt 保证不等于其中至少一个。也许您的意思是检查 notThisTxt ≠ text1 notThisTxt ≠ text2?
  • 是的!我的意思是检查 txtArea.getText() 是否不等于 text1 并再次与 txtArea.getText() 不等于 text2 相同。我到底做错了什么?
  • 把我最后一句话变成代码。意思是,在 if-test 中使用“and”(&&)而不是“or”(||)。
  • 天哪!谢谢VGR!我不敢相信事情就这么简单。一天没找到解决办法。我忽略了这个 && 只是因为解决我的问题似乎很简单。现在它可以按我的意愿工作。我怎么能在这个评论部分说谢谢,或者我应该怎么做才能回答这个问题?

标签: java swing jtextarea


【解决方案1】:

if (!notThisTxt.contentEquals(text1) || !notThisTxt.contentEquals(text2)) 将始终评估为真,因为一个字符串不可能同时等于两个不同的字符串。 notThisTxt 保证不等于其中至少一个。

您可能想检查 notThisTxt ≠ text1 notThisTxt ≠ text2。

您想编写代码来完全执行上述语句。具体来说,您需要使用“和”(&&)而不是“或”(||)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多