opening and writing text to a file in a java program
Results 1 to 3 of 3

Thread: opening and writing text to a file in a java program

  1. #1
    Senior Member
    Join Date
    Aug 2001

    opening and writing text to a file in a java program

    im currently trying to make a program that can be used to edit the hosts file (yes i know someone here already has one written in VB, ive got a copy of it) but im interested in java programming and would like to know just how to write to the file and update it, ive got the start part of my code which i'll include as well, what i will be using is a couple of text areas and a button to click which adds the site and IP adress to the hosts file.

    with the java im thinking of cross compatability and having one version for my xp and linux systems if needed, yeah so any help would be cool :-)

    // hosts editing progam
    // created by Graeme Leach
    // feel free to distribute or modify as necessary

    // import packages
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class hostsEdit extends JApplet implements ActionListener {

    boolean keepEditing = true; // determine if you are still editing the hosts file
    String IPAddy = " "; // used to store the IP address of the site being added or blocked
    String siteAddy = " "; // used to store the site domain name / address

    // declare the GUi componenets here
    JLabel IPLabel, siteAddyLabel;
    JTextField IPField, siteAddyField;
    JButton AddEdit;

    public void init() {
    Container HostsProgram = getContentPane();
    HostsProgram.setLayout ( new FlowLayout ());

    // create label and field for site IP address
    IPLabel = new JLabel( "Enter to block site with localhost or map to an IP address:" );
    HostsProgram.add( IPLabel );
    IPField = new JTextField( 15 );
    IPField.setEditable( true );
    HostsProgram.add( IPField );

    // create label and field for site domain name / address
    siteAddyLabel = new JLabel( "Enter the domain name or full path of site to block or map:" );
    HostsProgram.add( siteAddyLabel );
    siteAddyField = new JTextField( 100 );
    siteAddyField.setEditable( true );
    HostsProgram.add( siteAddyField );

    // create the button to click to add the changes to the hosts file
    AddEdit = new JButton( "Add entry" );
    AddEdit.addActionListener( this );
    HostsProgram.add( AddEdit );

    public void ActionPerformed ( ActionEvent addSite ) {



  2. #2
    Senior Member
    Join Date
    Nov 2002
    To read from a file user a BufferedReader
    BufferedReader in = new BufferedReader(new FileReader(filename));
    String foo = in.readLine();
    The FileReader can only read one char at a time, so we use BufferedReader to read the whole line

    To write to a file use a PrintWriter
    PrintWriter writer = new PrintWriter(new FileWriter(filename));

    Again, the FileWriter can only write one character at a time, so we use the PrintWriter to write the whole line

    The only thing I cannot remember is how to append the file, and not overwrite it. Hopefully someone else can answer that!
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday

  3. #3
    Antionline Herpetologist
    Join Date
    Aug 2001
    Also, as you're extending class JApplet, you can't write to local files. So you should be looking to extend JFrame instead and put all your init() code into the hostsEdit() constructor.
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts