将 JTextField 中的数据保存在 .csv 文件的列中
Save data from JTextField in columns in .csv File
我编写了以下程序以在四个 JTextFields 中获取用户输入,然后将其保存在 .csv 文件中。但是我在不同的行中得到它。我希望 .csv 文件中的输出是从一列到另一列
我的代码如下:
public class panel extends FirstClick {
final JTextField items;
final JTextField number;
final JTextField cost;
final JTextField amount;
public panel() {
JFrame myFrame = new JFrame();
myFrame.setLayout( new FlowLayout() );
myFrame.setTitle("GUI Demo - ISTE-121");
myFrame.pack();
myFrame.setLocation(600,300);
myFrame.setSize(400,200);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JPanel order = new JPanel();
order.setLayout(new GridLayout(5,5,2,2));
myFrame.add(order, BorderLayout.CENTER);
order.add(new JLabel("Item no:", SwingConstants.RIGHT));
order.add(items = new JTextField(3));
order.add(new JLabel("Number of:", SwingConstants.RIGHT));
order.add(number = new JTextField(3));
order.add(new JLabel("Cost", SwingConstants.RIGHT));
order.add(cost = new JTextField(3));
order.add(new JLabel("Amount owed:", SwingConstants.RIGHT));
order.add(amount = new JTextField(10));
amount.setEditable(false);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1,1,2,2));
myFrame.add(buttons ,BorderLayout.SOUTH);
JButton calculate;
buttons.add(calculate=new JButton("Calculate"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
double cost1 = new Double(cost.getText().trim()).doubleValue();
double number1 = new Double(number.getText().trim()).doubleValue();
double result = cost1*number1;
amount.setText(String.valueOf(result));
}
});
JButton save;
buttons.add(save = new JButton("Save"));
save.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// The name of the file to open.
String fileName = "temp1.csv";
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName, true);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write(items.getText());
bufferedWriter.newLine();
bufferedWriter.write(number.getText());
bufferedWriter.newLine();
bufferedWriter.write(cost.getText());
bufferedWriter.newLine();
bufferedWriter.write(amount.getText());
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}}
);
JButton clear;
buttons.add(clear=new JButton("Clear"));
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
items.setText(null);
number.setText(null);
cost.setText(null);
amount.setText(null);
}});
JButton exit;
buttons.add(exit= new JButton("Exit"));
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
}
}
public class FirstClick {
public static void main(String args[]) {
panel second = new panel();
} // end main
}
你在你的字段之间写了一个换行符
删除 newLine() 调用并替换为写入“;”
bufferedWriter.write(items.getText());
bufferedWriter.write(';');
bufferedWriter.write(number.getText());
bufferedWriter.write(';');
试试这个:
在 CSV 中复制此模式:
"Label","Value"
FileWriter fw = new FileWriter("/CSV/data.csv");
BufferedWriter bufferedWriter = new BufferedWriter(fw);
bufferedWriter.write("\"ItemCode\","+"\""+items.getText()+"\"");
bufferedWriter.write("\"ItemNumber\","+"\""+number.getText()+"\"");
bufferedWriter.write("\"ItemCost\","+"\""+cost.getText()+"\"");
bufferedWriter.write("\"AmountOwned\","+"\""+amount.getText()+"\"");
bufferedWriter.close();
我编写了以下程序以在四个 JTextFields 中获取用户输入,然后将其保存在 .csv 文件中。但是我在不同的行中得到它。我希望 .csv 文件中的输出是从一列到另一列
我的代码如下:
public class panel extends FirstClick {
final JTextField items;
final JTextField number;
final JTextField cost;
final JTextField amount;
public panel() {
JFrame myFrame = new JFrame();
myFrame.setLayout( new FlowLayout() );
myFrame.setTitle("GUI Demo - ISTE-121");
myFrame.pack();
myFrame.setLocation(600,300);
myFrame.setSize(400,200);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JPanel order = new JPanel();
order.setLayout(new GridLayout(5,5,2,2));
myFrame.add(order, BorderLayout.CENTER);
order.add(new JLabel("Item no:", SwingConstants.RIGHT));
order.add(items = new JTextField(3));
order.add(new JLabel("Number of:", SwingConstants.RIGHT));
order.add(number = new JTextField(3));
order.add(new JLabel("Cost", SwingConstants.RIGHT));
order.add(cost = new JTextField(3));
order.add(new JLabel("Amount owed:", SwingConstants.RIGHT));
order.add(amount = new JTextField(10));
amount.setEditable(false);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1,1,2,2));
myFrame.add(buttons ,BorderLayout.SOUTH);
JButton calculate;
buttons.add(calculate=new JButton("Calculate"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
double cost1 = new Double(cost.getText().trim()).doubleValue();
double number1 = new Double(number.getText().trim()).doubleValue();
double result = cost1*number1;
amount.setText(String.valueOf(result));
}
});
JButton save;
buttons.add(save = new JButton("Save"));
save.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// The name of the file to open.
String fileName = "temp1.csv";
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName, true);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write(items.getText());
bufferedWriter.newLine();
bufferedWriter.write(number.getText());
bufferedWriter.newLine();
bufferedWriter.write(cost.getText());
bufferedWriter.newLine();
bufferedWriter.write(amount.getText());
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}}
);
JButton clear;
buttons.add(clear=new JButton("Clear"));
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
items.setText(null);
number.setText(null);
cost.setText(null);
amount.setText(null);
}});
JButton exit;
buttons.add(exit= new JButton("Exit"));
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
}
}
public class FirstClick {
public static void main(String args[]) {
panel second = new panel();
} // end main
}
你在你的字段之间写了一个换行符
删除 newLine() 调用并替换为写入“;”
bufferedWriter.write(items.getText());
bufferedWriter.write(';');
bufferedWriter.write(number.getText());
bufferedWriter.write(';');
试试这个: 在 CSV 中复制此模式:
"Label","Value"
FileWriter fw = new FileWriter("/CSV/data.csv");
BufferedWriter bufferedWriter = new BufferedWriter(fw);
bufferedWriter.write("\"ItemCode\","+"\""+items.getText()+"\"");
bufferedWriter.write("\"ItemNumber\","+"\""+number.getText()+"\"");
bufferedWriter.write("\"ItemCost\","+"\""+cost.getText()+"\"");
bufferedWriter.write("\"AmountOwned\","+"\""+amount.getText()+"\"");
bufferedWriter.close();