tags:

views:

335

answers:

3
private void traverse(String dir, int ctr) throws IOException

{
 // get current file and name       

 File myFile = new File(dir);
 System.out.println("dir path: " + myFile.getAbsolutePath()); // correct path
 System.out.println("exists? : " + myFile.exists()); // returns false
 String name = myFile.getName();
 System.out.println(dir + " is dir? " + myFile.isDirectory());
 if (name.equals("tree.txt"))
  return;

 // print tabs and name
 for (int i = 0; i < ctr; ++i)
  bw2.write("\t");
 bw2.write(name);
 bw2.newLine();


 if (myFile.isFile() && name.charAt(0) != '.') 

 {
  File f = new File(dir + "." + name);
  int version = 1; // if doesn't exist then version is 1

  if (f.exists())
  {
   FileInputStream fis = new FileInputStream(f);
   InputStreamReader isr = new InputStreamReader(fis);
   BufferedReader br = new BufferedReader(isr);

   version = Integer.parseInt(br.readLine()); // get version

   br.close();
  }

  fos1 = new FileOutputStream(f);

  osw1 = new OutputStreamWriter(fos1);

  bw1 = new BufferedWriter(osw1);

  bw1.write(version); // write version

  bw1.close();

 }

 else if (myFile.isDirectory()) 

 {
  String dirContents[] = myFile.list(); 

  for (String content : dirContents)

  {
   traverse(dir + content + '/', ctr + 1);
  }

 }
} // end traverse

output:

kedy@Laptop:~/Desktop/connection$ java Server
dir path: /home/kedy/Desktop/connection/test.txt
exists? : false
test.txt/ is dir? false
dir path: /home/kedy/Desktop/connection/tree.txt
exists? : false
tree.txt/ is dir? false
dir path: /home/kedy/Desktop/connection/folder 2
exists? : false
folder 2/ is dir? false
dir path: /home/kedy/Desktop/connection/test2.txt
exists? : false
test2.txt/ is dir? false
dir path: /home/kedy/Desktop/connection/folder
exists? : false
folder/ is dir? false
A: 

I will probably end up adding to this answer, but, first, you may want to check permissions, to see if the application has read access.

Which version of Java, which OS?

If you move the file to a common directory, common to all users, can the file be found?

You may want to have the path be an argument, so you can easily change it without recompiling your application, which testing this.

James Black
java jdk 1.6, ubuntu. i copy pasted most of this code from my client side's code (the client's side works). yes the application has read access.
+2  A: 

Looking at the output, I would venture a guess that there is indeed no file "test.txt/". However, there probably is a file "text.txt".

Pavel Minaev
+2  A: 

Change

traverse(dir + content + '/', ctr + 1);

to

traverse(dir + '/' + content, ctr + 1);
JRL
this obviously is a mistake but this does not change the output because the program should never reach here since nothing seems to be a directory