文件类数据导入与导出数据库

create table(…,content mediumtext,image mediumblob……);

另:blob,text最大64KB,blob对大小写敏感,text不敏感;

mediumblob,mediumtext最大16MB;

longblob,longtext最大4GB;

txt读入:以字符流的形式读入

FileInputStream f=new FileInputStream("d:/a.txt");
BufferedReader dr=new BufferedReader(new InputStreamReader(f));
pstmt.setCharacterStream(3, dr,f.available());

或者是以字符串的形式读入

FileReader d=new FileReader("d:/a.txt");
char[] a = new char[1024];
int c;
  StringBuffer buffer = new StringBuffer();
    while((c = d.read(a)) != -1)
    {        
        
          buffer.append(a, 0, c);
     }    
   pstmt.setString(3, buffer.toString());

 

txt读出:可以直接以字符串的形式读出来,pstmt.getString()…

也可以以流的形式读出来:

Reader rd=rs.getCharacterStream(1);
        BufferedReader br=new BufferedReader(rd);
        String str="";
        String s=null;
        while((s=br.readLine())!=null)   
                str=str+s+"\n";       
        System.out.println(str);

 

图片的读入:

FileInputStream isimg=new FileInputStream("d:/a.bmp");

pstmt.setBinaryStream(4, isimg,isimg.available());

图片的读出:

Blob blob=rs.getBlob(2);
BufferedInputStream bis=new BufferedInputStream(blob.getBinaryStream());
BufferedOutputStream bos=new BufferedOutputStream(
                new FileOutputStream("d:/ab.bmp"));
        byte[] buffer =new byte[1024];
        int count=-1;
        while((count=bis.read(buffer,0,1024))!=-1)
        {   
            bos.write(buffer,0,count);
        }
        bos.flush();
        bos.close();

posted @ 2011-03-31 22:53  无敌小钰  阅读(258)  评论(0编辑  收藏  举报