0

'file.txt' 中的新功能是以下几行:

   [noduri1]
   {x0;140;104;31;50;}
   {x1;361;132;55;50;}
   [arce1]
   {a0;160;143;386;173;0;1;}
   [noduri2]
   {x2;192;82;27;50;}
   {x3;174;263;27;50;}
   [arce2]
   {a0;200;119;196;300;0;1;}
   [end_graph] 

这些线代表两个有向图的绘制,每一个都在两个不同的 JPanel 中,但是 writefile 函数将它们放在“file.txt”中。

当我想打开任何绘制的图形时,问题从 readfile 类开始,我收到此异常:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)

读取文件代码是:

          package pachet;
          import java.io.File;
          import java.io.FileReader;
          import java.io.BufferedReader;
          import java.io.IOException;
          import java.util.Vector;
          import java.util.StringTokenizer;
          public class FileOperationRead {
private File file;
private Vector lines;
private aArce1 arcele = new aArce1();
private aNoduri1 nodurile = new aNoduri1();
    private bArce2 arcul = new bArce2();
    private bNoduri2 nodul = new bNoduri2();
private FileOperationRead() {
    lines = new Vector();
}
public FileOperationRead(File file) {
    this();
    this.file = file;
    loadList();
    createListaNoduri1();
    createListaArce1();
            createListaNoduri2();
    createListaArce2();
}
public FileOperationRead(String pathAndFileName) {
    this();
    this.file = new File(pathAndFileName);
    loadList();
    createListaNoduri1();
    createListaArce1();
            createListaNoduri2();
    createListaArce2();
}
private void loadList() {
    String line;
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        line = br.readLine();
        while (line != null) {
            line = StringExt.blankEliminator(line);
            //add the lin to list
            lines.add(line);
            //read a new line from file
            line = br.readLine();
        }
    } catch (IOException e) {
    }
}
private void createListaNoduri1() {
    aNod nodet = new aNod();

    String line;
    StringTokenizerDebugDecorator st;
    int i = 1;

    //no nodes !!!
    if (lines.size() < 8) {
        return;
    }
    while (!((String) lines.get(i)).toUpperCase().equals("[ARCE1]")) {
        line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator (line, ";");
        //creating the node
        nodet = new aNod();
        nodet.setLabel(st.nextToken());
        nodet.setX(Integer.parseInt(st.nextToken()));
        nodet.setY(Integer.parseInt(st.nextToken()));
        nodet.setWidth(Integer.parseInt(st.nextToken()));
        nodet.setHeight(Integer.parseInt(st.nextToken()));
        nodet.setSelected(false);

        nodurile.add(nodet);

        //next node...
        i++;
    }
}
private void createListaArce1() {
    aArc arct = new aArc();

    String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
    while (!((String) lines.get(i)).toUpperCase().equals("[ARCE1]")) {
        i++;
    }
    i++;
    if (i == lines.size() - 1) {
        return;
    }
    while (i < lines.size() - 1) {
        line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
        //creating the arc
        arct = new aArc();
        arct.setLabel(st.nextToken());
        arct.setX1(Integer.parseInt(st.nextToken()));
        arct.setY1(Integer.parseInt(st.nextToken()));
        arct.setX2(Integer.parseInt(st.nextToken()));
        arct.setY2(Integer.parseInt(st.nextToken()));
        arct.setaNodSrc(nodurile.getaNod(Integer.parseInt(st.nextToken())));
        arct.setaNodDest(nodurile.getaNod(Integer.parseInt(st.nextToken())));
        //add the arc to the arcs list
        arcele.add(arct);

        //add the arc to the arcs list
        arcele.add(arct);

        //next arc...
        i++;
    }
}


   private void createListaNoduri2(){
        bNod nodel = new bNod();
        String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
        while (!((String) lines.get(i)).toLowerCase().equals("[end_graph]")) {
        i++;
    }
    i++;
            if (i == lines.size() - 1) {
        return;
    }
            while (i < lines.size() - 1) {
                line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
                    nodel = new bNod();
        nodel.setLabel(st.nextToken());
        nodel.setX(Integer.parseInt(st.nextToken()));
        nodel.setY(Integer.parseInt(st.nextToken()));
        nodel.setWidth(Integer.parseInt(st.nextToken()));
        nodel.setHeight(Integer.parseInt(st.nextToken()));
        nodel.setSelected(false);

        nodul.add(nodel);
                    i++;
            }

    }
   private void createListaArce2(){
       bArc arcl= new bArc();
       String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
        while (!((String) lines.get(i)).toUpperCase().equals("[END_GRAPH]")) {
        i++;
    }
    i++;
            if (i == lines.size() - 1) {
        return;
    }
            while (i < lines.size() - 1) {
                line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
                    arcl = new bArc();

        arcl.setLabel(st.nextToken());
        arcl.setX1(Integer.parseInt(st.nextToken()));
        arcl.setY1(Integer.parseInt(st.nextToken()));
        arcl.setX2(Integer.parseInt(st.nextToken()));
        arcl.setY2(Integer.parseInt(st.nextToken()));
        arcl.setbNodSrc(nodul.getbNod(Integer.parseInt(st.nextToken())));
        arcl.setbNodDest(nodul.getbNod(Integer.parseInt(st.nextToken())));
        arcul.add(arcl);
                    i++;

   }
   }
public aNoduri1 getaNoduri1() {
    return ((aNoduri1) nodurile.clone());
}
public aArce1 getaArce1() {
    return ((aArce1) arcele.clone());
}
    public bNoduri2 getbNoduri2(){
        return ((bNoduri2) nodul.clone());
    }
    public bArce2 getbArce2() {
    return ((bArce2) arcul.clone());
}

}

4

0 回答 0