Click to See Complete Forum and Search --> : file writer


The One
03-05-2006, 04:30 PM
i got the following bit of code:


[Code]
String baseFileName = "Salary";
int i = 0;
String writer;

String tempFileName = baseFileName + (i++) + ".txt";
writer = tempFileName;
PrintWriter out = new PrintWriter (writer);
[code]

i want everytime i press save it saves it to a new .txt file. but right now it is just saving to one txt file and not doin like an array. WATS WRONG?

Khalid Ali
03-05-2006, 08:38 PM
every time when code is run the counter variable "I" initialises to zero, hence doing the same thing over and over.

The One
03-06-2006, 01:01 AM
so how would i do it so that everytime i click save it saves in a new file or somethin like that

Khalid Ali
03-06-2006, 01:21 AM
you can look for files in a directory, see which file has the largest number in it and then from that number increment 1 and create a new file

The One
03-06-2006, 01:22 AM
ok i did it by saying: boolean appened = true;
but right now its saving in one column.
EG
name
...
name
...

It keeps saving downwards.


So how do i save in more that two columns and more.
n
EG:
name name
.... ....

The One
03-06-2006, 07:43 AM
String baseFileName = "Salary";
int i = 0;
for (i=0; i < 10; i++);
String tempFileName = baseFileName + (i++) + ".txt";
FileWriter writer = new FileWriter("tempFileName.txt");
PrintWriter out = new PrintWriter (writer);


Doesnt work above.

Would you be able to show me a basic file chooser that i can replace in ma program instead of wat am tryin to use above. Cause a file chooser in which when i press save it asks where i want to save it 2 and the name. IT would be best.

Or if u have any other options. see ma program there are textfields. i would like to save them in a file. Its a salary sheet so for each employee i would like a different file?

The One
03-06-2006, 10:10 AM
any help?

Khalid Ali
03-06-2006, 10:49 AM
I gave you the basic filechooser part of code sometime back.....explain what is your requirement, just as if you were giving me this task to complet.

The One
03-06-2006, 12:00 PM
ok say iv a gui with textfields in which i can type. i then want to be able after clicking the save button to be able to choose where to save it and wat name to give it?
Youve seen wat i am currently usin for save is it just saves in one file name. so i need to be able to select where to save??? any ideas or code?

Khalid Ali
03-06-2006, 12:53 PM
how many text fields and in what format you want to save the data in the text fields

The One
03-06-2006, 01:02 PM
iv got about 20 textfields. in the first 5 its for typing characters and the next 15 its for numbers. is that wat u want to know then.

Khalid Ali
03-06-2006, 01:05 PM
ok...u want to save contents of all 20 text fields in a file?
How u want data to be saved...like each text field with its name corresponding to its value on each line etc etc..give me details

The One
03-06-2006, 01:07 PM
yup. right now i can do that but if i imput new data it overides it so i would like to be able to choose where to save it and wat name to give it?

Khalid Ali
03-06-2006, 02:18 PM
if over writing is the concern then weren't u able to take care of that with setting a boolean value to false/true ?

The One
03-06-2006, 02:21 PM
ya but after a while the file will get to big. and it still saves in one file. so i cant select which one to load. so i need to be able to save in different ones. jfilechooser but i cant seem to understand it. so i need help doin that

The One
03-06-2006, 04:11 PM
so u think u can help cos i really need it? if i cud replace the old code i have for saving in one file so to now be able to save in multiple files with a file chooser. In other words i click save as and a window opens sayin where u want 2 and wat name?

Khalid Ali
03-06-2006, 04:28 PM
ok..I will write up some code for you, it will have couple of text fields on it and will save the data by allowing user to name the file in which to save and where......you should be able to use it for a million text fields cus functionality will remain the same..not sure how sonne I can do that though....

The One
03-06-2006, 04:30 PM
THANKS ALOT MAN. AM desperate. try as soon as u can please if u can. THANKS AGAIN

