here code for make new file ..
how i can make it to insert data to file and open it

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package files;
 import java.io.*;
 import java.util.Scanner;
public class Files{
  public static void main(String[] args) 
          throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter the file name to create : ");
  String file_name = in.readLine();
  File file = new File(file_name);

  boolean exist = file.createNewFile();
  if (!exist)
  {
  System.out.println("File already exists.");
  System.exit(0);
  }
  else
  {
  FileWriter fstream = new FileWriter(file_name);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write(in.readLine());
  //4out.close();
  System.out.println("File created successfully.");
  }
  }

}

Please edit the code and add proper indentations. All the statements should not start in column 1.

What is wrong with what the program does now?

its work .
and create file .. but not (.txt file)
i want to make it text file

If you want the filename to end with .txt, concatenate ".txt" to the end of the String: file_name.

i try with
String file_name.txt = in.readLine();
give me error .. i think i add .txt in wrong way

Concatenate to the String AFTER you have read a value into it.

i need to write to the file that i was created

public void addrecord(){
      Files record=new Files();
      Scanner input=new Scanner(System.in);
      while (input.hasNext()){
         input.nextInt();
      }

The code you just posted does not write to any file.
What is the code you just posted supposed to do?

its to add (write ) to same file that i created ..

If the file is created in the Files class, how will this code get to the file that was created in the Files class so it can write to it?

thanks Norm .. i make it now .. and its work ...
i will back to my previous threads ..to continuo with GUI

now i facing problem with gui . for this class :(

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mainme;
 import java.io.*;
 import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WriteToFile extends JFrame{
    private JTextField text;
    private JButton ok;
    private JFrame frame=new JFrame();
    public WriteToFile(){
        super("Save ");
         setLayout(new FlowLayout());
          text=new  JTextField (10);
  ok=new JButton("ok");
  add (text);
  add(ok);
  setSize(300,100);
  setVisible(true);

  TextHandler handler=new TextHandler();
  text.addActionListener(handler);

  ButtonHandler handlers=new ButtonHandler();
  ok.addActionListener(handlers);
    }
    private class TextHandler implements ActionListener{
        public void actionPerformed(ActionEvent event ){
            if(event.getSource()==text)
                event.getActionCommand();
        }
    }
      private class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent event ){
           event.getActionCommand();
            throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 String file_name = in.readLine();
  File file = new File(file_name);
  boolean exist = file.createNewFile();
  if (!exist)
  {
   JOptionPane.showMessageDialog(null, "File already exist");
  System.exit(0);
  }
  else
  {
  FileWriter fstream = new FileWriter(file_name);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write(in.readLine());
  //4out.close();
  System.out.println("File created successfully.");
  }

        }

    }}
  public static void main(String[] args) {
      new WriteToFile();

 /* InputStreamReader convert = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(convert);
        String instr;
        File outfile = new File(file_name);
   //     boolean append = true;
         FileWriter fout = new FileWriter(file_name);
            PrintWriter fileout = new PrintWriter(fout);
            System.out.print("Enter a string: ");
            instr = stdin.readLine();
            fileout.println(instr);
            fileout.flush();
            fileout.close();*/
        }

    }



  my problem with    throws IOException{ 
  and how i can implement event when user inter name of file in textfiled and press save button ( create that file ) ...

What is line 41 supposed to do? Did you mean to use a try/catch block?

i solved problem in line 41 by using try and catch ..
but still i have event ..i want to create file by event .. enter in textfield then save button

Why create the file until all the data has been entered and verified? Use the GUI for the user to enter the data, verify the data and save it in an arraylist. When the user is done entering the data and presses a button, create the file and write the contents of the arraylist to the file.

no .. i just in this step to create file .. and i make it like this ,,
this code its compiled but not whate i want .. i think i missing something

public class WriteToFile extends JFrame{
    private JTextField text;
    private JButton ok;
    private JFrame frame=new JFrame();
    public WriteToFile(){
        super("Save ");
         setLayout(new FlowLayout());
          text=new  JTextField (10);
  ok=new JButton("ok");
  add (text);
  add(ok);
  setSize(300,100);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
   TextHandler handler=new TextHandler();
  text.addActionListener(handler);
  ButtonHandler handlers=new ButtonHandler();
          ok.addActionListener(handlers);    
    }
    private class TextHandler implements ActionListener{
        public void actionPerformed(ActionEvent event ){
           ok.getAction();
         }
    }
      private class ButtonHandler implements ActionListener{   
        public void actionPerformed(ActionEvent event ){
          if(event.getSource()==ok)
             try { 
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 String file_name = in.readLine();
  File file = new File(file_name);
  boolean exist = file.createNewFile();
  if (!exist)
  {
   JOptionPane.showMessageDialog(null, "File already exist");
  System.exit(0);
  }
  else
  {
  FileWriter fstream = new FileWriter(file_name);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write(in.readLine());
  //4out.close();
  //System.out.println("File created successfully.");
  }
         }
       catch(IOException d) {
    System.out.println("Error writing to file " + d);
     }}}
  public static void main(String[] args) {
      new WriteToFile();}}

not whate i want .

Please explain what the code does and how that is different from what you want.

i make textfiled to enter name of file ... and button to do this action ...
its give me output gui button and textfiled but without active action .. when i press on button not do anything

See line 30. After you press OK it is waiting for you to type in file name on System.in

how i can join between textfield and button ,, i just have to make GUI not
when user write in textfield that should be the name of file , when he press on ok do the action and create file ..

BufferedReader in = new BufferedReader(new InputStreamReader(JOptionPane.showInputDialog("enter"));

whate i can use instead of this .

If the name of the file is in a textfield when the user presses the button, the listener method for the burron can get the name from the textfield using a textfield method.

because i think you get borid to answer me more 2 days ..
and i want quick clear answer because my teacher exepts me to submitted my project tomorow . as last dealine ...

Did you try what I suggested in my last post?

If all you want from the user is a filename, use a JOPtionPane method. Of a JFileChooser.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.