(轉(zhuǎn))java實(shí)現(xiàn)對(duì)文件的各種操作

          ?
          ?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 = "
          中文測(cè)試".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。整個(gè)文件夾拷貝
          <%@ 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ù)庫(kù)字段中的文件下載
          <%@ 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;
          //
          開(kāi)數(shù)據(jù)庫(kù)
          ResultSet result=null;
          String Sql=null;
          PreparedStatement prestmt=null;?
          DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
          DbaObj.OpenConnection();
          //
          得數(shù)據(jù)庫(kù)中的數(shù)據(jù)
          Sql="select? *? from? t_local_zhongzhuan ";
          result=DbaObj.ExecuteQuery(Sql);
          result.next();
          file://數(shù)據(jù)庫(kù)中的數(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)頁(yè)保存成文件
          <%@ 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指定的頁(yè)面以流的形式讀出,寫成指定的文件***/
          ?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.*"%&

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

          評(píng)論

          # re: (轉(zhuǎn))java實(shí)現(xiàn)對(duì)文件的各種操作 2011-12-24 19:38 石夜博客

          相當(dāng)不錯(cuò)啊 學(xué)習(xí)了 謝謝分享 發(fā)個(gè)毛的廣告啊 怎么評(píng)論不了  回復(fù)  更多評(píng)論   

          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類(31)

          隨筆檔案(30)

          文章分類(32)

          文章檔案(33)

          相冊(cè)

          PHP小站-首頁(yè)

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 庆城县| 虞城县| 陈巴尔虎旗| 嵩明县| 茶陵县| 南乐县| 越西县| 张家口市| 丹棱县| 无极县| 绥芬河市| 冷水江市| 台东县| 大同县| 布尔津县| 密山市| 鸡泽县| 和硕县| 八宿县| 耒阳市| 苏尼特左旗| 扎鲁特旗| 天气| 黄平县| 徐州市| 离岛区| 郯城县| 循化| 安阳县| 密云县| 老河口市| 四平市| 修武县| 英吉沙县| 福清市| 菏泽市| 年辖:市辖区| 慈溪市| 成武县| 增城市| 枣庄市|