Monday, October 12, 2015

File Info checker

This is .exe file called "File Info". You can check the detail of the file which you choose in your PC.

Download from
https://www.dropbox.com/s/fyww28guqop9d9d/FileInfo.exe?dl=0
Copy right of the original photo
Author: Matthew McClintock
Licence : Design Science License
Commercial use : Yes
Link: http://icones.pro/en/beos-book-help-png-image.html

Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MimeGUI extends JFrame implements ActionListener{

  JTextArea textarea;

  public static void main(String[] args){

 
    MimeGUI frame = new MimeGUI();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(10, 10, 500, 300);
    frame.setTitle("File Info Scanner");
    frame.setVisible(true);
 

  }

  MimeGUI(){
    JButton button = new JButton("file select");
    button.addActionListener(this);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(button);

    textarea = new JTextArea();
    textarea.setLineWrap(true);

    JScrollPane scrollpane = new JScrollPane(textarea);
    scrollpane.setBounds(40, 10, 400, 200);
    scrollpane.setPreferredSize(new Dimension(400, 300));

    JPanel textPanel = new JPanel();
    textPanel.setLayout(null);
    textPanel.add(scrollpane);

    getContentPane().add(textPanel, BorderLayout.CENTER);
    getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
 
  }

  public void actionPerformed(ActionEvent e){

    JFileChooser filechooser = new JFileChooser();

    int selected = filechooser.showOpenDialog(this);
    if (selected == JFileChooser.APPROVE_OPTION){
      File file = filechooser.getSelectedFile();

      textarea.setText("");

      try{
        if (checkBeforeReadfile(file)){
          BufferedReader br = new BufferedReader(new FileReader(file));

          Path source = Paths.get(file.getPath());
    String mime = Files.probeContentType(source);

          textarea.append("Location : " + file.getPath() + "\n\n\n");
          textarea.append("File name : " + file.getName() + "\n");
          textarea.append("File size: " + String.valueOf(getKB(file.length())) + "KB\n");
          textarea.append("Extension: " + getSuffix(file.getName()) + "\n");
          textarea.append("File mime type: " + mime + "\n");

          SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.US);
          textarea.append("Last modified time: " + sdf.format(file.lastModified()) + " (US time)");

          br.close();
        }else{
          System.out.println("Can't find or open the file");
        }
      }catch(FileNotFoundException err){
        System.out.println(err);
      }catch(IOException err){
        System.out.println(err);
      }
    }
  }

  private static boolean checkBeforeReadfile(File file){
    if (file.exists()){
      if (file.isFile() && file.canRead()){
        return true;
      }
    }

    return false;
  }

 private static BigDecimal getKB(long byte1){
  double kbyte1;
  double byte2;

  byte2 = byte1;
  kbyte1 = byte2/1024;

  BigDecimal value = new BigDecimal(kbyte1);
  BigDecimal roundHalfUp1 = value.setScale(2, BigDecimal.ROUND_HALF_UP);

  return roundHalfUp1;
 }

 private static String getSuffix(String FileName) {
     if (FileName == null)
           return null;
        int point = FileName.lastIndexOf(".");
        if (point != -1) {
            return FileName.substring(point + 1);
        }
        return FileName;
    }
}