服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java中对文件的操作


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

file[] file=(new file(url1)).listfiles();

for(int i=0;i<file.length;i++){

if(file[i].isfile()){

file[i].tostring();

fileinputstream input=new fileinputstream(file[i]);

fileoutputstream output=new fileoutputstream(url2+"/"+(file[i].getname()).tostring());

byte[] b=new byte[1024*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 byte[100];

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

}

%>


8。把网页保存成文件


<%@ page import="java.text.*"%>

<%@ page import="java.util.*"%>

<%@ page import="java.io.*"%>

<%@ page import="java.net.*"%>

<%

url stdurl = null;

bufferedreader stdin = null;

printwriter stdout = null;

try {

stdurl = new url("http://www.163.com");

}

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="java.net.*"%>

<%

int bytesum=0;

int byteread=0;


url url = new url("/upfile/2007-11/20071117103525406.gif");

urlconnection conn = url.openconnection();

inputstream instream = conn.getinputstream();

fileoutputstream fs=new fileoutputstream( "c:/abc.gif");


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(buff

扫描关注微信公众号