Java中对文件的操作

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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 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();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航