Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: BufferedWritter saves every file as a file type extension

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default BufferedWritter saves every file as a file type extension

    public class main extends javax.swing.JFrame {
     
        public main() {
            initComponents();
        }
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jLabel4 = new javax.swing.JLabel();
            labelID = new javax.swing.JLabel();
            labelName = new javax.swing.JLabel();
            labelDep = new javax.swing.JLabel();
            id = new javax.swing.JTextField();
            name = new javax.swing.JTextField();
            dep = new javax.swing.JTextField();
            save = new javax.swing.JButton();
            load = new javax.swing.JButton();
     
            jLabel4.setText("jLabel4");
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            labelID.setText("ID");
     
            labelName.setText("Name");
     
            labelDep.setText("Department");
     
            id.setText("2234");
     
            name.setText("name");
     
            dep.setText("dep");
     
            save.setText("Save");
            save.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    saveActionPerformed(evt);
                }
            });
     
            load.setText("Load");
            load.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loadActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(labelID)
                                .addComponent(labelName)
                                .addComponent(labelDep))
                            .addGap(42, 42, 42)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(id)
                                .addComponent(name)
                                .addComponent(dep, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(save)
                            .addGap(18, 18, 18)
                            .addComponent(load)))
                    .addContainerGap(34, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelID)
                        .addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelName)
                        .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelDep)
                        .addComponent(dep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(save)
                        .addComponent(load))
                    .addContainerGap(19, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
     
            saveToFile();//Show the save dialog.
     
        }    
     
        private void saveToFile(){
     
            JFileChooser fileChooser=new JFileChooser(System.getProperty("user.dir"));
            fileChooser.setDialogTitle("Save as...");
     
            fileChooser.setAcceptAllFileFilterUsed(false);
            FileNameExtensionFilter restric=new FileNameExtensionFilter("(*.txt)","txt");
            fileChooser.addChoosableFileFilter(restric);
     
            fileChooser.showSaveDialog(this);
     
            File file=fileChooser.getSelectedFile();
     
            try{
                FileWriter fwriter=new FileWriter(file);
                BufferedWriter bwriter=null;
                bwriter=new BufferedWriter(fwriter);
     
                bwriter.write(saveData());
                bwriter.flush();
                bwriter.close();
                fwriter.close();
     
            }catch(IOException e){
                e.printStackTrace();
            }
     
    private String saveData(){
                String a="";
                a=a+id.getText()+System.lineSeparator()+name.getText()+System.lineSeparator()+dep.getText();
                System.out.println(a);
                return a;
     
        }
     
        }
    Everything works fine except the (*.txt) extension. Always saves as a (*.file) extension file. Thanks in advance.
    Last edited by jms; July 2nd, 2019 at 11:57 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    Where is the BufferedWriter you ask about?

    The code works for me.

    What do you enter in the JFileChooser open window?

    What is returned by fileChooser.getSelectedFile()? Print it to see.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    I included most of the code. The code works for me too, but when i go check the file, the format is in file extension. I've tried txt, me, saf etc and still I get file extension.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    What was printed when you added the print statement to show the value in the file variable?

    Where is "format is in file extension" shown? What OS are you using? How are you viewing the files in the folder?
    Can you open a console window, change to the folder with the files in question and display the contents of that folder.
    Then copy what was shown on the console window and paste it here so we can see it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    I'm using Netbeans and I get the id, name and department. If I type, for example, 'try' in the save dialog I get a file that I have to open manually(notepad in windows 10), but if I type 'try.txt' I get a txt file that opens immediately with notepad. Is it possible to open a BufferedReader and use some command to get the directory with the filename, then change the filename to filename.txt?

    I'm not familiar with cmd, I'm learning java in my spare time.
    Last edited by jms; July 2nd, 2019 at 12:25 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    Did you miss this question?
    What was printed when you added the print statement to show the value in the file variable?

    The file is given the name that you type into the JFileChooser dialog window.
    It is up to you to give the file its extension.
    If you enter "test" then the file will be named test without any extension. The FileExplorer program will say it is a file

    change the filename to filename.txt?
    Look at what was printed with the print statement I asked for. The program can get a String with the filename from the File object, examine it and add a .txt extension if the filename does not have one. Then a new File object can be created with the new name.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jms (July 2nd, 2019)

  8. #7
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BufferedWritter saves every file as a file type extension

    I think I understand now. Thanks for your help!!!

Similar Threads

  1. Reading a file and comparing them to an array to get the file type - java
    By tomb1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 06:13 PM
  2. Created a Random Access File that saves gibberish to a text file
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 21st, 2012, 11:45 AM
  3. How to add file extension to JFileChooser.showSaveDialog() ?
    By camboy8 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2012, 09:23 AM
  4. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  5. [SOLVED] If a file extension is known, but not the name, how do you find it?
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 3rd, 2011, 03:09 PM