首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用FileReader加载文本文件后,在.setText()方法中不插入任何内容

使用FileReader加载文本文件后,在.setText()方法中不插入任何内容
EN

Stack Overflow用户
提问于 2013-05-15 05:27:36
回答 2查看 277关注 0票数 0

我正在完成一个学校的java项目,但我被困在了某个点上。

任务是制作一个任务计划器,它应该能够保存添加/编辑或删除的任务,并将这些信息保存在计算机目录中的文本文件中。

这样,每次开始打开应用程序时,保存的任务信息都会加载到GUI中。

我面临的问题是,我必须(在加载时)在GUI的某些文本字段中插入信息……然而,有时并不是所有的文本字段都应该完成-换句话说,它不是必须的-

这就是为什么有时.setText()方法会有一个空参数,代码不能工作……

我试图通过每次检测到一个空参数时插入一个“”(空格)来解决这个问题--但我发现这个解决方案很难看,因为它适用于当点击一个新的带有新的空TextField的图形用户界面窗口时,会自动出现一个空格,用户应该在写东西之前按退格键……

有没有更好的解决方案?

下面是加载所有任务的代码...

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

 public class ReadTask extends Planner {

    public static void read(){

        String[] myArray = new String[8];
        String line = "";

        TaskWindowNew currentTask = null;

        try 
        {
            BufferedReader br = new BufferedReader(new FileReader("PlannerText.txt"));          

            while (( line = br.readLine()) !=null) 
            {
                myArray = line.split("\\|");

                for(int i=0;i<myArray.length;i++)
                {
                    if( myArray[i].isEmpty())
                    {
                        myArray[i] = "yoooo";
                    }
                }

                System.out.println(myArray.toString());

                try{
                    currentTask = new TaskWindowNew();
                    currentTask.popupFrame.setVisible(false);

                    // IMPORTANT = If one or more field(s) is left empty the .doClick function will not work
                    // we need to find a solution for this.
                    currentTask.titleField.setText(myArray[1]);
                    currentTask.minuteField.setText(myArray[2]);
                    currentTask.hourField.setText(myArray[3]);
                    currentTask.daysMonthField.setText(myArray[4]);
                    currentTask.MonthField.setText(myArray[5]);
                    currentTask.daysWeekField.setText(myArray[6]);
                    currentTask.specialSymbolField.setText(myArray[7]);

                    //this emulates the action of the OK-button in the new task window !
                    currentTask.buttonOk.doClick();

                 } catch (ArrayIndexOutOfBoundsException e)
                 {

                 }
            }

            br.close(); 

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        updateScrollPanel();


    }

}
EN

回答 2

Stack Overflow用户

发布于 2013-05-15 05:58:19

您所做的是将它与文件contents.Meaning紧密耦合在一起,在数组索引1处,将有一个标题字段,或者在索引3处,小时field.But,如果用户没有输入任何值,那么索引将更改,在某些情况下,您可能会得到ArrayIndexOutOfBoundsException。

因此,当用户未输入非必填字段时,您不能期望下次在UI上显示它们。

您应该做的是将键值对存储在file.This中,这将使您能够检查哪个字段的值,而不是依赖于数组索引。

我的意思是这样的:

头衔:Mr

小时:四点

specialSymbol:#

然后,当您预先填充它时,您应该获取每一行,拆分键和值,如果值不为空,则在TaskWindowNew的适当变量(您将从键中了解)中设置它。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2013-05-16 00:05:11

感谢每个人的回复/评论

我通过擦除myArray并将.split结果直接赋值给一个数组解决了这个问题。我不知道为什么是这样的。

对于那些对我的SaveTask类感兴趣的人,我像这样保存数据

代码语言:javascript
复制
outputStream.println(order + "|"+ title +"|"+ minutes +"|"+ hours +"|"+ daysPerMonth +"|"+ months +"|"+ daysPerWeek +"|"+ specialSymbol +"|"+"end");
order++;

但是如果末尾没有"end"-flag,下面的代码将无法工作……否则,它不会使数组有8个插槽的长度,它只会使数组与最后一个填充的TextField一样长。(这给了我ArrayIndexOutOfBoundsException边界异常-问题)

这就是我修复它的方法。

我认为@skandigraun很困惑,因为StringTokenizer实际上做了相反的事情(我相信)

查看此处:http://www.coderanch.com/t/446893/java/java/Difference-split-StringTokenizer-nextToken

以及可以工作的代码:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

 public class ReadTask extends Planner {

    public static void read()
    {

        //try-catch for IOExceptions
        try 
        {
            //Make the inputStream
            BufferedReader br = new BufferedReader(new FileReader("PlannerText.txt"));

            //The String containing each line
            String line = null;

            //For each line do the following
            while ( (line = br.readLine()) !=null) 
            {               
                //Make a new Task-window
                TaskWindowNew currentTask = new TaskWindowNew();

                //Set visibility to false
                currentTask.popupFrame.setVisible(false);

                //An array containing each string seperated by the delimiter
                String[] currTask = line.split("\\|");

                System.out.println(currTask.length);

                System.out.println(currTask[0]);

                //Fill each field with the corresponding String
                currentTask.titleField.setText(currTask[1]);
                currentTask.minuteField.setText(currTask[2]);
                currentTask.hourField.setText(currTask[3]);
                currentTask.daysMonthField.setText(currTask[4]);
                currentTask.MonthField.setText(currTask[5]);
                currentTask.daysWeekField.setText(currTask[6]);
                currentTask.specialSymbolField.setText(currTask[7]);

                //this emulates the action of the OK-button in the new task window !
                currentTask.buttonOk.doClick();
            }

            //Close the stream
            br.close(); 

        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        updateScrollPanel();            
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16553157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档