java中提供了io類庫,可以輕松的用java實現(xiàn)對文件的各種操作。下面就來說一下如何用java來實現(xiàn)這些操作。
新建目錄
<%@ age contentType="text/html;charset=gb2312"%>
<%
//String URL = request.getRequestURI();
String filePath="C:\測試\";
filePath=filePath.toString();//中文轉(zhuǎn)換
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%>
新建文件
<%@ age contentType="text/html;charset=gb2312"%>
<%@ age import="java.io.*" %>
<%
String filePath="c:/測試/newFile.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new rintWriter(resultFile);
String content ="這是測試數(shù)據(jù)";
String trContent = content.toString();
myFile.println(strContent);
resultFile.close();
%>
刪除文件
<%@ age contentType="text/html;charset=gb2312"%>
<%
String filePath="c://測試//newFile.txt";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
if(myDelFile.exists())
{
myDelFile.delete();
out.println(filePath+"刪除成功!?。?);
}
else
{
out.println(filePath+"該文件不存在");
}
%>
文件拷貝<%@ age contentType="text/html charset=gb2312" %>
<%@ age import="java.io.*" %>
<%
int ytesum=0;
int yteread=0;
//file:讀到流中
I utStream i tream=new FileI utStream("c://測試//newFile.txt");
FileOutputStream fs=new FileOutputStream( "c://測試//copyFile.txt");
byte[] uffer =new yte[1444];
int length;
while ((byteread=i tream.read(buffer))!=-1)
{
out.println("<DT> lt gt;"+byteread+"</ gt lt;/DT>");
ytesum+=byteread;
out.println(bytesum);
fs.write(buffer,0,byteread);
}
i tream.close();
%>
整個文件夾拷貝
<%@ age contentType="text/html;charset=gb2312"%>
<%@ age import="java.io.*" %>
<%String url1="C:/aaa";
tring 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();
FileI utStream i ut=new FileI utStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+(file[i].getName()).toString());
yte[] =new yte[1024*5];
int le
while((len=i ut.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
i ut.close();
}
}
%>
JAVA天堂
文件下載
<%@ age contentType="text/html charset=gb2312"%>
<%@ age import="java.io.*" %>
<%
tring fileName = "newFile.txt".toString();
//讀到流中
I utStream i tream=new FileI utStream("c://測試//newFile.txt");
//設(shè)置輸出的格式
re o e.reset();
re o e.setContentType("text/plain");
re o e.addHeader("Content-Di osition","attachment filename="" + fileName + """);
//循環(huán)取出流中的數(shù)據(jù)
yte[] = ew yte[100];
int le
ervletOutputStream outStream = re o e.getOutputStream();
while((len=i tream.read(b)) gt;0)
outStream.write(b,0,len);
outStream.flush();
outStream.close();
i tream.close();
JAVA天堂
//o.close();
qlBean.disco ect();
}
catch(Exceptio ex)
{
out.println(ex.getMe age());
}
}
%>
把網(wǎng)頁保存成文件
<%@ age contentType="text/html;charset=gb2312"%>
<%@ age import="java.text.*,java.util.*,java.net.*,java.io.*"%>
<%
URL tdURL = ull;
ufferedReader tdI = ull;
rintWriter tdOut = ull;
try {
tdURL = ew URL("http://www.163.com");
}
catch (MalformedURLExceptio e) {
throw e;
}
try {
//將字節(jié)流轉(zhuǎn)變成為字符流
tdI = ew ufferedReader(new I utStreamReader(stdURL.ope tream()));
tring theFileName = "c://測試//163.html";
tdOut = ew rintWriter(new ufferedWriter(new FileWriter(theFileName.toString())));
}
catch (IOExceptio e) {
}
/***把URL指定的頁面以流的形式讀出,寫成指定的文件***/
try {
tring trHtml = "";
while((strHtml = tdIn.readLine())!=null) {
tdOut.println(strHtml);
}
}
catch (IOExceptio e) {
throw e;
}
finally {
直接下載網(wǎng)上的文件
<%@ age contentType="text/html;charset=gb2312"%>
<%@ age import="java.io.*"%>
<%@ age import="java.net.*"%>
<%
int ytesum=0;
int yteread=0;
URL url = ew URL("http://java.chinaitlab.com/UploadFiles_8734/200611/20061116101405957.gif");
yte[] uffer =new yte[1444];
while ((byteread=i tream.read(buffer))!=-1)
{
out.println("<DT> lt gt;"+byteread+"</ gt lt;/DT>");
ytesum+=byteread;
//System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
按行讀文件
<%@ age contentType="text/html charset=gb2312" %>
<%@ age import="java.io.*" %>
<%
FileReader myFileReader=new FileReader("c:/哈哈.txt");
BufferedReader myBufferedReader=new ufferedReader(myFileReader);
String myString=null;
String resultString=new tring();
while((myString=myBufferedReader.readLine())!=null) {
resultString=resultString+myString+"< r>";
}
out.println(resultString);
myFileReader.close();
%>
對word文檔的處理(上傳與下載)
<%@ age contentType="a lication/msword" %>
<!-- 以上這行設(shè)定本網(wǎng)頁為excel格式的網(wǎng)頁 -->
<%
re o e.setHeader("Content-di osition","inline filename=test1.doc") //線上瀏覽方式
// re o e.setHeader("Content-di osition","attachment filename=test1.doc");//下載方式
//以上這行設(shè)定傳送到前端瀏覽器時的檔名為test1.doc
//就是靠這一行,讓前端瀏覽器以為接收到一個word檔
%>
//然后輸出動態(tài)內(nèi)容就可以得到一個word文檔了
1,打開:
1)文件頭上加:<%@ age contentType="a lication/msword"%>
xml文件里:
<mime-ma ing>
lt;exte io gt;doc</exte io gt;
lt;mime-type>a lication/msword</mime-type>
</mime-ma ing>
2)可以用js,以下代碼來自引用:
<%@ age contentType="text/html;charset=gb2312" import= "java.io.*"%>
<HTML>
< cript>
var wrd=new ActiveXObject("Word.A lication")
wrd.visible=true
alert ("您的"+wrd.A lication.Caption+"安裝路徑為: "+wrd.A lication.Path+" 版本號是:"+ wrd.A lication.version+" 注冊使用者是:"+wrd.A lication.UserName)
wrd.Documents.Add()
//wrd.Documents.Open("c:\exam.doc")
wrd.Selection.TypeText("Thi i ome text.")
wrd.A lication.Activate()
wrd.ActiveDocument.SaveAs("c:\exam111.doc")
wrd=null
</script>
</HTML>
2,下載:
<%@ age contentType="text/html;charset=gb2312" import= "java.io.*"%>
<%// 得到文件名字和路徑
tring filename = "j .doc";
tring filepath = "C:\";
// 設(shè)置響應(yīng)頭和下載保存的文件名
re o e.setContentType("A LICATION/OCTET-STREAM");
re o e.setHeader("Content-Di osition","attachment filename="" + filename + """);
// 打開指定文件的流信息
java.io.FileI utStream fileI utStream = ew java.io.FileI utStream(filepath + filename);
//FileOutputStream out = ew FileOutputStream(filepath+"測試\" + filename);
// 寫出流信息
int i;
while ((i=fileI utStream.read()) != -1) { JAVA天堂
out.write(i);
}
fileI utStream.close();
out.close();
%>
JAVA天堂