Khalid Ali
03-06-2006, 07:15 PM
save the following code in a file named SaveFiel.java and compile and run it. Add as many text fields as u want..just follow how they are used in the code.


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
*
* Author:
* Date: Mar 6, 2006
* Time: 4:32:45 PM
*/
public class SaveFile extends JFrame implements ActionListener {
JTextField jtfName, jtfSalary;
JButton jbSave;

/**
* Constructor to start the processing
*/
public SaveFile() {
initApp();
}

/**
* Initilaizes the GUI
*/
public void initApp() {
this.setTitle("Save File GUI");
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
JLabel lblName = new JLabel("Name : ");
jtfName = new JTextField("", 20);
jtfName.setName("Name");

JLabel lblSalary = new JLabel("Salary :");
jtfSalary = new JTextField("", 20);
jtfSalary.setName("Salary");

jbSave = new JButton("Save As");
jbSave.addActionListener(this);

c.add(lblName);
c.add(jtfName);
c.add(lblSalary);
c.add(jtfSalary);
c.add(jbSave);

//close frame when user clicks on X at the top right corner
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
this.setSize(300, 150);
this.setVisible(true);
}

/**
* @param e
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {//user has clicked on save as button
//now get text field values and then show the save as dialogue
String data = "";
data += jtfName.getName() + "=" + jtfName.getText();
data += ", "; //comma separates to values
data += jtfSalary.getName() + "=" + jtfSalary.getText();

//show save as dilogue
File newFile = saveFile(data);
//now u can use that newly saved file for further processing if needed.
//uncomment lines below to clear the text fields after they are saved
//clearTextFields();
}
}

/**
* This method will allow you to save data from text fields into a file by showing you the
* Save As file dialogue.
* Once file is saved it returns a handle to that newly saved file.
*
* @param data
* @return File
*/
public File saveFile(String data) {

File file; // new file object that we want to save this data as
file = null;

JFileChooser fd = new JFileChooser();
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);

int result = fd.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fd.getSelectedFile();
if (file == null) {
JOptionPane.showMessageDialog(null, "Invalid file name", "Invalid file name", JOptionPane.ERROR_MESSAGE);
} else {

try {
//FileWriter writer = new FileWriter(file);
BufferedWriter br = new BufferedWriter(new FileWriter(file));
br.write(data);
br.flush();
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return file;
}

/**
* Clears up text fields.
* If a field is not intialized this method will throw a null pointer exception.
* Thats developers responsibility to set more checks for such errors.
*/
public void clearTextFields() {
jtfName.setText("");
jtfSalary.setText("");
}

/**
* @param args
*/
public static void main(String[] args) {
new SaveFile();
}
}

The One
03-07-2006, 07:35 AM
ok that dude. i owe u. So i can also edit this to do the opposite right of opening?

Khalid Ali
03-07-2006, 08:26 AM
huh..dude?

Read the API

ANd when u do that u will see that FileChooser has another method called
showOpenDialog(this);

and u need to call this method to shoe open file dialogue, rest of the code will prety much remain the same.

Here change this l ine to
int result = fd.showSaveDialog(this);
to this
int result = fd.showOpenDialog(this);

The One
03-07-2006, 09:50 AM
i got a class as shown below:

public class try1 {

public static void einfo() {

final int buttonWidth = 65;
final int buttonHeight = 25;

JButton saveButton = new JButton ("Save");
saveButton.setPreferredSize (new Dimension (buttonWidth, buttonHeight));



JLabel blankLabel = new JLabel (" "); // blank lines

// Date: Year Drop-down menu
final JComboBox yearChoice = new JComboBox();
int i;
i = 1940;
for (i = 1940; i < 2007; i++) {
yearChoice.addItem(i);
yearChoice.setSelectedItem(i);
}

//Contact Information
JLabel contactLabel = new JLabel (" Contact Information:");
JLabel telephoneLabel = new JLabel (" Telephone:");
final JTextField telephoneTextField = new JTextField(10);
JLabel cellphoneLabel = new JLabel (" Cell Phone:");
final JTextField cellphoneTextField = new JTextField(15);
JLabel emailLabel = new JLabel (" Email Address:");
final JTextField emailTextField = new JTextField(15);
JLabel haddressLabel = new JLabel (" House Address:");
final JTextField haddressTextField = new JTextField(15);

//Font
Font largeArialFont = new Font ("Arial", Font.BOLD, 16);
contactLabel.setFont (largeArialFont);

final JFrame info = new JFrame ("Employee Information");
info.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
info.setSize (150, 500);
info.setLayout (new GridLayout (8,2));
info.setLocationRelativeTo (null);


info.add (yearChoice);
info.add (contactLabel);
info.add (telephoneLabel);
info.add (telephoneTextField);
info.add (cellphoneLabel);
info.add (cellphoneTextField);
info.add (emailLabel);
info.add (emailTextField);
info.add (haddressLabel);
info.add (haddressTextField);

info.pack ();
info.show ();
}


is there a way so that i can call the save class to do the saving.?
or do i have to use the save class and just do as u did withe the labels and textsfields???
or do u have another idea?

Khalid Ali
03-07-2006, 10:30 AM
no I don't have any other idea, I have given you a 100% working example where you can add as many text fields by following the same logic. If this does not help and then I don't know what will. I have gone a great lengths to help you out, but you still pose the same question in different ways......