Using Java to implement HTML files instead of database storage data

Source: Internet
Author: User
Tags gettext trim

Java in the writing of some graphical small program, sometimes to store a small amount of data, if the use of JDBC to connect the database, it will slow down the program, and, in addition, we can use Java files to save data in lieu of the database, so that not only can realize the function of storage, And without considering the frequent access to the data, you can define the file as an HTML file and display the stored data in a tabular format so that you can see the data directly in the Web page, and here is an example of a small file store data I wrote:

Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.sql.*;
Import java.io.*;
Import javax.swing.*;
public class Filework extends JFrame implements ActionListener {
JPanel p;
JButton b0,b1,b2;
JTable tab;
Object my[][] = new OBJECT[50][3];
int a[]=new int[50];
int b[]=new int[50];
int j=0,rows=0;
int k=0,shu=0;
String Name,age;
String title[] = {"Name", "Age", "height"};
BufferedReader in;
int i = 0;
Public Filework () {
Super ("Data Reincarnation");
This.setsize (300, 300);
This.setdefaultcloseoperation (Jframe.exit_on_close);
p = new JPanel ();
tab = new JTable (my, title);
B0 = new JButton ("Add");
B1 = new JButton ("open");
b2 = new JButton ("Save");
B1.addactionlistener (this);
B2.addactionlistener (this);
B0.addactionlistener (this);
P.add (B0);
P.add (B1);
P.add (B2);
This.getcontentpane (). Add (New JScrollPane (tab), "Center");
This.getcontentpane (). Add (P, "South");
This.setvisible (TRUE);
}
public void FileOpen () {
try{
JFileChooser choose=new JFileChooser (".");
int Sis=choose.showopendialog (this);
String name=choose.getselectedfile () + "";
if (sis==jfilechooser.approve_option) {
In=new BufferedReader (new FileReader (name));
String Over=null;
String Line;
while ((Line=in.readline ())!=null) {
Over=over+line;
}
for (int i=0;i<a.length;i++) {
A[i]=over.indexof ("<td>") +4;
B[i]=over.indexof ("</td>");
My[rows][k]=over.substring (A[i],b[i]);
Over=over.substring (B[i]+4,over.length ());
k=k+1;
if (k==3) {k=0;rows=rows+1;}
}
}
}catch (Exception IE) {}
}
public static void Main (string[] args) {
Filework filework = new Filework ();
}

public void actionperformed (ActionEvent e) {
if (e.getsource () = = B1) {
FileOpen ();
}
if (e.getsource () = = B2) {
JFileChooser save=new JFileChooser (".");
int result=save.showsavedialog (NULL);
String s1=save.getselectedfile () + "";
if (result==jfilechooser.approve_option) {
try {
OutputStreamWriter out = new OutputStreamWriter (new
FileOutputStream (S1));
String s2= "s2=s2+ "<table border=" 1 "width=" 50% "bordercolor=" green "align=" center ">";
s2=s2+ "<tr>";
s2=s2+ "<th>" + "name" + "</th>";
s2=s2+ "<th>" + "age" + "</th>";
s2=s2+ "<th>" + "height" + "</th>";
s2=s2+ "</tr>";
for (int shu=0;shu<rows;shu++)
{
s2=s2+ "<tr align=" center ">";
s2=s2+ "<td>" +my[shu][0].tostring () trim () + "</td>";
s2=s2+ "<td>" +my[shu][1].tostring () trim () + "</td>";
s2=s2+ "<td>" +my[shu][2].tostring () trim () + "</td>";
s2=s2+ "</tr>";
}
s2=s2+ "</table></body>Out.write (S2);
Out.close ();
for (int i1=0;i1<50;i1++) {
My[i1][0]= "";
My[i1][1]= "";
My[i1][2]= "";
}
rows=0;
This.repaint ();
Joptionpane.showmessagedialog (NULL, "Save data Success!"),
"Information", joptionpane.question_message);
catch (Exception Ex1) {
}
}
}
if (E.getsource () ==b0) {
JTextField Message[]=new jtextfield[6];
Message[0]=new JTextField ("Please enter your name:");
Message[0].seteditable (FALSE);
Message[1]=new JTextField ();
Message[2]=new JTextField ("Please enter Age:");
Message[2].seteditable (FALSE);
Message[3]=new JTextField ();
Message[4]=new JTextField ("Please enter height:");
Message[4].seteditable (FALSE);
Message[5]=new JTextField ();
String str[]={"Confirm", "Cancel"};
int Go=joptionpane.showoptiondialog (NULL, message, "Add User",
Joptionpane.yes_option, joptionpane.information_message, NULL, str, str[0]);
if (go==0) {
if (Message[1].gettext (). Equals ("") &&
Message[3].gettext (). Equals ("") &&
Message[5].gettext (). Equals ("")) {
Joptionpane.showmessagedialog (NULL, "Data cannot be empty", "ERROR", Joptionpane.error_message);}
else{
My[rows][0]=message[1].gettext ();
My[rows][1]=message[3].gettext ();
My[rows][2]=message[5].gettext ();
This.repaint (); rows=rows+1;}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.