欢迎光临
我们一直在努力

Java中对文件的操作-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

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(“http://pimg.163.com/sms/micheal/logo.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(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 resultstring=new string();

while((mystring=mybufferedreader.readline())!=null) { resultstring=resultstring+mystring+”<br>”; }

out.println(resultstring); myfilereader.close();

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java中对文件的操作-JSP教程,Jsp/Servlet
分享到: 更多 (0)