Tình hình là em tạo 1 folder Test, trong folder đó có 1 file text Text.txt, em định copy nguyên folder đó sang folder Program File ( hoặc Program File (x86)), nhưng báo lỗi, anh chị giúp em cách sửa với
Mã:
package test; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream; import org.apache.commons.io.FileUtils; public class CopyFile { public static void main(String[] args) { File srcFolder = new File("C:/Users/sony/Desktop/test"); File destFolder = new File("C:/Program Files/test"); //make sure source exists if(!srcFolder.exists()){ System.out.println("Directory does not exist."); //just exit System.exit(0); }else{ try{ copyFolder(srcFolder,destFolder); }catch(IOException e){ e.printStackTrace(); //error, just exit System.exit(0); } } System.out.println("Done"); } public static void copyFolder(File src, File dest) throws IOException{ if(src.isDirectory()){ //if directory not exists, create it if(!dest.exists()){ if(dest.mkdirs()) { System.out.println("Directory copied from " + src + " to " + dest); } else { System.out.println("Failed to create multiple directories!"); } } //list all the directory contents String files[] = src.list(); for (String file : files) { //construct the src and dest file structure File srcFile = new File(src, file); File destFile = new File(dest, file); //recursive copy copyFolder(srcFile,destFile); } }else{ //if file, then copy it //Use bytes stream to support all file types InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; //copy the file content in bytes while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } in.close(); out.close(); System.out.println("File copied from " + src + " to " + dest); } }}
Thông báo lỗi:
Failed to create multiple directories!
java.io.FileNotFoundException: C:\Program Files\test\test.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at test.CopyFile.copyFolder(CopyFile.java:89)
at test.CopyFile.copyFolder(CopyFile.java:82)
at test.CopyFile.main(CopyFile.java:45)
Theo em tìm hiểu thì do cái UAC nó chặn, mình phải có quyền administrator mới tạo được folder trong Program File, loay hoay thì tạo thêm 1 file manifest như sau:
Mã:
Manifest-Version: 1.0
Main-;?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<v3:trustInfo xmlns:v3="urn:schemas-microsoft-com:asm.v3">
<v3:security> <v3:requestedPrivileges>
<v3:requestedExecutionLevel level="requireAdministrator"/>
</v3:requestedPrivileges>
</v3:security>
</v3:trustInfo>
</assembly>
Nhưng vẫn bị lỗi đó. Giờ em không biết phải làm sao, mong anh chị giúp đỡ!
View more random threads:
Với sự đa dạng và phong phú về mẫu mã và giá cả, choxeonline.vn hứa hẹn sẽ là địa chỉ tin cậy cho những ai đang tìm kiếm chiếc xe ô tô ưng ý. Từ những mẫu xe hạng sang đến những chiếc xe gia đình...
Khám Phá Đa Dạng Sản Phẩm Xe Ô Tô...