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

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

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

    內蒙古java團隊

    j2se,j2ee開發組
    posts - 139, comments - 212, trackbacks - 0, articles - 65
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java對各種文件操作.

    Posted on 2006-11-26 13:21 帥子 閱讀(264) 評論(0)  編輯  收藏 所屬分類: J2EE技術專區

    java中提供了io類庫,可以輕松的用java實現對文件的各種操作。下面就來說一下如何用java來實現這些操作。
    ?
    新建目錄<%@?page?contentType="text/html;charset=gb2312"%>
    <%
    //String?URL?=?request.getRequestURI();
    String?filePath="C:\\測試\\";
    filePath=filePath.toString();//中文轉換
    java.io.File?myFilePath=new?java.io.File(filePath);
    if(!myFilePath.exists())
    myFilePath.mkdir();
    %>
    ?
    新建文件
    <%@?page?contentType="text/html;charset=gb2312"%>
    <%@?page?import="java.io.*"?%>
    <%
    String?filePath="c:/測試/newFile.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?content?="這是測試數據";
    String?strContent?=?content.toString();
    myFile.println(strContent);
    resultFile.close();
    %>
    ?
    刪除文件<%@?page?contentType="text/html;charset=gb2312"%>
    <%
    String?filePath="c://測試//newFile.txt";
    filePath=filePath.toString();
    java.io.File?myDelFile=new?java.io.File(filePath);
    if(myDelFile.exists())
    {??
    ????myDelFile.delete();
    ????out.println(filePath+"刪除成功!??!");
    }
    else
    {
    ????out.println(filePath+"該文件不存在");
    }
    %>
    文件拷貝<%@?page?contentType="text/html;?charset=gb2312"?%>
    <%@?page?import="java.io.*"?%>
    <%
    int?bytesum=0;
    int?byteread=0;
    //file:讀到流中
    InputStream?inStream=new?FileInputStream("c://測試//newFile.txt");
    FileOutputStream?fs=new?FileOutputStream(?"c://測試//copyFile.txt");
    byte[]??buffer?=new??byte[1444];
    int?length;
    while?((byteread=inStream.read(buffer))!=-1)
    ?{
    ???out.println("<DT><B>"+byteread+"</B></DT>");
    ???bytesum+=byteread;
    ???out.println(bytesum);
    ???fs.write(buffer,0,byteread);
    ?}
    inStream.close();
    %>
    ?
    整個文件夾拷貝
    <%@?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();
    ??}
    ?}
    %>
    ?
    文件下載
    <%@?page?contentType="text/html;?charset=gb2312"%>
    <%@?page?import="java.io.*"?%>
    <%
    ??String?fileName?=?"newFile.txt".toString();
    ??//讀到流中
    ??InputStream?inStream=new?FileInputStream("c://測試//newFile.txt");
    ??//設置輸出的格式
    ??response.reset();
    ??response.setContentType("text/plain");
    ??response.addHeader("Content-Disposition","attachment;?filename=\""?+?fileName?+?"\"");
    ??//循環取出流中的數據
    ??byte[]?b?=?new?byte[100];
    ??int?len;
    ??ServletOutputStream?outStream?=?response.getOutputStream();
    ?
    ??while((len=inStream.read(b))?>0)
    ??outStream.write(b,0,len);
    ??outStream.flush();
    ??outStream.close();
    ??inStream.close();
    %>
    ?
    數據庫字段中的文件下載
    <%@?page?contentType="text/html;charset=gb2312"%>
    <%@?page?import="java.util.*,java.sql.*,java.io.*"%>
    <%
    ????String?id?=?request.getParameter("id");
    ????if(id==null)
    ????{???throw?new?Exception?("沒有找到圖片");
    ????}
    ????else
    ????{
    ???????try
    ???????{
    com.gzrealmap.lib.jdbc.JDBCUtil??SqlBean=?com.gzrealmap.lib.jdbc.JDBCUtil.getInstance();
    ???????????????SqlBean.connect();
    ???????????????String?sql?=?"select?*?from?innernews?where?id?=?'"+79+"'";
    ???????????????ResultSet?rs?=?SqlBean.queryforUpdate(sql);
    ???????????????rs.next();
    ???????????????//String?fileNamedb?=?rs.getString("imageName");
    ???????????????String?file=?rs.getString("acc");
    ???????????????//String?fileName?=?new?String(fileNamedb.getBytes(),"iso-8859-1");
    ???????????????String?fileName?=?"a.jpg";
    ????????????????response.setHeader("Content-Disposition",??"inline;?filename=\""?+?fileName?+?"\"");????
    ???????????????String?filter?=?fileName.substring(fileName.lastIndexOf("."));
    ??????????????
    ???????????????if(filter.equals(".txt"))
    ???????????????{
    ???????????????????response.setContentType("text/plain");
    ???????????????}
    ???????????????else?if(filter.equals(".doc")||filter.equals(".dot"))
    ???????????????{
    ???????????????????response.setContentType("application/msword");
    ???????????????}
    ???????????????else
    ???????????????{
    ?????????????????response.setContentType("image/jpeg;charset=GB2312");
    ???????????????}
    ???????????????ServletOutputStream?o?=?response.getOutputStream();
    ???????????????//o.write(file);
    ???????????????out.println(file);
    ???????????????//o.flush();
    ???????????????//o.close();
    ???????????????SqlBean.disconnect();
    ???????}
    ????????catch(Exception?ex)
    ???????{
    ???????????out.println(ex.getMessage());
    ???????}
    ????}??
    %>
    ?
    把網頁保存成文件<%@?page?contentType="text/html;charset=gb2312"%>
    <%@?page?import="java.text.*,java.util.*,java.net.*,java.io.*"%>
    <%
    ?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()));
    ????String?theFileName?=?"c://測試//163.html";
    ????stdOut?=?new?PrintWriter(new?BufferedWriter(new?FileWriter(theFileName.toString())));
    ?}
    ?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);
    ???}
    ?}
    %>
    ?
    直接下載網上的文件
    <%@?page?contentType="text/html;charset=gb2312"%>
    <%@?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();
    ?
    ??/**
    ??String?theFileName?=?"c:/測試/logo.gif";
    ??theFileName?=?theFileName.toString();
    ??File?myFilePath=new?File(theFileName);
    ??if(!myFilePath.exists())
    ??myFilePath.createNewFile();
    ??**/
    ?
    ??FileOutputStream?fs=new?FileOutputStream("c:/測試/logo2.gif");
    ??byte[]??buffer?=new??byte[1444];
    ????while?((byteread=inStream.read(buffer))!=-1)
    ????{
    ???????out.println("<DT><B>"+byteread+"</B></DT>");
    ???????bytesum+=byteread;
    ???????//System.out.println(bytesum);
    ???????fs.write(buffer,0,byteread);
    ?????}
    %>
    ?
    按行讀文件?<%@?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();
    %>
    ?
    對word文檔的處理(上傳與下載)<%@?page?contentType="application/msword"?%>
    <!--?以上這行設定本網頁為excel格式的網頁?-->
    <%
    ???response.setHeader("Content-disposition","inline;?filename=test1.doc");?//線上瀏覽方式
    ??//?response.setHeader("Content-disposition","attachment;?filename=test1.doc");//下載方式
    ???//以上這行設定傳送到前端瀏覽器時的檔名為test1.doc
    ???//就是靠這一行,讓前端瀏覽器以為接收到一個word檔
    %>
    //然后輸出動態內容就可以得到一個word文檔了
    ?
    1,打開:
    1)文件頭上加:<%@?page??contentType="application/msword"%>?
    xml文件里:
    <mime-mapping>
    ????????<extension>doc</extension>
    ????????<mime-type>application/msword</mime-type>
    </mime-mapping>
    2)可以用js,以下代碼來自引用:
    <%@?page?contentType="text/html;charset=gb2312"?import=?"java.io.*"%>
    <HTML>
    <script>
    var?wrd=new?ActiveXObject("Word.Application")
    wrd.visible=true
    alert?("您的"+wrd.Application.Caption+"安裝路徑為:\n"+wrd.Application.Path+"\n版本號是:"+?wrd.Application.version+"\n注冊使用者是:"+wrd.Application.UserName)
    wrd.Documents.Add()
    //wrd.Documents.Open("c:\\exam.doc")
    wrd.Selection.TypeText("This?is?some?text.")
    wrd.Application.Activate()
    wrd.ActiveDocument.SaveAs("c:\\exam111.doc")
    wrd=null
    </script>
    </HTML>
    ?
    2,下載:
    <%@?page?contentType="text/html;charset=gb2312"?import=?"java.io.*"%>
    <%//?得到文件名字和路徑
    ??String?filename?=?"jsp.doc";
    ??String?filepath?=?"C:\\";
    ?
    ??//?設置響應頭和下載保存的文件名
    ??response.setContentType("APPLICATION/OCTET-STREAM");
    ??response.setHeader("Content-Disposition","attachment;?filename=\""?+?filename?+?"\"");
    ?
    ??//?打開指定文件的流信息
    ??java.io.FileInputStream?fileInputStream?=?new?java.io.FileInputStream(filepath?+?filename);
    ??//FileOutputStream?out??=?new?FileOutputStream(filepath+"測試\\"?+?filename);
    ??//?寫出流信息
    ??int?i;
    ??while?((i=fileInputStream.read())?!=?-1)?{
    ???out.write(i);
    ??}
    ??fileInputStream.close();
    ??out.close();
    ?%>?
    主站蜘蛛池模板: 2020天堂在线亚洲精品专区| 亚洲资源在线视频| 老湿机一区午夜精品免费福利| 成年女人喷潮毛片免费播放| 亚洲熟伦熟女专区hd高清| 成人免费淫片在线费观看| 亚洲人成色77777在线观看| 日韩午夜免费视频| 免费激情网站国产高清第一页| 亚洲а∨天堂久久精品| 成年网在线观看免费观看网址| 国产成人精品曰本亚洲79ren| 国产猛男猛女超爽免费视频| 亚洲AV成人一区二区三区AV| 久久www免费人成看片| 亚洲一线产区二线产区区| 精品少妇人妻AV免费久久洗澡| 国产成人精品日本亚洲语音| 亚洲一区二区三区在线播放| 国产性生大片免费观看性| 91亚洲导航深夜福利| 成人影片麻豆国产影片免费观看 | 国产日韩一区二区三免费高清| 国产亚洲综合一区柠檬导航| 最近中文字幕mv免费高清在线 | 国产成人亚洲综合在线| 久久亚洲AV无码西西人体| 免费在线看黄网站| 亚洲三级高清免费| 亚洲福利在线播放| 91成人在线免费观看| 亚洲精品蜜夜内射| 亚洲尤码不卡AV麻豆| 免费观看无遮挡www的小视频| 亚洲国产精品ⅴa在线观看| 亚洲无线码在线一区观看| 在线看片韩国免费人成视频| 曰批免费视频播放免费| 日韩亚洲AV无码一区二区不卡| 香蕉视频在线观看免费国产婷婷| 一二三区免费视频|