您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java中對文件的操作有哪些”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java中對文件的操作有哪些”文章能幫助大家解決問題。
java中提供了io類庫,可以輕松的用java實現(xiàn)對文件的各種操作。下面就來說一下如何用java來實現(xiàn)這些操作。
1。新建目錄
<%@ page="" contenttype="text/html;charset=gb2312">
<%
String filePath="c:/aaa/";
filePath=filePath.toString();//中文轉(zhuǎn)換
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("
"+byteread+"
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");
//設(shè)置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename="" + fileName + """);
//循環(huán)取出流中的數(shù)據(jù)
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
7。數(shù)據(jù)庫字段中的文件下載
<%@ 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;
//打開數(shù)據(jù)庫
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 dbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得數(shù)據(jù)庫中的數(shù)據(jù)
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();
file://將數(shù)據(jù)庫中的數(shù)據(jù)讀到流中
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("
"+byteread+"
8。把網(wǎng)頁保存成文件
<%@ 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。直接下載網(wǎng)上的文件
<%@ page="" import="java.io.*">
<%@ page="" import="java.net.*">
<%
int bytesum=0;
int byteread=0;
URL url = new URL("https://cache.yisu.com/upload/information/20200703/145/53327.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("
"+byteread+"
關(guān)于“Java中對文件的操作有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。