|
-
December 26th, 2002, 01:18 PM
#1
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();
}
-
December 26th, 2002, 05:33 PM
#2
To read from a file user a BufferedReader
ex.
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
ex.
PrintWriter writer = new PrintWriter(new FileWriter(filename));
writer.print(foo);
or
writer.println(foo);
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
Visit alastairgrant.ca
-
December 26th, 2002, 06:30 PM
#3
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.
Cheers,
cgkanchi
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|