Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:25:31+00:00 2026-06-09T08:25:31+00:00

I use a FileWriter and a BufferWriter to write in a file. The file

  • 0

I use a FileWriter and a BufferWriter to write in a file. The file “test.txt” is created but nothing is written inside.

The file should be written in the ActionEvent of my button. Is that the reason why ?

That’s my code :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;  
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

class Cadre_fenetreA2 extends JFrame            
{
    JLabel phrase = new JLabel("Veuillez indiquer le nom de chaque groupe de generalisation pour chaque Niveau");
    JButton boutonOK = new JButton ("OK");
    public Cadre_fenetreA2 (String nom,int X, int Y, int lo, int la, int Na, int [] nbGrpGen,int maxnbGrpGen, File file)        
    {
        super(nom);                     
        setBounds(X,Y,lo,la);       
        setVisible(true);                   


        JTextField[][] allField = new JTextField [Na][maxnbGrpGen];
        JLabel phrases[] = new JLabel [Na];
        String[][] nomGrpGen = new String [Na][maxnbGrpGen];



        setLayout(null);
        for(int i = 0;i < Na;i++)
        {
            for(int j = 0;j<nbGrpGen[i];j++)
            {
                String uuid = UUID.randomUUID().toString();

                 allField[i][j] = new JTextField(uuid.substring(0,8));
                 allField[i][j].setBounds(150+j * 60, 75 + i * 25, 50, 20);
                 add(allField[i][j]);
            }

           phrases[i] = new JLabel("Niveau "+String.valueOf(i+1));
           phrases[i].setBounds(5, 75 + i * 25, 200, 20);

           add( phrases[i]);
        }
        phrase.setBounds(0,0,1000,50);
        add(phrase);
        boutonOK.setBounds(250+maxnbGrpGen*50,25*(Na),60,60);
        add(boutonOK);
        boutonOK.addActionListener(new ecout(Na,nbGrpGen,allField,nomGrpGen, file));
    }

    class ecout implements ActionListener 
    {   
        private int Na;
        private String [][] nomGrpGen ;
        private JTextField[][] allField;
        boolean correct=true;
        private int[] nbGrpGen;
        String chaine;
        File file;


        public ecout(int Na,int[] nbGrpGen, JTextField[][] allField, String[][] nomGrpGen, File file)
        {
            this.file = file;
            this.Na = Na;
            this.nbGrpGen =nbGrpGen;
            this.allField =allField;
            this.nomGrpGen =nomGrpGen;
        }

        public void actionPerformed(ActionEvent evt)
        {
            for(int i = 0;i < Na ;i++)
            {
                for(int j = 0;j < nbGrpGen[i] ;j++)
                {
                nomGrpGen[i][j] = allField[i][j].getText();
                    //chaine=allField[i][j].getText();
                    //nomGrpGen[i][j] ="A";
                  if(nomGrpGen[i][j]=="")
                  {
                      correct=false;
                  }
                }
            } 
            if(correct)
            {
                int i=0;
                int j=0;
                int nbElement;
                JOptionPane jop = new JOptionPane();
                String res;
                do
                {
                    res= jop.showInputDialog(null, "Veuillez indiquer le nombre d'attribut present au depart", "nombre d'attribut",JOptionPane.QUESTION_MESSAGE);
                }
                while(! isInteger(res));
                nbElement =Integer.parseInt(res); 


                int largeur=150+nbElement*30+80;
                int hauteur=30*(nbGrpGen[i])+100;
                if(largeur<600)
                {
                    largeur=600;
                }

                try 
                {
                    FileWriter fw = new FileWriter("test.txt");
                            BufferedWriter out = new BufferedWriter(fw);
                            out.write("aString");
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                dispose();
                i=0;
                j=0;
                new Cadre_fenetreA3 ("initialisation du groupe"+nomGrpGen[i][j],5,5,largeur, hauteur, nbGrpGen[i], nbElement,nomGrpGen[i], i+1); 
            }
        }
    }
    public boolean isInteger( String input )  
    {  
       try  
       {  
          Integer.parseInt( input );  
          return true;  
       }  
       catch( Exception e)  
       {  
          return false;  
       }  
    }  

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:25:33+00:00Added an answer on June 9, 2026 at 8:25 am

    Here,

    try 
    {
        FileWriter fw = new FileWriter("test.txt");
                BufferedWriter out = new BufferedWriter(fw);
                out.write("aString");
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    

    You wrapped it in a BufferedWriter which has a default buffer size of 8KB. So as long as you don’t write more than 8KB and don’t flush/close it, then it won’t appear in the target file.

    You need to close the writer when you’re done with it. This will flush any buffers and release any locks on the file. The normal idiom is to close it in the finally block of the very same try block as where it was been created. This way you guarantee that it’s also closed in case of exceptions, hereby preventing resource leaking and forever locked files.

    Writer writer = null;
    
    try {
        writer = new BufferedWriter(new FileWriter("test.txt"));
        writer.write("aString");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) try { writer.close(); } catch (IOException ignore) {}
    }
    

    Alternatively, if you’re already on Java 7, then you can also use the new try-with-resources statement. It’ll automatically close the resource when you leave the try block, resulting in less boilerplate code.

    try (Writer writer = new BufferedWriter(new FileWriter("test.txt"))) {
        writer.write("aString");
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    By the way, I’d work on exception handling as well. Rather display the enduser some sensible error message instead of plain printing it to the stdout and ignoring it further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a new file with the FileWriter . I use it
Please look through code below: // A.class File file = new File(blah.txt); FileWriter fileWriter
I use FileWriter for create a file. I have an error Directory does not
i use the code below to write a file to the sd card and
When I use this function to write to a csv file all the data
I made a simple graph to write MKV file. But I do not want
My code makes use of BufferedReader to read from a file [main.txt] and PrintWriter
use C#,want to upload excel file on google doc. bellow syntax use to upload
use strict; use warnings; use Time::HiRes qw(sleep); use Test::WWW::Selenium; use Test::More no_plan; use Test::Exception;
I have a java class that I use to create a text file from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.