java中对文件的操作

上传人:第*** 文档编号:30975033 上传时间:2018-02-03 格式:DOC 页数:6 大小:32KB
返回 下载 相关 举报
java中对文件的操作_第1页
第1页 / 共6页
java中对文件的操作_第2页
第2页 / 共6页
java中对文件的操作_第3页
第3页 / 共6页
java中对文件的操作_第4页
第4页 / 共6页
java中对文件的操作_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《java中对文件的操作》由会员分享,可在线阅读,更多相关《java中对文件的操作(6页珍藏版)》请在金锄头文库上搜索。

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。新建文件 % pa

2、ge 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(

3、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。文件拷贝 % pa

4、ge 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)!=

5、-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

6、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,

7、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); re

8、sponse.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.*% % p

9、age 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(); Db

10、aObj.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 (

11、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 =

12、null; PrintWriter stdOut = null; try stdURL = new URL(http:/); 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 指定的页面以流的形式读出,写成指定的文

13、件*/ 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=.

14、*% % int bytesum=0; int byteread=0; URL url = new URL(http:/ 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(D

15、TB+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 myBufferedReader=new BufferedReader(myFileReader); String myString=null; String

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号