opening and writing text to a file in a java program
hi,
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 127.0.0.1 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 ) {
openFile();
}