1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      java存儲(chǔ)器管理代碼,內(nèi)存管理java

      如何在java里長(zhǎng)期存儲(chǔ)數(shù)據(jù) 不要數(shù)據(jù)庫(kù)的那種

      長(zhǎng)期存儲(chǔ)數(shù)據(jù),即把數(shù)據(jù)(如內(nèi)存中的)保存到可永久保存的存儲(chǔ)設(shè)備中(如硬盤、U盤),也就是人們常說的持久化。

      創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的嶺東網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

      常用持久化的方案有數(shù)據(jù)庫(kù)、XML文件和文件存儲(chǔ)。

      數(shù)據(jù)庫(kù)是按照數(shù)據(jù)結(jié)構(gòu)來存儲(chǔ)和管理數(shù)據(jù)的倉(cāng)庫(kù),后文不再做詳細(xì)介紹。

      XML是可擴(kuò)展標(biāo)記語(yǔ)言,最早是為了簡(jiǎn)化Internet的文檔數(shù)據(jù)傳輸,它提供統(tǒng)一的語(yǔ)法格式來描述數(shù)據(jù)的結(jié)構(gòu),通常XML文件用于一些少量且無特殊類型要求的文本存儲(chǔ)。示例代碼使用W3C標(biāo)準(zhǔn)的接口生成XML:

      import?java.io.FileOutputStream;

      import?java.io.PrintWriter;

      import?javax.xml.parsers.DocumentBuilderFactory;

      import?javax.xml.transform.OutputKeys;

      import?javax.xml.transform.Transformer;

      import?javax.xml.transform.TransformerFactory;

      import?javax.xml.transform.dom.DOMSource;

      import?javax.xml.transform.stream.StreamResult;

      import?org.w3c.dom.Document;

      import?org.w3c.dom.Element;

      public?class?$?{

      public?static?void?main(String[]?args)?throws?Exception?{

      Document?document?=?DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

      //創(chuàng)建根節(jié)點(diǎn)為students的XML文件

      Element?students?=?document.createElement("students");

      document.appendChild(students);

      //在根節(jié)點(diǎn)下創(chuàng)建一個(gè)子節(jié)點(diǎn)學(xué)生

      Element?student?=?document.createElement("student");

      students.appendChild(student);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生姓名,值為張三

      Element?name?=?document.createElement("name");

      name.appendChild(document.createTextNode("張三"));

      student.appendChild(name);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生年齡,值為18

      Element?age?=?document.createElement("age");

      age.appendChild(document.createTextNode("18"));

      student.appendChild(age);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生編號(hào),值為150101

      Element?number?=?document.createElement("number");

      number.appendChild(document.createTextNode("150101"));

      student.appendChild(number);

      //在根節(jié)點(diǎn)下創(chuàng)建第二個(gè)子節(jié)點(diǎn)學(xué)生

      student?=?document.createElement("student");

      students.appendChild(student);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生姓名,值為李四

      name?=?document.createElement("name");

      name.appendChild(document.createTextNode("李四"));

      student.appendChild(name);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生年齡,值為20

      age?=?document.createElement("age");

      age.appendChild(document.createTextNode("20"));

      student.appendChild(age);

      //創(chuàng)建節(jié)點(diǎn)學(xué)生編號(hào),值為150102

      number?=?document.createElement("number");

      number.appendChild(document.createTextNode("150102"));

      student.appendChild(number);

      //將XML文件保存到硬盤

      Transformer?transformer?=?TransformerFactory.newInstance().newTransformer();

      transformer.setOutputProperty(OutputKeys.ENCODING,?"utf-8");

      transformer.setOutputProperty(OutputKeys.INDENT,?"yes");

      PrintWriter?writer?=?new?PrintWriter(new?FileOutputStream("/home/test.xml"));

      transformer.transform(new?DOMSource(document),?new?StreamResult(writer));

      }

      }

      無論是數(shù)據(jù)庫(kù)還是XML文件,它們都使用了能讓數(shù)據(jù)快速方便進(jìn)出的標(biāo)準(zhǔn)規(guī)范。其它文件如propeties、json,都可以使用類似XML的方式來打包數(shù)據(jù),然后通過Java豐富的io流接口保存到磁盤中。

      Java編寫圖書管理系統(tǒng),使用XML存儲(chǔ)

      import?java.io.File;

      import?java.io.FileOutputStream;

      import?java.util.ArrayList;

      import?java.util.List;

      import?java.util.Scanner;

      import?org.dom4j.Document;

      import?org.dom4j.DocumentHelper;

      import?org.dom4j.Element;

      import?org.dom4j.io.OutputFormat;

      import?org.dom4j.io.SAXReader;

      import?org.dom4j.io.XMLWriter;

      public?class?Book?{

      private?int?no;

      private?String?name;

      private?double?value;

      public?Book()?{

      }

      public?Book(int?no,?String?name,?double?value)?{

      this.no?=?no;

      this.name?=?name;

      this.value?=?value;

      }

      public?double?getValue()?{

      return?value;

      }

      public?void?setValue(double?value)?{

      this.value?=?value;

      }

      public?String?getName()?{

      return?name;

      }

      public?void?setName(String?name)?{

      this.name?=?name;

      }

      public?int?getNo()?{

      return?no;

      }

      public?void?setNo(int?no)?{

      this.no?=?no;

      }

      }

      class?BookList?{

      private?ListBook?bookList;

      public?BookList()?{

      bookList?=?readXML();

      }

      public?long?getCount()?{

      return?bookList.size();

      }

      public?ListBook?getBookList()?{

      return?bookList;

      }

      public?void?setBookList(ListBook?bookList)?{

      this.bookList?=?bookList;

      }

      public?void?add(Book?book)?{

      bookList.add(book);

      }

      public?boolean?delete(String?name)?{

      Book?book?=?query(name);

      return?bookList.remove(book);

      }

      public?void?update(Book?bookBefore,?Book?bookAfter)?{

      bookList.remove(bookBefore);

      add(bookAfter);

      }

      public?Book?query(String?name)?{

      Book?temp?=?null;

      for?(Book?book?:?bookList)?{

      if?(book.getName().equals(name))?{

      temp?=?book;

      }

      }

      return?temp;

      }

      public?synchronized?void?writeXmlDocument(Book?book)?{

      try?{

      File?file?=?new?File("D:\\book.xml");

      Document?document?=?null;

      Element?root?=?null;

      if?(!file.exists())?{

      //?新建student.xml文件并新增內(nèi)容

      document?=?DocumentHelper.createDocument();

      root?=?document.addElement("Books");//添加根節(jié)點(diǎn)???

      }?else?{

      SAXReader?saxReader?=?new?SAXReader();

      document?=?saxReader.read(file);

      root?=?document.getRootElement();//獲得根節(jié)點(diǎn)

      }

      Element?secondRoot?=?root.addElement("Book");//二級(jí)節(jié)點(diǎn)???

      //為二級(jí)節(jié)點(diǎn)添加屬性,屬性值為對(duì)應(yīng)屬性的值???

      secondRoot.addElement("no").setText(book.getNo()?+?"");

      secondRoot.addElement("name").setText(book.getName()?+?"");

      secondRoot.addElement("value").setText(book.getValue()?+?"");

      OutputFormat?format?=?OutputFormat.createPrettyPrint();

      format.setEncoding("GBK");

      XMLWriter?writer?=?new?XMLWriter(new?FileOutputStream("D:\\book.xml"),?format);

      writer.write(document);

      writer.close();

      document.clearContent();

      }?catch?(Exception?e)?{

      e.printStackTrace();

      }

      }

      public?synchronized?ListBook?readXML()?{

      ListBook?list?=?new?ArrayListBook();//創(chuàng)建list集合???

      File?file?=?null;

      try?{

      file?=?new?File("D:\\book.xml");//讀取文件???

      if?(file.exists())?{

      SAXReader?saxReader?=?new?SAXReader();

      Document?document?=?saxReader.read(file);

      List?nodeList?=?document.selectNodes("Books/Book");

      for?(int?i?=?0;?i??nodeList.size();?i++)?{

      Element?el?=?(Element)?nodeList.get(i);

      Book?book?=?new?Book();

      book.setNo(Integer.parseInt(el.elementText("no")));

      book.setName(el.elementText("name"));

      book.setValue(Double.parseDouble(el.elementText("value")));

      list.add(book);

      }

      }

      }?catch?(Exception?e)?{

      e.printStackTrace();

      }

      return?list;

      }

      }

      class?Test?{

      public?static?void?main(String?args[])?{

      BookList?bl?=?new?BookList();

      boolean?bBreak?=?true;

      while?(bBreak)?{

      System.out.println("請(qǐng)輸入操作代碼:");

      System.out.println("1:添加?2:刪除?3:修改?4:查詢?5:書籍統(tǒng)計(jì)?6:退出");

      Scanner?sc?=?new?Scanner(System.in);

      int?code?=?sc.nextInt();

      if?(code?==?1)?{

      System.out.println("請(qǐng)輸入編號(hào)");

      int?no?=?sc.nextInt();

      System.out.println("請(qǐng)輸入書名");

      String?name?=?sc.next();

      System.out.println("請(qǐng)輸入售價(jià)");

      double?value?=?sc.nextDouble();

      Book?book?=?new?Book(no,?name,?value);

      bl.add(book);

      bl.writeXmlDocument(book);

      }?else?if?(code?==?2)?{

      System.out.println("請(qǐng)輸入要?jiǎng)h除的書籍名");

      String?name?=?sc.next();

      if?(bl.delete(name))?{

      System.out.println("刪除成功");

      }?else?{

      System.out.println("書籍不存在");

      }

      }?else?if?(code?==?3)?{

      System.out.println("請(qǐng)輸入要修改的書籍名");

      String?name?=?sc.next();

      Book?bookBefore?=?bl.query(name);

      System.out.println("請(qǐng)輸入新的編號(hào)");

      int?newNo?=?sc.nextInt();

      System.out.println("請(qǐng)輸入新的書名");

      String?newName?=?sc.next();

      System.out.println("請(qǐng)輸入新的售價(jià)");

      double?value?=?sc.nextDouble();

      Book?bookAfter?=?new?Book(newNo,?newName,?value);

      bl.update(bookBefore,?bookAfter);

      }?else?if?(code?==?4)?{

      System.out.println("請(qǐng)輸入要查詢的書籍名");

      String?name?=?sc.next();

      Book?book?=?bl.query(name);

      System.out.println("編號(hào):"?+?book.getNo()?+?"?書名:"?+?book.getName()?+?"?售價(jià):"?+?book.getValue());

      }?else?if?(code?==?5)?{

      ListBook?list?=?bl.getBookList();

      System.out.println("總書籍?dāng)?shù):"?+?bl.getCount());

      for?(Book?book?:?list)?{

      System.out.println("編號(hào):"?+?book.getNo()?+?"?書名:"?+?book.getName()?+?"?售價(jià):"?+?book.getValue());

      }

      }?else?if?(code?==?6)?{

      bBreak?=?false;

      }

      }

      }

      }

      jar 包 ?dom4j.jar ?jaxen-1.1.4.jar

      幫我看一下,我的java代碼。為什么運(yùn)行結(jié)果不對(duì)!如圖:請(qǐng)?jiān)斀猓瑸槭裁雌渌M件沒有顯示?

      看注釋:

      import java.awt.GridLayout;

      import javax.swing.*;

      public class LoadForm extends JFrame{

      private JPanel jpanel1;

      private JTextField jtext1;

      private JPasswordField password;

      private JLabel jlable1;

      private JLabel jlable2;

      private JButton button1;

      private JButton button2;

      public LoadForm()

      {

      super("商品管理系統(tǒng)");

      this.setLayout(null);

      GridLayout layout=new GridLayout(3,3,10,10);//這句保留

      jpanel1=new JPanel();

      //jpanel1.setLayout(null);//jpanel1的布局不能為空

      jlable1=new JLabel("用戶名");

      jlable2=new JLabel("密 碼");

      jtext1=new JTextField();

      password=new JPasswordField();

      button1=new JButton("確定");

      button2=new JButton("取消");

      jpanel1.add(jlable1);

      jpanel1.add(jtext1);

      jpanel1.add(jlable2);

      jpanel1.add(password);

      jpanel1.add(button1);

      jpanel1.add(button2);

      //this.add(jpanel1);//寫法錯(cuò)誤

      this.setContentPane(jpanel1);//設(shè)置jpanel1為Frame的內(nèi)容面版

      this.setBounds(200,200,100,100);

      //this.setSize(200,200);

      this.setVisible(true);

      }

      //main方法測(cè)試

      public static void main(String args[]){

      new LoadForm();

      }

      }

      java程序中數(shù)據(jù)的儲(chǔ)存方法有哪些?

      java程序中數(shù)據(jù)儲(chǔ)存方法如下:

      一種是棧內(nèi)存,另一種是堆內(nèi)存

      (1)在函數(shù)中定義的基本類型變量(即基本類型的局部變量)和對(duì)象的引用變量(即對(duì)象的變量名)都在函數(shù)的棧內(nèi)存中分配;

      (2)堆內(nèi)存用來存放由new創(chuàng)建的對(duì)象和數(shù)組以及對(duì)象的實(shí)例變量(即全局變量)。

      在函數(shù)(代碼塊)中定義一個(gè)變量時(shí),java就在棧中為這個(gè)變量分配內(nèi)存空間,當(dāng)超過變量的作用域后,java會(huì)自動(dòng)釋放掉為該變量所分配的內(nèi)存空間;

      在堆中分配的內(nèi)存由java虛擬機(jī)的自動(dòng)垃圾回收器來管理

      堆和棧的優(yōu)缺點(diǎn)

      堆的優(yōu)勢(shì)是可以動(dòng)態(tài)分配內(nèi)存大小,生存期也不必事先告訴編譯器,因?yàn)樗窃谶\(yùn)行時(shí)動(dòng)態(tài)分配內(nèi)存的。

      缺點(diǎn)就是要在運(yùn)行時(shí)動(dòng)態(tài)分配內(nèi)存,存取速度較慢;

      棧的優(yōu)勢(shì)是,存取速度比堆要快,僅次于直接位于CPU中的寄存器。

      菜鳥:剛學(xué)java,堆區(qū),棧區(qū),靜態(tài)區(qū),代碼區(qū),暈了!!!!!

      程序運(yùn)行時(shí),我們最好對(duì)數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)在的分配,有六個(gè)地方都可以保存數(shù)據(jù):

      1、 寄存器。這是最快的保存區(qū)域,因?yàn)樗挥诤推渌斜4娣绞讲煌牡胤剑禾幚砥鲀?nèi)部。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。我們對(duì)此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。

      2、 堆棧。駐留于常規(guī)RAM(隨機(jī)訪問存儲(chǔ)器)區(qū)域。但可通過它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,會(huì)創(chuàng)建新的內(nèi)存;若向上移,則會(huì)釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時(shí),java編譯器必須準(zhǔn)確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長(zhǎng)度”以及“存在時(shí)間”。這是由于它必須生成相應(yīng)的代碼,以便向上和向下移動(dòng)指針。這一限制無疑影響了程序的靈活性,所以盡管有些java數(shù)據(jù)要保存在堆棧里——特別是對(duì)象句柄,但java對(duì)象并不放到其中。

      3、 堆。一種常規(guī)用途的內(nèi)存池(也在RAM區(qū)域),其中保存了java對(duì)象。和堆棧不同:“內(nèi)存堆”或“堆”最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲(chǔ)空間,也不必知道存儲(chǔ)的數(shù)據(jù)要在堆里停留多長(zhǎng)的時(shí)間。因此,用堆保存數(shù)據(jù)時(shí)會(huì)得到更大的靈活性。要求創(chuàng)建一個(gè)對(duì)象時(shí),只需用new命令編制相碰的代碼即可。執(zhí)行這些代碼時(shí),會(huì)在堆里自動(dòng)進(jìn)行數(shù)據(jù)的保存。當(dāng)然,為達(dá)到這種靈活性,必然會(huì)付出一定的代價(jià):在堆里分配存儲(chǔ)空間時(shí)會(huì)花掉更長(zhǎng)的時(shí)間

      4、 靜態(tài)存儲(chǔ)。這兒的“靜態(tài)”是指“位于固定位置”。程序運(yùn)行期間,靜態(tài)存儲(chǔ)的數(shù)據(jù)將隨時(shí)等候調(diào)用。可用static關(guān)鍵字指出一個(gè)對(duì)象的特定元素是靜態(tài)的。但java對(duì)象本身永遠(yuǎn)都不會(huì)置入靜態(tài)存儲(chǔ)空間。

      5、 常數(shù)存儲(chǔ)。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的。因?yàn)樗鼈冇肋h(yuǎn)都不會(huì)改變,有的常數(shù)需要嚴(yán)格地保護(hù),所以可考慮將它們置入只讀存儲(chǔ)器(ROM)。

      6、 非RAM存儲(chǔ)。若數(shù)據(jù)完全獨(dú)立于一個(gè)程序之外,則程序不運(yùn)行時(shí)仍可存在,并在程序的控制范圍之外。其中兩個(gè)最主要的例子便是“流式對(duì)象”和“固定對(duì)象”。對(duì)于流式對(duì)象,對(duì)象會(huì)變成字節(jié)流,通常會(huì)發(fā)給另一臺(tái)機(jī)器,而對(duì)于固定對(duì)象,對(duì)象保存在磁盤中。即使程序中止運(yùn)行,它們?nèi)钥杀3肿约旱臓顟B(tài)不變。對(duì)于這些類型的數(shù)據(jù)存儲(chǔ),一個(gè)特別有用的技藝就是它們能存在于其他媒體中,一旦需要,甚至能將它們恢復(fù)成普通的、基于RAM的對(duì)象。


      文章名稱:java存儲(chǔ)器管理代碼,內(nèi)存管理java
      新聞來源:http://www.ef60e0e.cn/article/phscge.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        丹凤县| 舞阳县| 凯里市| 全州县| 林口县| 乌鲁木齐县| 华宁县| 卓资县| 晴隆县| 浮梁县| 织金县| 察雅县| 双峰县| 金阳县| 东光县| 南华县| 闵行区| 特克斯县| 盈江县| 同心县| 铁力市| 玛多县| 巴楚县| 宁津县| 宝丰县| 商洛市| 沾益县| 安庆市| 西城区| 吴江市| 龙海市| 基隆市| 图们市| 洛隆县| 江孜县| 山丹县| 麻城市| 昌平区| 定襄县| 南和县| 满洲里市|