Java对各种文件的操作详解
2007-06-06 04:11:39 作者: 来源:空 浏览次数:44 文字大小:【大】【中】【小】 java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何 用java来实现这些操作。
新建目录
<%@ age 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(); %>
新建文件
<%@ age contentType=\"text/html;charset=gb2312\"%> <%@ age 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 rintWriter(resultFile); String content =\"这是测试数据\"; String trContent = content.toString(); myFile.println(strContent); resultFile.close(); %>
删除文件
<%@ age 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+\"该文件不存在\"); } %>
文件拷贝<%@ age contentType=\"text/html charset=gb2312\" %> <%@ age import=\"java.io.*\" %> <%
int ytesum=0; int yteread=0; //file:读到流中
I utStream i tream=new FileI utStream(\"c://测试//newFile.txt\"); FileOutputStream fs=new FileOutputStream( \"c://测试//copyFile.txt\"); byte[] uffer =new yte[1444]; int length;
while ((byteread=i tream.read(buffer))!=-1) {
out.println(\"
i tream.close(); %>
整个文件夹拷贝
<%@ age contentType=\"text/html;charset=gb2312\"%> <%@ age import=\"java.io.*\" %> <%String url1=\"C:/aaa\"; tring url2=\"d:/java/\"; (new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles(); for(int i=0;i FileOutputStream output=new FileOutputStream(url2+\"/\"+(file[i].getName()).toString()); yte[] =new yte[1024*5]; int le while((len=i ut.read(b))!=-1){ output.write(b,0,len); } output.flush(); output.close(); i ut.close(); } } %> JAVA天堂 文件下载 <%@ age contentType=\"text/html charset=gb2312\"%> <%@ age import=\"java.io.*\" %> <% tring fileName = \"newFile.txt\".toString(); //读到流中 I utStream i tream=new FileI utStream(\"c://测试//newFile.txt\"); //设置输出的格式 re o e.reset(); re o e.setContentType(\"text/plain\"); re o e.addHeader(\"Content-Di osition\ //循环取出流中的数据 yte[] = ew yte[100]; int le ervletOutputStream outStream = re o e.getOutputStream(); while((len=i tream.read(b)) gt;0) outStream.write(b,0,len); outStream.flush(); outStream.close(); i tream.close(); %> 数据库字段中的文件下载 <%@ age contentType=\"text/html;charset=gb2312\"%> <%@ age import=\"java.util.*,java.sql.*,java.io.*\"%> <% tring id = request.getParameter(\"id\"); if(id==null) { throw ew Exceptio (\"没有找到图片\"); } else { try { com.gzrealmap.lib.jdbc.JDBCUtil qlBean= com.gzrealmap.lib.jdbc.JDBCUtil.getI tance(); qlBean.co ect(); tring ql = \"select * from i ernew where id = \"\"+79+\"\"\"; ResultSet r = qlBean.queryforUpdate(sql); rs.next(); //String fileNamed = rs.getString(\"imageName\"); tring file= rs.getString(\"acc\"); //String fileName = ew tring(fileNamedb.getBytes(),\"iso-8859-1\"); tring fileName = \"a.jpg\"; re o e.setHeader(\"Content-Di osition\ JAVA天堂 tring filter = fileName.su tring(fileName.lastIndexOf(\".\")); if(filter.equals(\".txt\")) { re o e.setContentType(\"text/plain\"); } else if(filter.equals(\".doc\")||filter.equals(\".dot\")) { re o e.setContentType(\"a lication/msword\"); JAVA天堂 } else { re o e.setContentType(\"image/jpeg;charset=GB2312\"); } ervletOutputStream o = re o e.getOutputStream(); //o.write(file); out.println(file); //o.flush(); //o.close(); qlBean.disco ect(); } catch(Exceptio ex) { out.println(ex.getMe age()); } } %> 把网页保存成文件 <%@ age contentType=\"text/html;charset=gb2312\"%> <%@ age import=\"java.text.*,java.util.*,java.net.*,java.io.*\"%> <% URL tdURL = ull; ufferedReader tdI = ull; rintWriter tdOut = ull; try { tdURL = ew URL(\"http://www.163.com\"); } catch (MalformedURLExceptio e) { throw e; } try { //将字节流转变成为字符流 tdI = ew ufferedReader(new I utStreamReader(stdURL.ope tream())); tring theFileName = \"c://测试//163.html\"; tdOut = ew rintWriter(new ufferedWriter(new FileWriter(theFileName.toString()))); } catch (IOExceptio e) { } /***把URL指定的页面以流的形式读出,写成指定的文件***/ try { tring trHtml = \"\"; while((strHtml = tdIn.readLine())!=null) { tdOut.println(strHtml); } } catch (IOExceptio e) { throw e; } finally { try { if(stdI != ull) tdIn.close(); if(stdOut != ull) tdOut.close(); } catch (Exceptio e) { ystem.out.println(e); } } %> 直接下载网上的文件 <%@ age contentType=\"text/html;charset=gb2312\"%> <%@ age import=\"java.io.*\"%> <%@ age import=\"java.net.*\"%> <% int ytesum=0; int yteread=0; URL url = ew URL(\"http://java.chinaitlab.com/UploadFiles_8734/200611/20061116101405957.gif\"); yte[] uffer =new yte[1444]; while ((byteread=i tream.read(buffer))!=-1) { out.println(\" //System.out.println(bytesum); fs.write(buffer,0,byteread); } %> 按行读文件 <%@ age contentType=\"text/html charset=gb2312\" %> <%@ age import=\"java.io.*\" %> <% FileReader myFileReader=new FileReader(\"c:/哈哈.txt\"); BufferedReader myBufferedReader=new ufferedReader(myFileReader); String myString=null; String resultString=new tring(); while((myString=myBufferedReader.readLine())!=null) { resultString=resultString+myString+\"< r>\"; } out.println(resultString); myFileReader.close(); %> 对word文档的处理(上传与下载) <%@ age contentType=\"a lication/msword\" %> <% re o e.setHeader(\"Content-di osition\线上浏览方式 // re o e.setHeader(\"Content-di osition\下载方式 //以上这行设定传送到前端浏览器时的档名为test1.doc //就是靠这一行,让前端浏览器以为接收到一个word档 %> //然后输出动态内容就可以得到一个word文档了 1,打开: 1)文件头上加:<%@ age contentType=\"a lication/msword\"%> xml文件里: lt;exte io gt;doclt;mime-type>a lication/msword 2)可以用js,以下代码来自引用: <%@ age contentType=\"text/html;charset=gb2312\" import= \"java.io.*\"%> < cript> var wrd=new ActiveXObject(\"Word.A lication\") wrd.visible=true alert (\"您的\"+wrd.A lication.Caption+\"安装路径为: \"+wrd.A lication.Path+\" 版本号是:\"+ wrd.A lication.version+\" 注册使用者是:\"+wrd.A lication.UserName) wrd.Documents.Add() //wrd.Documents.Open(\"c:\\exam.doc\") wrd.Selection.TypeText(\"Thi i ome text.\") wrd.A lication.Activate() wrd.ActiveDocument.SaveAs(\"c:\\exam111.doc\") wrd=null 2,下载: <%@ age contentType=\"text/html;charset=gb2312\" import= \"java.io.*\"%> <%// 得到文件名字和路径 tring filename = \"j .doc\"; tring filepath = \"C:\\\"; // 设置响应头和下载保存的文件名 re o e.setContentType(\"A LICATION/OCTET-STREAM\"); re o e.setHeader(\"Content-Di osition\ // 打开指定文件的流信息 java.io.FileI utStream fileI utStream = ew java.io.FileI utStream(filepath + filename); //FileOutputStream out = ew FileOutputStream(filepath+\"测试\\\" + filename); // 写出流信息 int i; while ((i=fileI utStream.read()) != -1) { JAVA天堂 out.write(i); } fileI utStream.close(); out.close(); %> 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务