Saurab
Saurab

Reputation: 2051

Failed to rename a file

This might be a duplicate but trust me i have done lots of research before posting this question

i am working on a text editor project " Sodalime ", and i want to append ".txt" to file name if user forgets to put this extension after their file, but the problem is i just can't rename the "userFile" to "userFile.txt".

the code below is just a demo and not a part of my project Sodalime

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Window;
import javax.swing.JTextPane;
import java.awt.Point;
import javax.swing.JTextArea;
import java.awt.Component;
import java.io.File;
import java.time.temporal.JulianFields;


public class MenuTest {

    JFrame frame;
    /**
     * @wbp.nonvisual location=280,89
     */

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MenuTest window = new MenuTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MenuTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(new Rectangle(0, 0, 300, 400));

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setIcon(new ImageIcon("E:\\Sodalime\\icons\\delete.png"));
        btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        final JTextArea textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(250,300));
        textArea.setBackground(Color.LIGHT_GRAY);

        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                JFileChooser fc = new JFileChooser();
                int filechoice = fc.showOpenDialog(fc);
                if(filechoice == JFileChooser.APPROVE_OPTION){
                File chosedfile = fc.getSelectedFile();
                FilenewFIle=newFile(chosedfile.getParent()+File.separator+".txt");

                if(chosedfile.renameTo(newFIle)) {
                    textArea.setText("done \n");
                    textArea.append(chosedfile.getAbsolutePath()+"\n");
                    textArea.append(newFIle.getAbsolutePath());
                }
                else textArea.setText("failed");
                }
            }
        });

        panel.add(textArea);


    }
}

Also any suggestions to make my editor working properly will be highly appreciated. This might be OFFTOPIC but i have couple of issues with my editor like gridLayout leaves lots of Vgap and some more , so i would appreciate my suggestions given to improve my coding style and my project

my project is on github Sodalime-v1.0

Upvotes: 0

Views: 87

Answers (1)

dryairship
dryairship

Reputation: 6077

Make your :

File newFIle=new File(chosedfile.getParent()+File.separator+".txt");

To:

File newFIle=new File(chosedfile.getParent()+File.separator+chosedfile.getName()+".txt");

Your code is wrong.
For example, if the path is C:/ABC/d, your code will make it C:/ABC/.txt. You need to add the name of the file after the file separator, to make it C:/ABC/d.txt

Upvotes: 2

Related Questions