Posts which you will also like.

Wednesday, January 12, 2011

Program in java to show file description

This program shows the full description of a file or directory such as name ,when modified , length etc.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class FileDemonstration extends JFrame
{
    private JTextArea outarea;
    private JScrollPane scroll;
    public FileDemonstration()
    {
        super("Testing Class file..");
        outarea=new JTextArea();
        outarea.setFont(new Font("Serif",Font.BOLD,14));
        scroll=new JScrollPane(outarea);
        add(scroll,BorderLayout.CENTER);
        setSize(400,400);
        setVisible(true);
        analyzefiles();
    }
    private File getFileinf()
    {
        JFileChooser fc=new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int result=fc.showOpenDialog(this);
        if(result==JFileChooser.CANCEL_OPTION)
            System.exit(1);
           
        File fname=fc.getSelectedFile();
        if((fname==null) || (fname.getName().equals("")))
        {
            JOptionPane.showMessageDialog(this,"Invalid Name!","Invalid Name!",JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        return fname;
    }
    public void analyzefiles()
    {
        File name=getFileinf();
        if(name.exists())
        {
            outarea.setText(String.format("%s %s\n %s\n %s\n %s\n %s \n %s\n%s\n %s %s\n %s %s",
            name.getName(),"exists.",(name.isFile()?" is file.":"is not a file."),
            (name.isDirectory()?"is directory":"is not a directory"),(name.isAbsolute() ? " is absolute.":"is not absolute"),
            "Last Modified:",name.lastModified(),"Length",name.length(),"Path:",name.getPath(),"Absolute Path",name.getAbsolutePath(),
            "Parent",name.getParent()));
        }
        if(name.isDirectory())
        {
            String[] dir=name.list();
            outarea.append("\n\nDirectory content\n\n");
            for(String dirname:dir)
                outarea.append(dirname+"\n");
        }
        else
            {
                JOptionPane.showMessageDialog(this,name+"does not exits.","Error",JOptionPane.ERROR_MESSAGE);
            }
    }
}
class jfile
{
    public static void main(String args[])
    {
        FileDemonstration f=new FileDemonstration();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

output:



No comments:

Post a Comment

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool