Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

File handling using Swing in Java

In this article I’ll show how the methods in file handling with java+swing works.

In this article about File handling using Swing, I'll show how to work with the methods below:

  • getName: return the file name;
  • length: return the file lenght;
  • exists: Check if the file exists;
  • canRead: return true if the file can be read;
  • canWrite: return true if the file can be write;
  • renameTo: rename the file;
  • isFile: Return true if the path is valid for a file;
  • lastModified: Show date of last file change;
  • delete: Delete the file;

Now let's create the methods above:

1-GetName ( )

1.1- Create a new button and set the "label" corresponding to his action, in our case "FileName":

imagem 1

1.2- Enter this code in commands package.

Listing 1: GetName

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-Length ( )

2.1- Create a new button and set the "label" corresponding to his action, in our case "size (length)":

imagem2

2.2- Write this code in commands package:

Listing 2: Length

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-Exists ( )

3.1- Create a new button and set the "label" corresponding to his action, in our case "Exists?"

imagem 3

3.2- Write this code in commands package:

Listing 3: Exists

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-CanRead ( )

4.1- Create a new button and set the "label" corresponding to his action, in our case "Can Read?":

imagem 4

4.2- Write this code in commands package:

Listing 4: CanRead

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-CanWrite ( )

5.1- Create a new button and set the "label" corresponding to his action, in our case "Can Write?":

imagem5

5.2- Write this code in commands package:

Listing 5: CanWrite

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-renameTo( )

6.1- Insert a new field of type text to serve as a parameter to rename a particular file.

imagem 6

6.2- Write this code in commands package:

Listing 6: renameTo

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- isFile ( )

7.1- Create a new button and set the "label" corresponding to his action, in our case the method is "isFile".

imagem 7

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- lastModified ( )

8.1- Create a new button and set the "label" corresponding to his action, in our case the method is "lastModified".

imagem 8

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- delete ( )

9.1- Create a new button and set the "label" corresponding to his action, in our case the method is "DeleteFile".

imagem 9

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.



colunista nao disponivel

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login