1.11M Members

write data to existing text file

 
0
 

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.");
  }
  }

}
 
0
 

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?

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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();
      }
 
0
 

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

 
0
 

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

 
0
 

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?

 
0
 

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

Question Answered as of 2 Years Ago by NormR1
 
0
 

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 ) ...
 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 

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();}}
 
0
 

not whate i want .

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

 
0
 

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

 
0
 

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

 
0
 

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 .

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article