java jsp 文件操作 文件创建相关
1;
/**
* 创建文件夹
* @param adir String 文件的路径文件名
*/
public static void makeDir(String dir) {
File d = new File(dir);
if (!d.exists()) {
d.mkdir();
}
}
/**
* 拷贝一个目录下的文件(不包括目录本身)到另一个目录
* @param aoldDir String 要拷贝的目录
* @param anewDir String 拷贝到的目录
* @throws IOException
*/
public static void copyDir(String aoldDir, String anewDir) throws IOException {
File oldDir = new File(aoldDir);
File newDir = new File(anewDir);
if (oldDir.isDirectory()) {
if (!newDir.exists()) {
newDir.mkdir();
}
String[] children = oldDir.list();
for (int i = 0; i < children.length; i++) {
copyDir(new File(oldDir, children[i]),
new File(newDir, children[i]));
}
}
else {
copyFile(oldDir, newDir);
}
}
2;
<%
String name="add";
String path1=request.getRealPath("/")+name+"/";
out.println(path1);
File aa=new File(path1);
if(!aa.exists())
{
boolean b=aa.mkdir();
out.println(b);
}
3;
package com;
import java.io.*;
public class Test1 {
public static void main(String []args){
try {
if(! new File("f:/oo/1").isDirectory())
{
new File("f:/oo/1").mkdir();
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
4;
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream("d:1.xls");
out=new FileOutputStream("c:/1/1.xls");
byte buff[]=new byte[1024];
int len;
try {
while((len=in.read(buff))>-1)
{
out.write(buff,0,len);
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally
{
try {
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
5;
package com.yeeyoo.str;
import java.io.*;
public class Test {
/*public static void copyDir(String aoldDir,String anewrDir) throws IOException
{
File oldDir =new File(aoldDir);
File newDir =new File(anewrDir);
if(oldDir.isDirectory())
{
if(!newDir.isDirectory())
{
newDir.mkdirs();
}
String children[]=oldDir.list();
for(int i=0;i<children.length;i++)
{
copyDir(new File(oldDir,children[i]).toString(),new File(newDir,children
[i]).toString());
}
}
else
{
copyDir(oldDir.toString(),newDir.toString());
System.out.println("sfsdfg");
}
}
*/
public static boolean move(String srcFile,String destPath)
{
File file=new File(srcFile);
File dir=new File(destPath);
boolean success=file.renameTo(new File(dir,file.getName()));
return success;
}
public static void main(String args[])
{
/*try {
copyDir
("C:/Tomcat5/webapps/yeeyoo/index.jsp","C:/Tomcat5/webapps/yeeyoo/users/bb/index.jsp");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}*/
boolean b=move
("C:/Tomcat5/webapps/yeeyoo/index.jsp","C:/Tomcat5/webapps/yeeyoo/users/bb/index.jsp");
System.out.println("dd");
System.out.println(b);
}
}
6;File oldFile=new File("d:/1.xls");
String newPath="c:/1/";
File fnewpath=new File(newPath);
if(!fnewpath.exists())
{
fnewpath.mkdirs();
}
File fnew=new File(newPath+oldFile.getName());
oldFile.renameTo(fnew);