Skip to main content

Weird File.renameTo behavior

No replies
peterskurt
Offline
Joined: 2009-03-03

I'm running what I think is pretty simple code. It works (that is, the file names ARE actually CHANGED). The "problem" is that the feedback via System.out.println gives inconsistent results. Here are the results first:

<br />
You chose to open this file: My Music<br />
Selected directory is C:\Documents and Settings\Kurt\My Documents\My Music<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\Andrea Bocelli_Romanza (Romance).wma which has an extension: wma<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Andrea Bocelli_Romanza (Romance).wma<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\Dean Martin_You're Nobody 'Til Somebody Loves You.wma which has an extension: wma<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Dean Martin_You're Nobody 'Til Somebody Loves You.wma<br />
...<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Pimsleur - Conversational Modern Greek - 10 - Greek Lesson 10.mp3<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\Pimsleur_-_Conversational_Modern_Greek_-_01_-_Greek_Lesson_1.mp3 which has an extension: mp3<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Various Artists - I Can See Clearly Now.mp3<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\Various Artists - What I Like About You [Karaoke].mp3 which has an extension: mp3<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Various Artists - What I Like About You [Karaoke].mp3<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\Various Artists - What I Like About You.mp3 which has an extension: mp3<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\Various Artists - What I Like About You.mp3<br />
Renaming C:\Documents and Settings\Kurt\My Documents\My Music\wedding exit_-_Wolfstone_Ballavanich.mp3 which has an extension: mp3<br />
New File name is C:\Documents and Settings\Kurt\My Documents\My Music\wedding exit_-_Wolfstone_Ballavanich.mp3<br />

As I said, the file system actually has all spaces replaced with lines, but the println says differently, randomly!

here's the code:

</p>
<p>package fileUtilities;</p>
<p>import java.io.File;<br />
import java.io.IOException;<br />
import javax.swing.JFileChooser;</p>
<p>public class spacetolines extends javax.swing.JFrame {</p>
<p>    /**<br />
     * Creates new form spacetolines<br />
     */<br />
    public spacetolines() {<br />
        initComponents();<br />
    }</p>
<p>    /** This method is called from within the constructor to<br />
     * initialize the form.<br />
     * WARNING: Do NOT modify this code. The content of this method is<br />
     * always regenerated by the Form Editor.<br />
     */<br />
    //<br />
    private void initComponents() {<br />
        jLabel1 = new javax.swing.JLabel();<br />
        jTF_DirName = new javax.swing.JTextField();<br />
        jCB_MatchCase = new javax.swing.JCheckBox();<br />
        jCB_WholeWords = new javax.swing.JCheckBox();<br />
        jCB_Recursive = new javax.swing.JCheckBox();<br />
        jB_ChooseDirectory = new javax.swing.JButton();<br />
        jB_Quit = new javax.swing.JButton();</p>
<p>        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);<br />
        setTitle("Find");<br />
        jLabel1.setText("Choice:");</p>
<p>        jCB_MatchCase.setText("Match Case");<br />
        jCB_MatchCase.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));<br />
        jCB_MatchCase.setMargin(new java.awt.Insets(0, 0, 0, 0));</p>
<p>        jCB_WholeWords.setText("Whole Words");<br />
        jCB_WholeWords.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));<br />
        jCB_WholeWords.setMargin(new java.awt.Insets(0, 0, 0, 0));</p>
<p>        jCB_Recursive.setText("Search subdirectories");<br />
        jCB_Recursive.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));<br />
        jCB_Recursive.setMargin(new java.awt.Insets(0, 0, 0, 0));</p>
<p>        jB_ChooseDirectory.setText("Directory...");<br />
        jB_ChooseDirectory.addActionListener(new java.awt.event.ActionListener() {<br />
            public void actionPerformed(java.awt.event.ActionEvent evt) {<br />
                jB_ChooseDirectoryActionPerformed(evt);<br />
            }<br />
        });</p>
<p>        jB_Quit.setText("Quit");<br />
        jB_Quit.addActionListener(new java.awt.event.ActionListener() {<br />
            public void actionPerformed(java.awt.event.ActionEvent evt) {<br />
                jB_QuitActionPerformed(evt);<br />
            }<br />
        });</p>
