目录,文件操作:删除,创建,拷贝...
String path = "c:\\aa ";
file = new File(path);
if (file.exists()) {// 检查目录是否存在
// 目录存在,删除目录及下所有文件和目录
Runtime.getRuntime().exec( "cmd.exe /c rd " + path + " /s/q ");
}
file.mkdir();// 建立目录
System.out.println(path);
Runtime.getRuntime().exec( "xcopy f:\\bb " + path + " /s/e ");
如果目录不存在,可以创建目录成功,而且拷贝也成功;但是目录如何存在,删除可以,创建目录不成功,拷贝也不可以。请各位DX帮忙看看为什么?在此先谢谢。
[解决办法]
Runtime.getRuntime().exec( "cmd.exe /c rd " + path + " /s/q ");是异步的
[解决办法]
java 自己有删除,拷贝目录,等方法亚,你的功能我都做过呀,直接用java 的方法就可以了,可以不Runtime.getRuntime().exec 就可以实现亚,你可以尝试
[解决办法]
/** 删除指定文件
*
*/
public void deleteFile(String filename) {
File f = new File(filename);
if (!f.exists()) System.out.print( "Delete: no such file or directory: " +filename);
if (!f.canWrite()) System.out.print( "Delete: write protected: " + filename);
if (f.isDirectory()) {
String[] files = f.list();
if (files.length > 0)
System.out.print( "Delete: directory not empty: " + filename);
}
boolean success = f.delete();
if (!success) System.out.print( "Delete: deletion failed ");
System.out.print( "delete ");
}
[解决办法]
/**
*
* @param filename 以有的文件(c:\a.doc)
* @param copyto 要copy到的文件名称(d:\A.doc)
* @throws IOException
*/
public static void copyFile(String filename,String copyto)throws IOException{
InputStream is=new FileInputStream(filename);
OutputStream streamOut = new FileOutputStream(copyto);//定义输出流
int byteRead = 0;
byte[] buffer = new byte[8192];
while ((byteRead = is.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, byteRead);
}
streamOut.close();
is.close();
}
[解决办法]
/**
* 删除文件夹下所有文件
*
* @param filePath
* @throws IOException
*/
public static void deleteDir(String filePath)throws IOException{
File path=new File(filePath);
if(path.exists()){
if(path.isFile()){
path.delete();
}else{
String[] fileList=path.list();
for(int i=0;i <fileList.length;i++){
File child=new File(path + "\\ " +fileList[i]);
if(child.isFile()){
child.delete();
}else{
String childPath=child.toString();
deleteDir(childPath);
}
child.delete();
}
path.delete();
}
}
}
[解决办法]
可以这样
Process process = Runtime.getRuntime().exec( "cmd.exe /c rd " + path + " /s/q ");
process.waitFor();
if (process.exitValue() == 0)
file.mkdir();
else
...
[解决办法]
1。新建目录
String filePath= "root:/XXX/ ";
filePath=filePath.toString();//中文转换
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
2。新建文件
String filePath= "root:/XXX.XXX ";
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。删除文件
String filePath= "root:/XXX.XXX ";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
myDelFile.delete();
4。文件拷贝
int bytesum=0;
int byteread=0;
InputStream inStream=new FileInputStream( "root:/XXX.XXX ");
FileOutputStream fs=new FileOutputStream( "root:/../XXX.XXX ");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
5。整个文件夹拷贝
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。文件下载
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;
//打开数据縼E
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();
//将数据库中的数据读到流中
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);
}
%>