How to Get File Name through Path in Java
file path in java eclipse, file path in java
example, file path in java windows, Get File Name through Path in
Java, Java
Structure
of the Problem Requirements
This
Java Program will get file path from user and and extract the file name from
it. The user just browse the system and enter the file path and will get the
name of required file.
Source
Code
package button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class BrowseFile {
static JFrame frame;
static JButton butt;
static JTextField
textPanel;
static String
path =null;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e) {
}
Start();
}
private static void Start() {
JPanel youLabelPanel = new
JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
youLabelPanel.add(new JLabel("Path:"));
JPanel myLabelPanel = new
JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
final JLabel myLabel = new JLabel();
myLabelPanel.add(myLabel);
JPanel sPanel= new
JPanel();//new FlowLayout(FlowLayout.LEFT, 0, 0)
sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.LINE_AXIS));
textPanel = new
JTextField();
textPanel.setPreferredSize(new Dimension(350,30));
butt = new JButton("Browse");
butt.addActionListener(new ActionListener()
{
public void
actionPerformed(ActionEvent e)
{
path = browseAssociatedTask();
textPanel.setText(path);
//**************************************************************
// get file name
//**************************************************************
int index = path.lastIndexOf("\\");
String fileName = path.substring(index + 1);
//**************************************************************
//**************************************************************
myLabel.setText("File name :"+fileName);
frame.pack();
}
});
sPanel.add(textPanel);
sPanel.add(butt);
JPanel mainPanel = new
JPanel();
mainPanel.setLayout(new
BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(Box.createVerticalStrut(10));
mainPanel.add(youLabelPanel);
mainPanel.add(sPanel);
mainPanel.add(myLabelPanel);
frame = new
JFrame("Browse File Path");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static String browseAssociatedTask() {
String AbsolutePath = null;
JFileChooser fc ;
fc = new JFileChooser();
int returnVal = fc.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
AbsolutePath = file.getAbsolutePath();
}else{
Component frame = null;
JOptionPane.showMessageDialog(frame,"Error in FileChooser APPROVE_OPTION",
"Error",JOptionPane.ERROR_MESSAGE);
}
return AbsolutePath;
}
}
Output
of the Program

No comments:
Post a Comment