/    Sign up×
Community /Pin to ProfileBookmark

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?

to post a comment
Java

22 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — every time when code is run the counter variable "I" initialises to zero, hence doing the same thing over and over.
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — so how would i do it so that everytime i click save it saves in a new file or somethin like that
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — 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
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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

.... ....
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — <i>
</i>String baseFileName = "Salary";
int i = 0;
for (i=0; i &lt; 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?
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — any help?
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — 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.
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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?
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — how many text fields and in what format you want to save the data in the text fields
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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.
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — 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
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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?
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — if over writing is the concern then weren't u able to take care of that with setting a boolean value to false/true ?
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — 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?
Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2006 — 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....
Copy linkTweet thisAlerts:
@The_OneauthorMar 06.2006 — THANKS ALOT MAN. AM desperate. try as soon as u can please if u can. THANKS AGAIN
Copy linkTweet thisAlerts:
@Khalid_AliMar 07.2006 — 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.
[code=php]

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();
}
}

[/code]
Copy linkTweet thisAlerts:
@The_OneauthorMar 07.2006 — ok that dude. i owe u. So i can also edit this to do the opposite right of opening?
Copy linkTweet thisAlerts:
@Khalid_AliMar 07.2006 — huh..dude?

[b] Read the API[/b]

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);
Copy linkTweet thisAlerts:
@The_OneauthorMar 07.2006 — i got a class as shown below:
<i>
</i>public class try1 {

public static void einfo() {

<i> </i>final int buttonWidth = 65;
<i> </i>final int buttonHeight = 25;

<i> </i> JButton saveButton = new JButton ("Save");
<i> </i> saveButton.setPreferredSize (new Dimension (buttonWidth, buttonHeight));



<i> </i> JLabel blankLabel = new JLabel (" "); // blank lines

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

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

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

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


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

<i> </i> info.pack ();
<i> </i> 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?
Copy linkTweet thisAlerts:
@Khalid_AliMar 07.2006 — 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......
×

Success!

Help @The_One spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...