We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,063 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

write data to existing text file

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

}
3
Contributors
23
Replies
20 Hours
Discussion Span
1 Year Ago
Last Updated
24
Views
Question
Answered
programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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();
      }
programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by NormR1

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 ) ...
programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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();}}
programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

not whate i want .

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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 .

programing
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1236 seconds using 2.72MB