【发布时间】:2016-01-16 17:36:48
【问题描述】:
你能告诉我我的filewriter 有什么问题吗?我想将图像的名称和图像坐标保存到文件save.txt 中。有什么我想念的吗?我什至走在正确的轨道上吗?这是我的第二个项目。我的第一个是做一个可以存储图像的数组。
else if (savePicture.isPointInElement(clickX, clickY)){ //start of save code
FileWriter fw = new FileWriter ("save.txt");
for (int i = 0; i < arrayhat.numElements; i++){
if (arrayhat.images[i].isShowing){
fw.write("hat" + arrayhat.images[i].getXCenter() + " " + arrayhat.images[i].getYCenter() + " ");
}
}
for (int i = 0; i < arrayblunt.numElements; i++){
if (arrayblunt.images[i].isShowing){
fw.write("blunt" + arrayblunt.images[i].getXCenter() + " " + arrayblunt.images[i].getYCenter() + " ");
}
}
for (int i = 0; i < arraydealwithit.numElements; i++){
if (arraydealwithit.images[i].isShowing){
fw.write("dealwithit" + arraydealwithit.images[i].getXCenter() + " " + arraydealwithit.images[i].getYCenter() + " ");
}
}
for (int i = 0; i < arrayweed.numElements; i++){
if (arrayweed.images[i].isShowing){
fw.write("weed" + arrayweed.images[i].getXCenter() + " " + arrayweed.images[i].getYCenter() + " ");
}
}
fw.close();
System.out.println("saved");
}
else if (loadPicture.isPointInElement(clickX, clickY)){//start of load code
Scanner sc = new Scanner (new File("save.txt"));
for (int i = 0; i < arrayhat.numElements; i++){
arrayhat.removeimage(arrayhat.images[i], grouphat);
}
for (int i = 0; i < arrayhat.numElements; i++){
arrayblunt.removeimage(arrayblunt.images[i], groupblunt);
}
for (int i = 0; i < arrayhat.numElements; i++){
arraydealwithit.removeimage(arraydealwithit.images[i], groupdealwithit);
}
for (int i = 0; i < arrayhat.numElements; i++){
arrayweed.removeimage(arrayweed.images[i], groupweed);
}
while (sc.hasNext()){
String word = sc.next();
int x = sc.nextInt();
int y = sc.nextInt();
switch(word){
case "hat":
arrayhat.addImage(x, y, grouphat);
break;
case "blunt":
arrayblunt.addImage(x, y, groupblunt);
break;
case "dealwithit":
arraydealwithit.addImage(x, y, groupdealwithit);
break;
case "weed":
arrayweed.addImage(x, y, groupweed);
break;
}
}
sc.close();
}
这是我的贴纸课
public class sticker {
public EZImage[] images;
public int numElements = 0;
public String filename;
void arraysticker(EZImage image, String name, int x, int y){
image = EZ.addImage(name, x, y);
}
public void addImage(int X, int Y, EZGroup group) {
images[numElements] = EZ.addImage(filename, X, Y);
group.addElement(images[numElements]);
numElements++;
}
public sticker (int size, String file){
filename = file;
images = new EZImage[size];
}
public void removeimage(EZImage image, EZGroup group){
if (containsElement(image)){
group.removeElement(image);
EZ.removeEZElement(image);
image = null;
}
}
public boolean containsElement(EZImage element){
for(int i = 0; i < numElements; i++){
if (element == images[i]){
return true;
}
}
return false;
}
}
【问题讨论】:
-
更清楚地描述您的问题,选择一个能反映您确切问题的标题
-
请向我们展示与您的初始“else”块配对的 if 条件,因为此代码可能会被完全跳过:导致没有文件被写入。
标签: java arrays filewriter