15 June, 2012

Read First Line of Any File

 Read First Line of Any File

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class FileOrganizer extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final int NUMBER_OF_CHAR = 30;
private JTextField fileNameField;
private JTextField firstLineField;
//******************************************************************
public FileOrganizer()
    {
        setSize(WIDTH, HEIGHT);
        //WindowDestroyer listener = new WindowDestroyer();
       // addWindowListener(listener);
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());
        JButton showButton = new JButton("Show first line");
        showButton.addActionListener(this);
        contentPane.add(showButton);
        JButton removeButton = new JButton("Remove file");
        removeButton.addActionListener(this);
        contentPane.add(removeButton);
        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        contentPane.add(resetButton);
        fileNameField = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(fileNameField);
        fileNameField.setText("Enter file name");
        firstLineField = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(firstLineField);
}
//******************************************************************
public void actionPerformed(ActionEvent e)
{
    String actionCommand = e.getActionCommand();
if (actionCommand.equals("Show first line"))
        showFirstLine();
else if (actionCommand.equals("Remove file"))
        removeFile();
else if (actionCommand.equals("Reset"))
        resetFields();
else
        firstLineField.setText("Unexpected error.");
}
//******************************************************************
private void showFirstLine()
{
    Scanner fileInput = null;
    String fileName = fileNameField.getText();
    File fileObject = new File(fileName);
if (!fileObject.exists())
        firstLineField.setText("No such file");
else if (!fileObject.canRead())
        firstLineField.setText("That file is not readable.");
else
    {
try
        {
            fileInput = new Scanner(fileObject);
        }
catch(FileNotFoundException e)
        {
            firstLineField.setText("Error opening the file " + fileName);
        }
        String firstLine = fileInput.nextLine();
        firstLineField.setText(firstLine);
        fileInput.close();
    }
}
//******************************************************************
private void resetFields()
{
    fileNameField.setText("");
    firstLineField.setText("");
}
//******************************************************************
private void removeFile()
{
 Scanner fileInput = null;
 String firstLine;
 String fileName = fileNameField.getText();
 File fileObject = new File(fileName);
  if (!fileObject.exists())
  firstLineField.setText("No such file");
  else if (!fileObject.canWrite())
  firstLineField.setText("Permission denied.");
  else
 {
   if (fileObject.delete())
   firstLineField.setText("File deleted.");
   else
   firstLineField.setText("Could not delete file.");
 }
}
//******************************************************************
public static void main(String[] args)
{
 FileOrganizer gui = new FileOrganizer();
 gui.setVisible(true);
}
//******************************************************************
}



No comments:

Post a Comment

Thank You !