queue and stack reader code example

Example: queue and stack reader

public static void main(String[] args) throws IOException {

    Stack stack = new Stack(); 
    Queues queue = new Queues();

    File stackfile = new File("stack.txt"); 


    if (!stackfile.exists()) { 
        stackfile.createNewFile(); 
    } else {
        System.out.println("File is done"); 
    }
    FileReader r= new FileReader(stackfile);
    BufferedReader reader = new BufferedReader(r); 

    String line = null; 
    Data data= new Data(); // this class have String name, surname and string number

    int i=1;
    while ((line=reader.readLine())!=null) { 

        { if(line.trim().equals("#")){
            stack.Push(data);
            data=new Data();
            i=1;
        }
        else{
            if(i==1){
                data.setNo(line);
            }
            else if(i==2){
                data.setName(line);

            }
            else if(i==3){
                data.setSurName(line);
            }
            i++;

            }
        }





    }
    stack.Push(data);
    reader.close();

    File queuefile = new File("queue.txt"); 

    if (!queuefile.exists()) { 
        queuefile.createNewFile(); 
    } else {
        System.out.println("File is done");
    }

    BufferedReader read = null; 
    read = new BufferedReader(new FileReader(queuefile));
    String lines = read.readLine();


    while (lines != null) { 
        System.out.println("Read from queue: " + lines);


         { if(lines.trim().equals("#")){
            queue.insert(data);
            data=new Data();
            i=1;
        }
        else{
            if(i==1){
                data.setNo(line);
            }
            else if(i==2){
                data.setName(line);

            }
            else if(i==3){
                data.setSurName(line);
            }
            i++;

            }
        }


    }
    queue.insert(data);
    read.close();

   }
  }

Tags:

Java Example