jsp 中對檔案及資料夾進行操作
阿新 • • 發佈:2019-02-13
/**
* 複製單個檔案
* @param oldPath String 原檔案路徑 如:c:/fqf.txt
* @param newPath String 複製後路徑 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //檔案存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原檔案
FileOutputStream fs = new FileOutputStream(newPath);
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();
}
}
catch (Exception e) {
System.out.println("複製單個檔案操作出錯");
e.printStackTrace();
* 複製單個檔案
* @param oldPath String 原檔案路徑 如:c:/fqf.txt
* @param newPath String 複製後路徑 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //檔案存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原檔案
FileOutputStream fs = new FileOutputStream(newPath);
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();
}
}
catch (Exception e) {
System.out.println("複製單個檔案操作出錯");
e.printStackTrace();