<p>        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());<br />
        getContentPane().setLayout(layout);<br />
        layout.setHorizontalGroup(<br />
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)<br />
            .add(layout.createSequentialGroup()<br />
                .addContainerGap()<br />
                .add(jLabel1)<br />
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)<br />
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)<br />
                    .add(jTF_DirName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)<br />
                    .add(layout.createSequentialGroup()<br />
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)<br />
                            .add(jCB_WholeWords)<br />
                            .add(jCB_MatchCase))<br />
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)<br />
                        .add(jCB_Recursive)))<br />
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)<br />
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)<br />
                    .add(jB_ChooseDirectory, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)<br />
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jB_Quit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))<br />
                .addContainerGap())<br />
        );<br />
        layout.setVerticalGroup(<br />
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)<br />
            .add(layout.createSequentialGroup()<br />
                .addContainerGap()<br />
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)<br />
                    .add(jLabel1)<br />
                    .add(jTF_DirName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)<br />
                    .add(jB_ChooseDirectory))<br />
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)<br />
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)<br />
                    .add(layout.createSequentialGroup()<br />
                        .add(jCB_MatchCase)<br />
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)<br />
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)<br />
                            .add(jCB_WholeWords)<br />
                            .add(jCB_Recursive)))<br />
                    .add(jB_Quit))<br />
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))<br />
        );<br />
        pack();<br />
    }//                         </p>
<p>    private void jB_QuitActionPerformed(java.awt.event.ActionEvent evt) {<br />
        dispose();<br />
        System.exit(0);<br />
    }                                       </p>
<p>    private void jB_ChooseDirectoryActionPerformed(java.awt.event.ActionEvent evt) {<br />
        JFileChooser chooser = new JFileChooser();<br />
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);<br />
        int returnVal = chooser.showOpenDialog(this);<br />
        if(returnVal == JFileChooser.APPROVE_OPTION) {<br />
            System.out.println("You chose to open this file: " +<br />
                    chooser.getSelectedFile().getName());<br />
            changeToLines(chooser.getSelectedFile());</p>
<p>        }</p>
<p>    }                                                  </p>
<p>    public void changeToLines(File selectedFile)  {<br />
        File[] files = null;<br />
        String tmpFileName;<br />
        String extension;<br />
        String newFileName;<br />
        try {<br />
            if (selectedFile.isDirectory()) {<br />
                files = selectedFile.listFiles();<br />
            }</p>
<p>            System.out.println("Selected directory is "+selectedFile.getCanonicalPath());<br />
        } catch (IOException ex) {<br />
            ex.printStackTrace();<br />
        }<br />
        for (File file : files) {<br />
            if (file.isFile()) {<br />
                tmpFileName = file.getName();<br />
                extension = tmpFileName.substring(tmpFileName.lastIndexOf('.')+1,tmpFileName.length());<br />
                if ((extension.compareToIgnoreCase("mp3")==0) || (extension.compareToIgnoreCase("wma")==0)) {<br />
                    System.out.println("Renaming "+file + " which has an extension: " + extension);<br />
                    newFileName=tmpFileName.replace(' ','_');<br />
                    if (file.renameTo(new File(selectedFile,newFileName))) {<br />
                        System.out.println("New File name is "+file);<br />
                    }<br />
                }</p>
<p>            }<br />
        }<br />
    }</p>
<p>    /**<br />
     * @param args the command line arguments<br />
     */<br />
    public static void main(String args[]) {<br />
        java.awt.EventQueue.invokeLater(new Runnable() {<br />
            public void run() {<br />
                new spacetolines().setVisible(true);<br />
            }<br />
        });<br />
    }</p>
<p>    // Variables declaration - do not modify<br />
    private javax.swing.JButton jB_ChooseDirectory;<br />
    private javax.swing.JButton jB_Quit;<br />
    private javax.swing.JCheckBox jCB_MatchCase;<br />
    private javax.swing.JCheckBox jCB_Recursive;<br />
    private javax.swing.JCheckBox jCB_WholeWords;<br />
    private javax.swing.JLabel jLabel1;<br />
    private javax.swing.JTextField jTF_DirName;<br />
    // End of variables declaration                   </p>
<p>}<br />

Any thoughts?
Kurt