The Artima Developer Community
Sponsored Link

Java Answers Forum
File Writing and Reading

1 reply on 1 page. Most recent reply: Jun 29, 2002 11:58 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
DAYO

Posts: 22
Nickname: done
Registered: Jun, 2002

File Writing and Reading Posted: Jun 29, 2002 4:52 AM
Reply to this message Reply
Advertisement
I am trying to write the content of the textfields
to a file on my computer.
i want to write the content of the textfield to a text file.
When it has been written the content should look like
this:
content of Textfield1 followed by new line x'ter
content of Textfield2 followed by new line x'ter
content of Textfield3 followed by new line x'ter
content of Textfield4 followed by new line x'ter
end of file x'ter.
Also, i want to be able to write content of a Textfile
to the Textfield.Like this:
Textfield1=line1
Textfield2=line2
Textfield3=line3
Textfield3=line4
How can i achieve this?
Here is a section of a program i have written
package DAYOSPACKAGE.test1;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
//package DAYOSPACKAGE.test1;
public class testing2 extends JFrame
{ private JTextField T1[];
private JButton B1,B2;
private JLabel L1[];
private JPanel P1,P2,P3;
private ObjectOutputStream output;
public testing2()
{ T1=new JTextField[4];
for(int i=0;i<T1.length;i++)
{ T1=new JTextField();
}

L1=new JLabel[4];
for(int i=0;i<L1.length;i++)
{ L1=new JLabel(" "+i);
}
....etc


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: File Writing and Reading Posted: Jun 29, 2002 11:58 AM
Reply to this message Reply


import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class JTextFieldTest extends JFrame implements ActionListener{

private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;

private String fileName = "UserData.txt";

public JTextFieldTest(){
super("JTextFieldTest");
init();
}

public static void main(String[] args){
JTextFieldTest test = new JTextFieldTest();
}

public void init(){

textField1 = new JTextField();
textField2 = new JTextField();
textField3 = new JTextField();
textField4 = new JTextField();
JPanel textFieldPanel = new JPanel();
textFieldPanel.setLayout(new GridLayout(4,2));
textFieldPanel.add(new JLabel("Name: ",SwingConstants.RIGHT));
textFieldPanel.add(textField1);
textFieldPanel.add(new JLabel("Address: ",SwingConstants.RIGHT));
textFieldPanel.add(textField2);
textFieldPanel.add(new JLabel("City: ",SwingConstants.RIGHT));
textFieldPanel.add(textField3);
textFieldPanel.add(new JLabel("State: ",SwingConstants.RIGHT));
textFieldPanel.add(textField4);

getContentPane().add(textFieldPanel,BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());

JButton saveButton = new JButton("Save");
saveButton.addActionListener(this);
buttonPanel.add(saveButton);

JButton loadButton = new JButton("Load");
loadButton.addActionListener(this);
buttonPanel.add(loadButton);

JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);

JButton exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);

getContentPane().add(buttonPanel,BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
show();

}

public void actionPerformed(ActionEvent ae){
String actionCommand = ae.getActionCommand();
if (actionCommand.compareTo("Save") == 0) {
save();
}else if (actionCommand.compareTo("Load") == 0) {
load();
}else if (actionCommand.compareTo("Clear") == 0) {
clear();
}else if (actionCommand.compareTo("Exit") == 0) {
exit();
}
}

public void save(){
try{
FileWriter fw = new FileWriter(fileName);
fw.write(textField1.getText() + "\n");
fw.write(textField2.getText() + "\n");
fw.write(textField3.getText() + "\n");
fw.write(textField4.getText() + "\n");
fw.close();
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}

public void load(){
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
textField1.setText(br.readLine());
textField2.setText(br.readLine());
textField3.setText(br.readLine());
textField4.setText(br.readLine());
fr.close();
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}

public void clear(){
textField1.setText("");
textField2.setText("");
textField3.setText("");
textField4.setText("");
}

public void exit(){
System.exit(0);
}
}

Flat View: This topic has 1 reply on 1 page
Topic: Change the Icon of the JFrame Previous Topic   Next Topic Topic: reading pixel value using mouse click event

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use