<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
    數(shù)據(jù)加載中……

    Java中對文件的操作

     java中提供了io類庫,可以輕松的用java實現(xiàn)對文件的各種操作。下面就來說一下如何用java來實現(xiàn)這些操作。

       1。新建目錄

    <%@ page contentType="text/html;charset=gb2312"%>
    <%
    String filePath="c:/aaa/";
    filePath=filePath.toString();//中文轉(zhuǎn)換
    java.io.File myFilePath=new java.io.File(filePath);
    if(!myFilePath.exists())
    myFilePath.mkdir();
    %>

      2。新建文件

    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.io.*" %>
    <%
    String filePath="c:/哈哈.txt";
    filePath=filePath.toString();
    File myFilePath=new File(filePath);
    if(!myFilePath.exists())
    myFilePath.createNewFile();
    FileWriter resultFile=new FileWriter(myFilePath);
    PrintWriter myFile=new PrintWriter(resultFile);
    String strContent = "中文測試".toString();
    myFile.println(strContent);
    resultFile.close();
    %>

     3。刪除文件

    <%@ page contentType="text/html;charset=gb2312"%>
    <%
    String filePath="c:/支出證明單.xls";
    filePath=filePath.toString();
    java.io.File myDelFile=new java.io.File(filePath);
    myDelFile.delete();
    %>

     4。文件拷貝

    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
    int bytesum=0;
    int byteread=0; 
    file://讀到流中
    InputStream inStream=new FileInputStream("c:/aaa.doc");
    FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");
    byte[]  buffer =new  byte[1444];
    int length;
    while ((byteread=inStream.read(buffer))!=-1)
     {
       out.println("<DT><B>"+byteread+"</B></DT>");
       bytesum+=byteread;
       System.out.println(bytesum);
       fs.write(buffer,0,byteread);
     } 
    inStream.close();
    %>

     5。整個文件夾拷貝

    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.io.*" %>
    <%String url1="C:/aaa";
      String url2="d:/java/";
      (new File(url2)).mkdirs();
     File[] file=(new File(url1)).listFiles();
     for(int i=0;i<file.length;i++){
      if(file[i].isFile()){
       file[i].toString();
       FileInputStream input=new FileInputStream(file[i]);
       FileOutputStream output=new FileOutputStream(url2+"/"+(file[i].getName()).toString());
       byte[] b=new byte[1024*5];
        int len;
        while((len=input.read(b))!=-1){
        output.write(b,0,len);
        }
        output.flush();
        output.close();
        input.close();
      }
     }
    %>

     6。文件下載

    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
      String fileName = "zsc104.swf".toString();
    //讀到流中
    InputStream inStream=new FileInputStream("c:/zsc104.swf");
    //設(shè)置輸出的格式
      response.reset();
      response.setContentType("bin");
      response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
    //循環(huán)取出流中的數(shù)據(jù)
      byte[] b = new byte[100];
      int len;
      while((len=inStream.read(b)) >0)
      response.getOutputStream().write(b,0,len);  
      inStream.close();
    %>

     7。數(shù)據(jù)庫字段中的文件下載

    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.sql.*"%>
    <%@ page import="java.lang.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="com.jspsmart.upload.*" %>
    <%@ page import="DBstep.iDBManager2000.*"%>
    <%
    int bytesum=0;
    int byteread=0;
    //打開數(shù)據(jù)庫
    ResultSet result=null;
    String Sql=null;
    PreparedStatement prestmt=null; 
    DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
    DbaObj.OpenConnection();
    //取得數(shù)據(jù)庫中的數(shù)據(jù)
    Sql="select  *  from  t_local_zhongzhuan ";
    result=DbaObj.ExecuteQuery(Sql);
    result.next();

    file://將數(shù)據(jù)庫中的數(shù)據(jù)讀到流中
    InputStream inStream=result.getBinaryStream("content");
    FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");

    byte[]  buffer =new  byte[1444];
    int length;
    while ((byteread=inStream.read(buffer))!=-1)
      {
         out.println("<DT><B>"+byteread+"</B></DT>");
         bytesum+=byteread;
         System.out.println(bytesum);
         fs.write(buffer,0,byteread);
         }
    %>

     8。把網(wǎng)頁保存成文件

    <%@ page import="java.text.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.net.*"%>
    <%
     URL stdURL = null;
     BufferedReader stdIn = null;
     PrintWriter stdOut = null;
     try {
      stdURL = new URL("http://www.163.com");
     }
     catch (MalformedURLException e) {
       throw e;
     }

    try {
       stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
       stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html")));
     }
     catch (IOException e) {
     }

     /***把URL指定的頁面以流的形式讀出,寫成指定的文件***/
     try {
       String strHtml = "";
       while((strHtml = stdIn.readLine())!=null) {
       stdOut.println(strHtml);
       }
     }
     catch (IOException e) {
       throw e;
     }
     finally {
       try {
         if(stdIn != null)
           stdIn.close();
         if(stdOut != null)
           stdOut.close();
       }
       catch (Exception e) {
         System.out.println(e);
       }
     }
    %>

     9。直接下載網(wǎng)上的文件

    <%@ page import="java.io.*"%>
    <%@ page import="java.net.*"%>
    <%
    int bytesum=0;
    int byteread=0;

    URL url = new URL("http://pimg.163.com/sms/micheal/logo.gif");
     URLConnection conn = url.openConnection();
     InputStream inStream = conn.getInputStream();
     FileOutputStream fs=new FileOutputStream( "c:/abc.gif");

      byte[]  buffer =new  byte[1444];
       int length;
        while ((byteread=inStream.read(buffer))!=-1)
        {
           out.println("<DT><B>"+byteread+"</B></DT>");
           bytesum+=byteread;
           System.out.println(bytesum);
           fs.write(buffer,0,byteread);
         }
    %>

    10。按行讀文件
    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
    FileReader  myFileReader=new FileReader("c:/哈哈.txt");
    BufferedReader myBufferedReader=new BufferedReader(myFileReader);
    String myString=null;
    String resultString=new String();
    while((myString=myBufferedReader.readLine())!=null)
      {  resultString=resultString+myString+"<br>";
       }
    out.println(resultString);
    myFileReader.close();
    %>

    11。 數(shù)據(jù)庫里字段文件直接下載到客戶端
    <%@ page import="java.sql.*"%>
    <%@ page import="java.lang.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="com.jspsmart.upload.*" %>
    <%@ page import="DBstep.iDBManager2000.*"%>
    <%
      String fileName = "bb.doc".toString();
      //打開數(shù)據(jù)庫
      ResultSet result=null;
      String Sql=null;
      PreparedStatement prestmt=null;
      DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
      DbaObj.OpenConnection();
    //取得數(shù)據(jù)庫中的數(shù)據(jù)
    Sql="select  *  from  marklist order by markdate desc";
    result=DbaObj.ExecuteQuery(Sql);
    result.next();
    //將數(shù)據(jù)庫中的數(shù)據(jù)讀到流中
      InputStream in =result.getBinaryStream("markbody");
    //設(shè)置輸出的格式
      response.reset();
      response.setContentType("application/Msword");
      response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
    //循環(huán)取出流中的數(shù)據(jù)
      byte[] b = new byte[1024];
      int len;
      while((len=in.read(b)) >0)
      response.getOutputStream().write(b,0,len);
      in.close();
    %>

    12。文件夾遍歷
    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
    String url1="C:/aaa";
    File  f=(new File(url1));
    if(f.isDirectory()){
           File [] fe = f.listFiles();
           go_on:
          for (int i = 0;i<fe.length;i++){
          if (fe[i].isDirectory()){                           
        File [] fe1 = fe[i].listFiles();
        for (int j = 0;j<fe1.length;j++){
        if (fe1[j].isDirectory())
        continue go_on;
        out.println(fe1[j].toString());
                }
            }                            
         else out.println(fe[i].toString());            
        }
      }
                              
    %>
    13。通過字符編碼移動文件
    <%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
    String ret=new String();
    try
    {
      byte[] bytes=new byte[102400];
      InputStream in=new FileInputStream("c:/aaa.doc");
      in.read(bytes);
      ret=new sun.misc.BASE64Encoder().encode(bytes); //具體的編碼方法
      in.close();
    }
    catch(FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch(java.io.IOException ex)
    {
      ex.printStackTrace();
    }
    out.println(ret);

    byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(ret);    
    java.io.ByteArrayInputStream  inStream=new java.io.ByteArrayInputStream(bytes);
    byte[]  buffer =new  byte[1444];
    FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");
    int bytesum=0;
    int byteread=0;        
    while ((byteread=inStream.read(buffer))!=-1)
      {    
         bytesum+=byteread;
         fs.write(buffer,0,byteread);
         }    
    %>

    14。把文件編碼成base64字符串
    <%
         String ret=new String();
          byte[] bytes=new byte[1024];
         String aa="aaaa";
         bytes=aa.getBytes();
         ret=new sun.misc.BASE64Encoder().encode(bytes); //具體的編碼方法
      bytes = new sun.misc.BASE64Decoder().decodeBuffer(ret);    
      aa=new String(bytes);
      out.println(aa);
    %>

    posted on 2005-03-28 09:55 閱讀(280) 評論(0)  編輯  收藏 所屬分類: J2se

    主站蜘蛛池模板: 波多野结衣亚洲一级| 中文字幕亚洲电影| 亚洲精品乱码久久久久久下载| 免费一区二区三区在线视频| 国产无遮挡色视频免费视频| 亚洲狠狠婷婷综合久久蜜芽| 在线jyzzjyzz免费视频| 亚洲中文字幕一区精品自拍| 夜夜嘿视频免费看| 国产成人不卡亚洲精品91| 免费观看日本污污ww网站一区| 国产亚洲男人的天堂在线观看 | 免费黄网站在线看| 久久亚洲精品AB无码播放| 久久精品人成免费| 中文字幕 亚洲 有码 在线| 日韩精品无码人妻免费视频| 国产亚洲Av综合人人澡精品| 久久综合亚洲色HEZYO国产| 免费黄网站在线观看| 亚洲av无码电影网| 免费国内精品久久久久影院| 久久毛片免费看一区二区三区| 久久国产精品亚洲综合| 在线观看av永久免费| 爱情岛亚洲论坛在线观看 | 精品国产亚洲男女在线线电影| 最近免费中文字幕中文高清 | 亚洲AV午夜成人片| 久草视频免费在线观看| 亚洲av无码专区在线观看下载 | 91视频免费观看高清观看完整| 麻豆亚洲AV永久无码精品久久| 最近免费中文字幕4| 国产JIZZ中国JIZZ免费看| 亚洲一卡2卡三卡4卡有限公司| 天天摸夜夜摸成人免费视频 | 亚洲18在线天美| 亚洲小说区图片区另类春色| 在线a级毛片免费视频| 国产福利免费视频 |