Why does this program fall into endless loop?

J

Jack Dowson

Hello EveryBody:
Here is a small demo:

import java.io.*;
import java.util.*;
class DeleteDir{
public DeleteDir(File dir){
if(dir.isDirectory()){
LinkedList dirs = new LinkedList();
dirs.add(dir);
while(dirs.size() > 0){
File currentDir = (File)dirs.getFirst();
File[] files = currentDir.listFiles();
boolean emptyDir = true;
for(int i= 0; i<files.length; i++){
if(files.isFile()){
System.out.println("Deleting..." + files.getAbsolutePath());
try{
files.delete();
}catch(SecurityException se){
se.printStackTrace();
}
}else{
dirs.addFirst(files);
emptyDir = false;
}
}

if(emptyDir){
System.out.println("Deleting... " + currentDir.getAbsolutePath());
try{
currentDir.delete();
}catch(SecurityException se){
se.printStackTrace();
}
dirs.removeFirst();
}
}
}else if(dir.isFile()){
System.out.println("Error: " + dir.getName() + " is a file");
}else System.out.println("Error: " + dir.getName() + " is unknown");

}
public static void main(String[] args){
File dir = new File(args[0]);
DeleteDir delDir = new DeleteDir(dir);
}
}

When I excute "java DeleteDir experiment",it goes to endless loop.
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
.......
Only by Ctrl+c can I stop it!

Experiment here is a directory in which there is only another directory
named experiment which includes an empty directory named experiment
again in it.Mayb you will feel puzzled.Tree this directory,we can get:
©¸©¤experiment
©¸©¤experiment
©¸©¤experiment

That's the whole of my question!
Any help will be greatly appreciated!

Dowson.
 
C

Christian

Jack said:
Hello EveryBody:
Here is a small demo:

import java.io.*;
import java.util.*;
class DeleteDir{
public DeleteDir(File dir){
if(dir.isDirectory()){
LinkedList dirs = new LinkedList();
dirs.add(dir);
while(dirs.size() > 0){
File currentDir = (File)dirs.getFirst();
File[] files = currentDir.listFiles();
boolean emptyDir = true;
for(int i= 0; i<files.length; i++){
if(files.isFile()){
System.out.println("Deleting..." + files.getAbsolutePath());
try{
files.delete();
}catch(SecurityException se){
se.printStackTrace();
}
}else{
dirs.addFirst(files);
emptyDir = false;
}
}

if(emptyDir){
System.out.println("Deleting... " + currentDir.getAbsolutePath());
try{
currentDir.delete();
}catch(SecurityException se){
se.printStackTrace();
}
dirs.removeFirst();
}
}
}else if(dir.isFile()){
System.out.println("Error: " + dir.getName() + " is a file");
}else System.out.println("Error: " + dir.getName() + " is unknown");

}
public static void main(String[] args){
File dir = new File(args[0]);
DeleteDir delDir = new DeleteDir(dir);
}
}

When I excute "java DeleteDir experiment",it goes to endless loop.
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
......
Only by Ctrl+c can I stop it!

Experiment here is a directory in which there is only another directory
named experiment which includes an empty directory named experiment
again in it.Mayb you will feel puzzled.Tree this directory,we can get:
©¸©¤experiment
©¸©¤experiment
©¸©¤experiment

That's the whole of my question!
Any help will be greatly appreciated!

Dowson.



here some advice:
1. Don't place all your code in a constructor.. use Methods for
executing program logic.
2. Filesystems are trees, a recursive structure.. so use recursion to
work them off... in fact with your Linked List you implemented your own
stack.. that is something recursion can do for you way more elegant..
3. SecurityExceptions? You only need these if a SecurityManager is present..
4. Check if the deletion worked.. if deletion of your Dir did not work
your program will fall in an infinite loop .. (delete() returns a
boolean that tells you this)

Christian
 
L

Lew

here some advice:
1. Don't place all your code in a constructor.. use Methods for
executing program logic.
2. Filesystems are trees, a recursive structure.. so use recursion to
work them off... in fact with your Linked List you implemented your own
stack.. that is something recursion can do for you way more elegant..
3. SecurityExceptions? You only need these if a SecurityManager is present..
4. Check if the deletion worked.. if deletion of your Dir did not work
your program will fall in an infinite loop .. (delete() returns a
boolean that tells you this)


Also, use spaces not TABs to indent UseNet code examples.
 
R

Roman

Jack Dowson pisze:
Hello EveryBody:
Here is a small demo:

import java.io.*;
import java.util.*;
class DeleteDir{
public DeleteDir(File dir){
if(dir.isDirectory()){
LinkedList dirs = new LinkedList();
dirs.add(dir);
while(dirs.size() > 0){
File currentDir = (File)dirs.getFirst();
File[] files = currentDir.listFiles();
boolean emptyDir = true;
for(int i= 0; i<files.length; i++){
if(files.isFile()){
System.out.println("Deleting..." + files.getAbsolutePath());
try{
files.delete();
}catch(SecurityException se){
se.printStackTrace();
}
}else{
dirs.addFirst(files);
emptyDir = false;
}
}

if(emptyDir){
System.out.println("Deleting... " + currentDir.getAbsolutePath());
try{
currentDir.delete();
}catch(SecurityException se){
se.printStackTrace();
}
dirs.removeFirst();
}
}
}else if(dir.isFile()){
System.out.println("Error: " + dir.getName() + " is a file");
}else System.out.println("Error: " + dir.getName() + " is unknown");

}
public static void main(String[] args){
File dir = new File(args[0]);
DeleteDir delDir = new DeleteDir(dir);
}
}

When I excute "java DeleteDir experiment",it goes to endless loop.
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
Deleting... F:\java\practise\practise8\experiment\experiment\experiment
......
Only by Ctrl+c can I stop it!

Experiment here is a directory in which there is only another directory
named experiment which includes an empty directory named experiment
again in it.Mayb you will feel puzzled.Tree this directory,we can get:
└─experiment
└─experiment
└─experiment

That's the whole of my question!
Any help will be greatly appreciated!

Dowson.


one hint from me. Check this condition :)

while(dirs.size() > 0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top