數據加載中……
          Java 文件操作
          2008年5月1日   Edited By DingDangXiaoMa
          Java 對文件的操作有很多種方法,類也很多,這里舉出最簡單的測試例子,讀取文件輸入輸出數據庫操作。
          以下是部分源代碼:
          package com.zh.wsdy.dao;

          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;

          import org.hibernate.Query;
          import org.hibernate.Session;
          import org.hibernate.Transaction;

          import com.zh.wsdy.model.AjPrgWsmb;
          import com.zh.wsdy.model.BaseHibernateDAO;
          import com.zh.wsdy.model.Wtest;

          //測試讀寫文件及讀文件到數據庫中。。。
          public class TestDAO extends BaseHibernateDAO {
              
          private static final int BUFFER_SIZE = 16 * 1024;

              
          public TestDAO() {

              }

              
          /**
               * 測試程序,把讀取一個文件,把個文件的內容,以字節的形式保存到數據中去。
               
          */
              
          public void saveFileStreamToDataBase(String id) {
                  
          // 先進進行數據庫方面的操作。連接及新建類。
                  Session session = getSession();
                  Wtest test 
          = new Wtest(id);

                  
          // 以下是文件的定義。
                  File file = new File("c:" + File.separator + "response.txt");
                  
          // File file = new File("c:" + File.separator + "a.java");
                  
          // File file = new File("c:" + File.separator + "word.doc");

                  
          try {
                      FileInputStream inputStream 
          = new FileInputStream(file);
                      BufferedInputStream bufferedInputStream 
          = new BufferedInputStream(
                              inputStream);
                      
          byte[] buffer = new byte[BUFFER_SIZE];
                      
          while (bufferedInputStream.read(buffer) > 0) {
                          test.setWcontent(buffer);
                      }
                      Transaction trac 
          = session.beginTransaction();
                      trac.begin();
                      session.save(test);
                      System.out.println(
          "插入一條數據成功.");
                      trac.commit();
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }

              
          /**
               * 傳遞主鍵參數,從數據庫中讀取數據。
               * 
               * 
          @param id
               
          */
              
          public void ReadDataBaseStreamToString(String id) {
                  Session session 
          = getSession();
                  Query query 
          = session.createQuery("from AjPrgZdms m where m.FId =:id");
                  
          // Query query = session.createQuery("from Wtest t where t.wid =:id ");
                  query.setString("id", id);
                  AjPrgWsmb test 
          = (AjPrgWsmb) query.list().get(0);
                  
          // Wtest test = (Wtest)query.list().get(0);
                  
          // byte [] content = test.getWcontent();
                  byte[] content = test.getFWsys();
                  System.out.print(content); 
          // 在這里直接輸出字節,沒有進行字符串的轉換。
                  
          // String contentString = new String(content); //進行轉換,把字節轉換成String
                  
          // System.out.print("the String is : "+contentString);
              }

              
          /**
               * 從數據庫讀取byte型數據,寫入當地文件,看一下,寫入與原始文件的差別。
               * 
               * 
          @param id
               
          */
              
          public void writeFileFromDataBase(String id) {
                  Session session 
          = getSession();
                  Query query 
          = session.createQuery("from Wtest t  where t.wid =:id");
                  query.setString(
          "id", id);
                  Wtest test 
          = (Wtest) query.list().get(0);
                  
          byte[] content = test.getWcontent();
                  
          try {
                      BufferedOutputStream out 
          = new BufferedOutputStream(
                              
          new FileOutputStream("c:\\word2.doc"));
                      out.write(content);
                      System.out.println(
          "從數據庫中讀取數據,并寫入相應的文件中。。。");
                  } 
          catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          // System.out.print(content); //在這里直接輸出字節,沒有進行字符串的轉換。
              }

              
          /**
               * 這個測試的方法是,從AjPrgWsmb表中,讀取數據,寫入到相應的文件中去。
               * 
               * 
          @param id
               
          */
              
          public void writeFile(String id) {
                  Session session 
          = getSession();
                  Query query 
          = session.createQuery("from AjPrgWsmb m where m.FId =:id");
                  
          // Query query = session.createQuery("from Wtest t where t.wid =:id ");
                  query.setString("id", id);
                  AjPrgWsmb test 
          = (AjPrgWsmb) query.list().get(0);
                  
          // Wtest test = (Wtest)query.list().get(0);
                  
          // byte [] content = test.getWcontent();
                  
          //byte[] content = test.getFWsys();
                  byte[] content = test.getFContent();
                  
          if(content!=null){
                  
          try {
                      BufferedOutputStream out 
          = new BufferedOutputStream(
                              
          new FileOutputStream("c:\\word2.doc"));
                      out.write(content);
                      System.out.println(
          "從數據庫中讀取數據,并寫入相應的文件中。。。");
                  } 
          catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  }
          else{
                      System.out.println(
          "這個文書模板樣式為空值。。。。。");
                  }
              }

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  TestDAO test 
          = new TestDAO();
                  
          // test.saveFileStreamToDataBase("txt"); // 保存一條數據到數據庫中,主鍵為txt,word,java
                  
          // test.ReadDataBaseStreamToString("word");
                  
          // test.writeFileFromDataBase("0000000010");
                  test.writeFile("0000000023");

              }

          }
          以上的例子很簡單,是一些普通的文件的操盤。

          posted on 2008-05-01 17:04 叮當小馬 閱讀(239) 評論(0)  編輯  收藏 所屬分類: JSP/JAVA

          主站蜘蛛池模板: 青神县| 凤阳县| 称多县| 青海省| 乐平市| 昌吉市| 罗定市| 宁晋县| 金塔县| 武义县| 岑巩县| 彰武县| 呼伦贝尔市| 彝良县| 馆陶县| 肃北| 闵行区| 靖安县| 瓦房店市| 铁岭市| 西宁市| 叙永县| 巴塘县| 雷波县| 三明市| 宣武区| 称多县| 海南省| 广德县| 庆安县| 荃湾区| 乐山市| 舞阳县| 葵青区| 新巴尔虎右旗| 望奎县| 岳阳县| 扎囊县| 花莲县| 张家港市| 麟游县|