首先,你需要创建一个 JTable 对象,并在表格中输入数据。例如:
String[] columnNames = {"Name", "Age", "Gender"}Object[][] data = {{"Alice", 20, "Female"}, {"Bob", 25, "Male"}}JTable table = new JTable(data, columnNames)
然后,你需要使用 I/O 类库中的文件操作方法,将表格中的数据保存到文件中。例如,你可以使用 FileWriter 类来写入文件:
FileWriter writer = new FileWriter("table.txt")for (int i = 0i <table.getRowCount()i++) {for (int j = 0j <table.getColumnCount()j++) {
writer.write(table.getValueAt(i, j).toString())
writer.write("\t")
}
writer.write("\n")
}
writer.close()
上面的代码会将表格中的数据写入文件 "table.txt" 中,每行数据之间用制表符隔开,每列数据之间用换行符隔开。
注意,在使用文件操作方法时,你需要处理文件读写可能出现的异常。你可以使用 try-catch 语句将文件操作代码包装起来,以便在发生异常时能够正确处理。
这是一个简单的例子,你可以根据自己的需求来调整代码。例如,你可以使用其他的 I/O 类,比如 BufferedWriter 或 PrintWriter 等,来更方便地写入文件。你还可以使用其他的文件格式,比如 CSV、Excel 等,来保存数据。
这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考./**
* 五个按钮的故事,西西哈。
*/
import java.awt.*
import java.awt.event.*
import java.io.*
public class FileMessage extends Frame implements ActionListener
{
private static final long serialVersionUID = 10L
Dialog dia
private Panel p
private File fi
Process po=null
private String s
private TextArea ta
private FileDialog fd
private Button b1,b2,b3,b4,b5
private Button b6
public FileMessage()
{
super("文本文件处理")
setBackground( Color.LIGHT_GRAY )
setLocation(200,300)
setResizable( false)
setVisible( true)
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit( 0)
}
})
}
public void init()
{
ta=new TextArea("\n\n\n\n\n\t\t\t\t文本显示区")
ta.setSize(30,5)
ta.setEditable(false)
add( ta,"North")
p=new Panel()
add( p,"Center")
b1=new Button("浏览")
b2=new Button("保存")
b3=new Button("清空")
b4=new Button("关闭")
b5=new Button("独立打开")
b6=new Button("确定")
p.add(b1)
p.add(b2)
p.add(b3)
p.add(b4)
p.add(b5)
b1.addActionListener(this)
b2.addActionListener(this)
b3.addActionListener(this)
b4.addActionListener(this)
b5.addActionListener(this)
b6.addActionListener(this)
fd=new FileDialog(this,"请选择文件",FileDialog.LOAD)
fd.setDirectory("f:\\note")
pack()
dia=new Dialog(this,"注意",true)
dia.setLayout(new BorderLayout())
Panel p1=new Panel()
p1.add( b6)
dia.add(new Label(" 请先选择文件"),BorderLayout.CENTER)
dia.add( p1,BorderLayout.SOUTH)
dia.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dia.setVisible( false)
}
})
dia.setLocation(310,370)
dia.setSize(200,130)
}
public static void main(String[] args)
{
new FileMessage().init()
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
fd.setVisible(true)
s=fd.getDirectory()+fd.getFile()
fi=new File(s)
byte[] b=new byte[(int)fi.length()]
try
{
new FileInputStream(fi).read(b)
ta.setText(new String(b,0,(int)fi.length()))
}
catch(Exception e1){}
ta.setEditable(true)
}
else if(e.getSource()==b2)
{
try
{
if(ta.getText().equals("保存成功")||ta.getText() .equals( ""))
{}
else
{
new FileOutputStream(fi).write(ta.getText().getBytes())
ta.setText("保存成功")
ta.setEditable(false)
}
}
catch(FileNotFoundException e1)
{
ta.setText(e1.getMessage())
}
catch(IOException e1)
{
ta.setText("出现IOException异常")
}
}
else if(e.getSource()==b4)
System.exit(0)
else if(e.getSource()==b3)
{
ta.setText("")
ta.setEditable( false)
}
else if(e.getSource()==b5)
{
if(s==null)
{
dia.setVisible(true)
}
else
{
try
{
po=Runtime.getRuntime().exec("notepad.exe "+s)
}
catch(Exception ei)
{}
}
}
else if(e.getSource() ==b6)
{
dia.setVisible(false)
}
}
}
1、首先需要在记事本中编写一个“hello,下午好”程序。
2、编写完成后,保存该文件,并将文件名改为与类名相同。
3、把文件的格式从.txt改成.java文件。
4、更改完毕后,打开cmd指令(wins+R)。
5、输入javac,如果下方出现许多东西,则说明环境变量已经配置成功,否则就要去配置环境变量。
6、找到.java文件所在的位置,也在cmd中找到它(D盘)。
7、在cmd中进入这个.java文件所在的文件夹,输入javac 类名.java ,此时在.java文件所存在的地方出现同名.class文件。
8、再输入java 类名,下方便会运行出.java文件,输出“hello,下午好”。