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