Cadastre-se Revistas DevMedia Cursos
 

Space de CARLOS EDUARDO DOMINGUES MAZZI
Busca Autor


Últimas 20 atualizações de CARLOS EDUARDO DOMINGUES MAZZI

Article - File handling using Swing in Java

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:

...
Exibição do post interrompida. Para ler conteúdo completo, clique aqui
23/04/2012 21:28:00





 

Specialist in Project Management and Development. Systems, Linux Certified, Certified-Frameworks ITILv3, developer and systems architect Java, Delphi and C #. Founder of DATACON.
Arquivo de atualizações
 2012

Estatísticas do Autor:
Número de posts: 1
Características dos posts deste autor:
Conteúdo:
Utilidade:
1 0
 
DevMedia Group - Tel: (21) 3382-5038 - www.devmedia.com.br
Todos os Direitos Reservados a DevMedia Group