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

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

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

    (轉)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.*"%&

    posted on 2007-03-29 09:42 扭轉乾坤 閱讀(786) 評論(1)  編輯  收藏 所屬分類: JAVA使用技巧

    評論

    # re: (轉)java實現對文件的各種操作 2011-12-24 19:38 石夜博客

    相當不錯啊 學習了 謝謝分享 發個毛的廣告啊 怎么評論不了  回復  更多評論   

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 毛片免费视频观看| 成年黄网站色大免费全看| 免费看大黄高清网站视频在线| 亚洲午夜电影在线观看高清| 91成人在线免费观看| 久久亚洲熟女cc98cm| 最近中文字幕无免费| 亚洲系列国产精品制服丝袜第| 最近免费2019中文字幕大全| 337p日本欧洲亚洲大胆色噜噜| 91在线手机精品免费观看| 亚洲成a人片毛片在线| 毛片基地免费观看| 特级毛片免费观看视频| 国产亚洲精aa成人网站| 男人的天堂网免费网站| 亚洲成AV人综合在线观看| 成年轻人网站色免费看| 国产精品亚洲а∨天堂2021| 亚洲综合精品网站| 久久午夜伦鲁片免费无码| 亚洲天堂2016| 俄罗斯极品美女毛片免费播放| 你懂的网址免费国产| 777亚洲精品乱码久久久久久| 性色av免费观看| 亚洲一区二区三区免费| 亚洲伦理一区二区| 在线播放高清国语自产拍免费| 九九免费精品视频在这里| 亚洲AV美女一区二区三区| 毛片网站免费在线观看| 老司机精品免费视频| 男人天堂2018亚洲男人天堂| 四虎AV永久在线精品免费观看| 一级毛片完整版免费播放一区| 久久久久亚洲AV无码网站| 午夜国产大片免费观看| 国产色爽免费无码视频| 国产91在线|亚洲| 国产亚洲精品影视在线产品 |