Skip to content

Commit 7b84ffb

Browse files
authored
Create ListAndRename
1 parent fb4e029 commit 7b84ffb

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

File/ListAndRename

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.DecimalFormat;
4+
5+
public class filetree1
6+
{
7+
static int x = 0;
8+
static void RecursivePrint(File[] arr, int level)
9+
{
10+
// for-each loop for main directory files
11+
for (File f : arr)
12+
{
13+
// tabs for internal levels
14+
for (int i = 0; i < level; i++)
15+
System.out.print("\t");
16+
17+
if(f.isFile()){
18+
19+
//DecimalFormat df = new DecimalFormat("#.000");
20+
float size = f.length()/1048576;
21+
String newName= (x + f.getName());
22+
x++;
23+
f.renameTo(new File((newName)));
24+
System.out.println(f.getName() + " " + size + " MB");
25+
}
26+
27+
28+
else if(f.isDirectory())
29+
{
30+
float size = f.length();
31+
System.out.println("[" + f.getName() + "]" + size + " MB");
32+
33+
// recursion for sub-directories
34+
RecursivePrint(f.listFiles(), level + 1);
35+
}
36+
}
37+
}
38+
39+
// Driver Method
40+
public static void main(String[] args)
41+
{
42+
// Provide full path for directory(change accordingly)
43+
String maindirpath = "D:\\javaprog";
44+
45+
// File object
46+
File maindir = new File(maindirpath);
47+
48+
// Use this to convert perform any actions in the directory specified in the
49+
File file = new File("./list.txt");
50+
try{
51+
PrintStream fileOut = new PrintStream(file);
52+
System.setOut(fileOut);
53+
}
54+
catch(Exception e) {
55+
56+
}
57+
58+
59+
if(maindir.exists() && maindir.isDirectory())
60+
{
61+
// array for files and sub-directories
62+
// of directory pointed by maindir
63+
File arr[] = maindir.listFiles();
64+
65+
System.out.println("**********************************************");
66+
System.out.println("Files from main directory : " + maindir);
67+
System.out.println("**********************************************");
68+
69+
// Calling recursive method
70+
RecursivePrint(arr, 0);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)