如何让我的文件写入器从一开始就写入而不是最近的?

时间:2015-10-19 05:53:42

标签: java arrays save

我的项目要求我创建一个保存按钮,按下该按钮时,会将图像名称(贴纸)及其坐标写入save.txt。它写入save.txt但只会写入最新的图像。我明白为什么会这样做而不是如何解决它。这样做是因为每次放置图像时,它都会增加整数“图像”。当我按下保存时,它会写出“图像”的名称和坐标。所以它会写出最新的一个。请帮我把它从头开始写。这是我代码的保存部分。

else if (savePicture.isPointInElement(clickX, clickY)){ 
    FileWriter fw = new FileWriter("save.txt");
        for (int i = 0; i < image; i++){
            if (arraysticker[image] != null){
                String name;
                int x,y;
                name = sticker.getname();
                x = sticker.getx();
                y = sticker.gety();
                fw.write(name + " " + x + " " + y + "\n");
            }
        }
    fw.flush();
    fw.close();
    System.out.println("saved");
}

如果您需要,这是我的完整代码。唯一缺少的是我放置初始图像。

    sticker arrayhat = new sticker (20, "hat.png");
    sticker arrayblunt = new sticker (20, "blunt.png");
    sticker arraydealwithit = new sticker (20, "dealwithit.png");
    sticker arrayweed = new sticker (20, "weed.png");

    int image = -1;//declares an integer variable named image
    //creates a boolean variable and sets it to false
    boolean hatSoundPlay = false;
    boolean bluntSoundPlay = false;
    boolean dealwithitSoundPlay = false;
    boolean weedSoundPlay = false;


    while (true){//while it is true
        if (EZInteraction.wasMouseLeftButtonReleased()){//if left button is released
        int clickX = EZInteraction.getXMouse(); //get x coordinates
        int clickY = EZInteraction.getYMouse();//get y coordinates

            if (hatPicture.isPointInElement(clickX, clickY)){//if hat is in the x and y coordinates
                bluntSoundPlay = false; //turn all sounds false
                weedSoundPlay = false; 
                dealwithitSoundPlay = false; 
                if (!hatSoundPlay) { //if hatsound not play
                    hatsound.play(); //hatsound will play
                    hatSoundPlay = true; //hat sound is now true    
                }
            }
            else if(bluntPicture.isPointInElement(clickX, clickY)){//if blunt is in the x and y coordinates
                hatSoundPlay = false;//turn all sounds false
                weedSoundPlay = false;
                dealwithitSoundPlay = false;
                if (!bluntSoundPlay) {//if bluntsound not play
                    bluntsound.play(); //then bluntsound will play
                    bluntSoundPlay = true; //bluntSoundPLay is now assigned to true
                }
            }
            else if(dealwithitPicture.isPointInElement(clickX, clickY)){//if dealwithit is in the x and y coordinates
                hatSoundPlay = false;//turn all sounds false
                bluntSoundPlay = false;
                weedSoundPlay = false;
                if (!dealwithitSoundPlay) {//if dealwithitsound not play
                    dealwithitsound.play(); //then dealwithitsound will pay
                    dealwithitSoundPlay = true; //dealwithitSoundPlay is assigned as true
                }
            }
            else if(weedPicture.isPointInElement(clickX, clickY)){//if weed is in the x and y coordinates
                dealwithitSoundPlay = false;//turn all sounds false
                hatSoundPlay = false;
                bluntSoundPlay = false;
                if (!weedSoundPlay) {//if weedsound not play
                    weedsound.play(); //then weedsound will play
                    weedSoundPlay = true; //sets the varialbe weedSoundPlay to true
                } 
            }


            else if (backgroundPicture.isPointInElement(clickX, clickY) && !savePicture.isPointInElement(clickX, clickY) 
                    && !loadPicture.isPointInElement(clickX, clickY) && !clearPicture.isPointInElement(clickX, clickY) 
                    && !rectanglePicture.isPointInElement(clickX, clickY)){
                if (hatSoundPlay){ //if hatSoundPlay is true
                    hatsound.play(); //and a hatsound will play
                    sticker sticker1 = new sticker();//creates a new sticker1
                    sticker1.arraysticker(hatPicture, "hat.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker1;//puts sticker 1 into the array

                } else if (bluntSoundPlay){ //else if bluntSound is true
                    bluntsound.play(); //and blunt sound will play
                    sticker sticker2 = new sticker();
                    sticker2.arraysticker(bluntPicture, "blunt.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker2;//puts sticker 2 into the array

                }else if (dealwithitSoundPlay){ //if dealwithitsound is true
                    dealwithitsound.play(); //dealwithitsound will play
                    sticker sticker3 = new sticker();
                    sticker3.arraysticker(dealwithitPicture, "dealwithit.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker3;//puts sticker 3 into the array

                }else if (weedSoundPlay){ //if weedsound is true
                    weedsound.play(); //weedsound will also play
                    sticker sticker4 = new sticker();
                    sticker4.arraysticker(weedPicture, "weed.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker4;//puts sticker 4 into the array
                }
            }
            else if (savePicture.isPointInElement(clickX, clickY)){ 
                FileWriter fw = new FileWriter("save.txt");
                for (int i = 0; i < image; i++){
                    if (arraysticker[image] != null){
                        String name;
                        int x,y;
                        name = sticker.getname();
                        x = sticker.getx();
                        y = sticker.gety();
                        fw.write(name + " " + x + " " + y + "\n");
                    }
                }
                fw.flush();
                fw.close();
                System.out.println("saved");
            }
        //  else if (loadPicture.isPointInElement(clickX, clickY)){
            //  Scanner sc = new Scanner (new File ("save.txt"));
            //  for (int i = 0; i < arraysticker.length; i++){
                //  hatPicture.removeImage;
                //}
            //}
            }
        EZ.refreshScreen();     
    }

    }
}

1 个答案:

答案 0 :(得分:0)

您的for-loop使用了错误的index值...

for (int i = 0; i < image; i++){
    if (arraysticker[image] != null){

您还没有访问arraysticker中的值,它应该是

for (int i = 0; image < arraysticker.length; i++){
    if (arraysticker[i] != null){
        String name;
        int x,y;
        name = arraysticker[i].getname();
        x = arraysticker[i].getx();
        y = arraysticker[i].gety();
        fw.write(name + " " + x + " " + y + "\n");
    }

您还应该看看The try-with-resources Statement,以便更好地管理资源并确保资源正常关闭