首页 > 学院 > 开发设计 > 正文

Java中对文件的操作

2019-11-18 14:29:37
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表