In this article about File handling using Swing, I'll show how to work with the methods below:
Now let's create the methods above:
1.1- Create a new button and set the "label" corresponding to his action, in our case "FileName":

1.2- Enter this code in commands package.
package commands;
import java.io.File;
public class FileName {
public String GetName(String fi){
File f = new File(fi);
if(!f.exists()){
System.out.println("File not found");
return "0";
}
return f.getName();
}
}
2.1- Create a new button and set the "label" corresponding to his action, in our case "size (length)":

2.2- Write this code in commands package:
package commands;
import java.io.File;
public class FileSize {
public Integer GetSize(String fi){
File f = new File(fi);
if(!f.exists()){
System.out.println("File not found");
return 0;
}
return (int) f.length();
}
}
3.1- Create a new button and set the "label" corresponding to his action, in our case "Exists?"

3.2- Write this code in commands package:
package commands;
import java.io.File;
public class IfExist {
public boolean exist(String file){
File f = new File(file);
if(!f.exists()){
System.out.println("File not found");
return false;
}
return true;
}
}
4.1- Create a new button and set the "label" corresponding to his action, in our case "Can Read?":

4.2- Write this code in commands package:
package commands;
import java.io.File;
public class CanReadClass {
public boolean canread(String file){
File f = new File(file);
if(!f.canread()){
System.out.println("File not readable");
return false;
}
return true;
}
}
5.1- Create a new button and set the "label" corresponding to his action, in our case "Can Write?":

5.2- Write this code in commands package:
package commands;
import java.io.File;
public class CanWriteClass {
public boolean canwrite(String file){
File f = new File(file);
if(!f.canWrite()){
System.out.println("File not writable");
return false;
}
return true;
}
}
As you can see all methods are repetitive, just create a class, and (or) importing the same class and run the command for each object, or each button, according to your need.
Let's do now a file rename method.
6.1- Insert a new field of type text to serve as a parameter to rename a particular file.

6.2- Write this code in commands package:
package commands;
import java.io.File;
public class RenameTo {
public void rename(String fi, String fi2){
File f = new File(fi);
File f2 = new File(fi2);
f.renameTo(f2);
}
}
}
7.1- Create a new button and set the "label" corresponding to his action, in our case the method is "isFile".

7.2- Write this code in commands package:
package commands;
import java.io.File;
public class IsFilecommand {
public boolean testFile(String fi){
File f = new File(fi);
if(!f.isFile()){
System.out.println("File is Valid!");
return true;
}
return false;
}
}
8.1- Create a new button and set the "label" corresponding to his action, in our case the method is "lastModified".

8.2- Write this code in commands package:
package commands;
import java.io.File;
import java.text.SimpleDateFormat;
public class lastModified {
public String lastMod(String file){
File f = new File(file);
String r ;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
r = sdf.format(f.lastModified());
return r;
}
}
Note: in this example we need to create a date and time format! In other article we can better address these methods. So don't worry about understanding this class SimpleFormat right now, just focus on understanding the files methods.
9.1- Create a new button and set the "label" corresponding to his action, in our case the method is "DeleteFile".

9.2- Write this code in commands package:
package commands;
import java.io.File;
public class DeleteFile {
public boolean delFile(String fi){
File f = new File(fi);
try{
f.delete();
return true;
}
catch(Exception e){
return false;
}
}
} I believe you can do the other class files methods, send questions and I'll help you to do this.
See you in the next article.
See the prices for this post in Mr.Bool Credits System below: