已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 byte1444; int length; while (byteread=inStream.read(buffer)!=-1) out.println(DTB+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; ifile.length; i+) if(filei.isFile() filei.toString(); FileInputStream input=new FileInputStream(filei); FileOutputStream output=new FileOutputStream(url2+/+(filei.getName().toString(); byte b=new byte1024*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 byte100; 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 byte1444; int length; while (byteread=inStream.read(buffer)!=-1) out.println(DTB+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=.*% % URL stdURL = null; BufferedReader stdIn = null; PrintWriter stdOut = null; try stdURL = new URL(); 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=.*% % int bytesum=0; int byteread=0; URL url = new URL(/sms/micheal/logo.gif); URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); FileOutputStream fs=new FileOutputStream( c:/abc.gif); byte buffer =new byte1444; int length; while (byteread=inStream.read(buffer)!=-1) out.println(DTB+byteread+/B/DT); bytesum+=byteread; System.out.println(bytesum); fs.write(buffer,0,byteread); % 10。按行读文件 % page contentType=text/html; charset=gb2312 % % page import=java.io.* % % FileReader myFileReader=new FileReader(c:/哈哈.txt); BufferedReader myBuffere
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 天津市津南区多校2024-2025学年九年级上学期期中英语试题(含答案)
- 华融资产招聘笔试题及答案
- 公务员面试快递面试题及答案
- 恒力集团秋招笔试题及答案
- 海南航空秋招题库及答案
- 国家能源集团秋招试题及答案
- 贵州茅台秋招试题及答案
- 光大集团秋招试题及答案
- 公务员考试试卷销毁试题及答案
- 公务员考试实际竞争试题及答案
- 2025至2030中国生物质燃料行业市场发展现状及竞争格局与投资发展报告
- TIA病情观察及护理
- 经皮椎体成形术
- 机械设计制造工艺和基础习题及答案
- 华为销售团队管理制度
- 2025-2030全球及中国航空航天风洞试验服务行业市场现状供需分析及市场深度研究发展前景及规划可行性分析研究报告
- 电气用电安全知识培训
- 酒店点菜技巧培训
- 2025年心理b证笔试试题及答案
- 2024-2025学年新乡市一中八年级上册期末考试数学试卷(含部分答案)
- 高一上学期统编版(2019)必修中外历史纲要上翻书大赛课件
评论
0/150
提交评论