java learnging

          一塊探討JAVA的奧妙吧
          posts - 34, comments - 27, trackbacks - 0, articles - 22

          Java中對文件的操作

          Posted on 2005-02-20 21:23 bigseal 閱讀(355) 評論(0)  編輯  收藏
           

          Java中對文件的操作

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

             1。新建目錄

          <%@ page contentType="text/html;charset=gb2312"%>
          <%
          String filePath="c:/aaa/";
          filePath=filePath.toString();//中文轉換
          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");
          //設置輸出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
          //循環取出流中的數據
            byte[] b = new byte[100];
            int len;
            while((len=inStream.read(b)) >0)
            response.getOutputStream().write(b,0,len);  
            inStream.close();
          %>

           7。數據庫字段中的文件下載

          <%@ 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;
          //打開數據庫
          ResultSet result=null;
          String Sql=null;
          PreparedStatement prestmt=null; 
          DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
          DbaObj.OpenConnection();
          //取得數據庫中的數據
          Sql="select  *  from  t_local_zhongzhuan ";
          result=DbaObj.ExecuteQuery(Sql);
          result.next();

          file://將數據庫中的數據讀到流中
          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。把網頁保存成文件

          <%@ 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。直接下載網上的文件

          <%@ 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);
               }
          %>


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 侯马市| 东海县| 赤峰市| 通州市| 东乌珠穆沁旗| 磐安县| 石首市| 陈巴尔虎旗| 旬邑县| 章丘市| 彝良县| 安西县| 高州市| 黎平县| 三原县| 理塘县| 清流县| 博乐市| 北海市| 二连浩特市| 于田县| 樟树市| 靖宇县| 洛川县| 磐石市| 米易县| 和林格尔县| 庆城县| 牙克石市| 汕头市| 左权县| 北安市| 水富县| 桑日县| 乌鲁木齐县| 青州市| 驻马店市| 温宿县| 双柏县| 聊城市| 昆山市|