JAVA中,怎样打开一个文件,保存文件.
解决方案 »
- 关于mapping.getInclude的使用方法
- axis webservice的helloworld
- 如果客户使用firefox, 你们是怎么处理session的?
- jsp怎么实现和MSXML2.XMLHTTP.3.0的ActiveXObject进行交互???
- 服务器资源共享。。。
- 数据库查询问题?在些恭候!!谢谢
- jsp实现添加视频文件到oracle数据库的blob字段
- jboss3下,如何不用重启,就可以刷新servlet,谢谢
- 哪有ms sql server的支持中文gb2312的jdbc驱动程序,要免费的,不能是试用版?
- 各位给我加个注释,100分,
- 请问如何实现远程文件的下载功能,在线等
- 用jsp实现excle、txt文件导入oracle,求助!
import java.io.*;
public class processFile{
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/test/x.txt
* @param newPath String 复制后路径 如:f:/test/x.txt
* @return boolean
*/
//复制单个文件
public boolean copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = -1;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
//int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}else{
return false;
}
}
catch (Exception e) {
System.out.println("processFile.copyFile() 复制单个文件操作出错"+e.getMessage());
return false;
}
return true;
}
/**
* 查找子文件夹
* 目的:查找与网页文件同名的网页文件图片目录,便于复制。
* @param oldPath String 当前文件夹路径 如:c:/test
* @param folderName String 目录名 如:yyy或xxx.files(网页文件目录)
* @return boolean
*/
public boolean findFolder(String curPath,String folderName){
boolean foundFlag = false;
try{
File curFile = new File(curPath);
String[] fileList = curFile.list();
File temp = null;
for(int i=0;i<fileList.length;i++){
temp=new File(curPath+File.separator+fileList[i]);
if(temp.isDirectory()){
//System.out.println(temp.getName());
if(temp.getName().equalsIgnoreCase(folderName)) {
foundFlag = true;
break;
}
}
}
temp = null;
}catch(Exception e){
System.out.println("processFile.findFolder()发生错误!"+e.getMessage());
return false;
}
if (foundFlag) return true;
else return false;
}
/**
* 查找文件
* 目的:查看文件是否存在
* @param curPath String 当前文件夹路径 如:c:/test
* @param fileName String 文件名 如:yyy.jpg
* @return boolean
*/
public boolean findFile(String curPath,String fileName){
boolean foundFlag = false;
try{
File curFile = new File(curPath);
String[] fileList = curFile.list();
File temp = null;
for(int i=0;i<fileList.length;i++){
temp=new File(curPath+File.separator+fileList[i]);
if(temp.isFile()){
//System.out.println(temp.getName());
if(temp.getName().equalsIgnoreCase(fileName)) {
foundFlag = true;
break;
}
}
}
temp = null;
}catch(Exception e){
System.out.println("processFile.findFile()发生错误!"+e.getMessage());
return false;
}
if (foundFlag) return true;
else return false;
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件夹路径 如:c:/test
* @param newPath String 复制后路径 如:f:/test
* @return boolean
*/
public boolean copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File old =new File(oldPath);
String[] file=old.list();
File temp=null;
for (int i = 0; i<file.length;i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 2];//读入缓冲
int len=-1;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("processFile.copyFolder() 复制整个文件夹内容操作出错"+e.getMessage());
return false;
}
return true;
}
public boolean delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myDelFile = new File(filePath);
if(myDelFile.exists())
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错!"+e.getMessage());
return false;
}
return true;
}
/**
* 删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public boolean delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if(myFilePath.exists())
myFilePath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错"+e.getMessage());
return false;
}
return true;
}
/**
* 删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public boolean delAllFile(String path) {
try{
File file = new File(path);
if(!file.exists()){
return false;
}
if(!file.isDirectory()) {
return false;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i<tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+ tempList[i]);//再删除空文件夹
}
}
}catch(Exception e){
System.out.println("删除文件夹操作出错"+e.getMessage());
return false;
}
return true;
}
public static void main(String[] args){
processFile pf = new processFile();
//System.out.println(pf.copyFolder("e://test","e://test2"));
System.out.println(pf.delFolder("e://test2"));
}