8 Replies Latest reply: Sep 3, 2013 2:27 AM by PhHein RSS

    How to get file name

    1033862

      Dear,

       

      i have developed small program which read file from particula folder, i used method "getfilename" to read file name but system raise an error.

       

      how can we read file name instead of completion path of file.

      and method, help

       

      thanks.

        • 1. Re: How to get file name
          jwenting

          what "getfilename" method?

          There's none such anywhere in the core API.

           

          But overall: remember that the path is actually a part of the filename as far as the operating system is concerned.

          If you want to prune the path, just take the last bit of the fully classified name past the last path separator.

          • 2. Re: How to get file name
            rp0428
            i have developed small program which read file from particula folder, i used method "getfilename" to read file name but system raise an error.

             

            how can we read file name instead of completion path of file.

            and method, help

            'system raise an error' is meaningless - Java NEVER just says 'there was an error.'

             

            If you want help you have to SHOW us your code, how you ran it and an EXACT copy of the output, including any exceptions or error messages.

             

            There is no method 'getfilename' but there is a method 'getFileName' for the Path interface.

            http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html

            • getFileName

              Path getFileName()

              Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from the root in the directory hierarchy.

              Returns:
              a path representing the name of the file or directory, or null if this path has zero elements
            • 3. Re: How to get file name
              1033862

              Thanks guys,

               

                It has been done by file.getName();

               

                Thanks.

              • 4. Re: How to get file name
                TPD-Opitz

                If you want us to help you we still need the actual exception (including the stacktrace) your program is showing.

                Also we may need to see the surrounding code (at lease the method where the error occures).

                 

                bye

                TPD

                • 5. Re: How to get file name
                  rp0428
                    It has been done by file.getName();

                  Which is just how to do it - see the JavaDocs for the File class

                  http://docs.oracle.com/javase/6/docs/api/java/io/File.html#getName()

                   

                  getName

                  public String getName()
                  Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.

                   

                  You've already been told several times how to get help in the forum. In particular this is what I said

                  'system raise an error' is meaningless - Java NEVER just says 'there was an error.'

                   

                  If you want help you have to SHOW us your code, how you ran it and an EXACT copy of the output, including any exceptions or error messages.

                  • 6. Re: How to get file name
                    939520

                    Oh well, I'm sure its been said many times in many other ways, but here it is again:


                    http://forums.devshed.com/java-help-9/all-users--how-to-post-a-quesiton-147056.html

                    • 7. Re: How to get file name
                      Tharaka Uchiha

                      Try this with GUI

                      /*

                      * CodeSmell.java

                      *

                      * Created on September 2, 2013, 3:18 PM

                      */

                       

                       

                      package codesmell;

                       

                       

                       

                       

                      import java.io.File;

                      import java.util.Vector;

                      import javax.swing.JFileChooser;

                       

                       

                      /**

                      *

                      * @author  Tharaka

                      */

                      public class CodeSmell extends javax.swing.JFrame {

                       

                       

                          Vector<String> fileList=new Vector();

                          /** Creates new form CodeSmell */

                          public CodeSmell() {

                              initComponents();

                          }

                       

                       

                          /** This method is called from within the constructor to

                           * initialize the form.

                           * WARNING: Do NOT modify this code. The content of this method is

                           * always regenerated by the Form Editor.

                           */

                          @SuppressWarnings("unchecked")

                          // <editor-fold defaultstate="collapsed" desc="Generated Code">                       

                          private void initComponents() {

                       

                       

                              jPanel1 = new javax.swing.JPanel();

                              btn_browes = new javax.swing.JButton();

                              txt_browes = new javax.swing.JTextField();

                              btn_check = new javax.swing.JButton();

                       

                       

                              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

                       

                       

                              btn_browes.setText("Browes");

                              btn_browes.addActionListener(new java.awt.event.ActionListener() {

                                  public void actionPerformed(java.awt.event.ActionEvent evt) {

                                      btn_browesActionPerformed(evt);

                                  }

                              });

                       

                       

                              txt_browes.setText("jTextField1");

                       

                       

                              btn_check.setText("Check");

                       

                       

                              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

                              jPanel1.setLayout(jPanel1Layout);

                              jPanel1Layout.setHorizontalGroup(

                                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

                                      .addContainerGap()

                                      .addComponent(txt_browes, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)

                                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                          .addComponent(btn_check)

                                          .addComponent(btn_browes))

                                      .addGap(45, 45, 45))

                              );

                              jPanel1Layout.setVerticalGroup(

                                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                  .addGroup(jPanel1Layout.createSequentialGroup()

                                      .addGap(41, 41, 41)

                                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

                                          .addComponent(txt_browes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)

                                          .addComponent(btn_browes, javax.swing.GroupLayout.Alignment.LEADING))

                                      .addGap(32, 32, 32)

                                      .addComponent(btn_check)

                                      .addGap(181, 181, 181))

                              );

                       

                       

                              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

                              getContentPane().setLayout(layout);

                              layout.setHorizontalGroup(

                                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                              );

                              layout.setVerticalGroup(

                                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                              );

                       

                       

                              pack();

                          }// </editor-fold>                     

                       

                       

                      private void btn_browesActionPerformed(java.awt.event.ActionEvent evt) {                                        

                       

                       

                          JFileChooser j = new JFileChooser();

                          j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                          Integer opt = j.showSaveDialog(this);

                          txt_browes.setText(j.getSelectedFile().getAbsolutePath());

                       

                           String path = txt_browes.getText();

                       

                         String files;

                         File folder = new File(path);

                         File[] listOfFiles = folder.listFiles();

                       

                         for (int i = 0; i < listOfFiles.length; i++){

                          if (listOfFiles[i].isFile()){

                                           files = listOfFiles[i].getName();

                              if (files.endsWith(".java")){

                                                fileList.add(files);

                                              }

                            }

                         }

                                        if(fileList.size()==0){

                                          System.out.println("No java file in Selected Directory");

                                        }

                                        else{

                                            for(int i=0;i<fileList.size();i++){

                                              System.out.println(fileList.elementAt(i));

                                            }

                                        }

                      }                                       

                       

                       

                          /**

                          * @param args the command line arguments

                          */

                          public static void main(String args[]) {

                              java.awt.EventQueue.invokeLater(new Runnable() {

                                  public void run() {

                                      new CodeSmell().setVisible(true);

                                  }

                              });

                          }

                       

                       

                          // Variables declaration - do not modify                  

                          private javax.swing.JButton btn_browes;

                          private javax.swing.JButton btn_check;

                          private javax.swing.JPanel jPanel1;

                          private javax.swing.JTextField txt_browes;

                          // End of variables declaration                

                       

                       

                      }

                      • 8. Re: How to get file name
                        PhHein

                        TharakaUchiha: spoonfeeding doesn't help anyone. The OP got links to the API doc which should be sufficient for him to learn. Posting ready made code doesn't help at all.