Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Chuyển 1 folder vào folder program file?

    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 đỡ!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn thử xem lại đường dẫn nhé.
    java.io.FileNotFoundException: C:\Program Files\test\test.txt (The system cannot find the path specified)
    Thử thay chuỗi đường dẫn trong

    Mã:
    File srcFolder = new File("C:/Users/sony/Desktop/test");File destFolder = new File("C:/Program Files/test");
    bằng C:\\Users\\sony\\Desktop\\test và cả destFolder nữa.
    Đừờng dẫn trong windows thì phải là \\

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •