工具类,通过系统代码实现点击按钮拷贝U盘里的文件到指定的文件夹,工具类

```

代码块

public class Utilities {

private static final StringTAG ="Utilities";

public static final StringUSB_PATH ="/storage/usbotg";

public static final StringUSB_NAVI_FILE ="/storage/usbotg/AutoNaviData/ReLoginCode.csv";

public static final StringPAD_NAVI_FOLDER ="/oem/AutoNaviData";

public static final StringPAD_NAVI_FILE_NAME ="ReLoginCode.csv";

public static boolean copyNaviFile(Context context) {

return copyFile(USB_NAVI_FILE,PAD_NAVI_FOLDER,PAD_NAVI_FILE_NAME, context);

}

public static boolean copyFile(String fromFilePath, String toFolderPath, String toFileName, Context context) {

String toFilePath = toFolderPath +"/" + toFileName;

Log.d(TAG,"copyFile from " + fromFilePath +" to " + toFilePath);

File usbDir =new File(USB_PATH);

if (!usbDir.exists()) {

Toast.makeText(context,"请插入U盘", Toast.LENGTH_SHORT).show();

Log.e(TAG,"usbDir: " + usbDir +" usb file exists usbDir.");

return false;

}

try {

File fromFile =new File(fromFilePath);

if (!fromFile.exists() || fromFile.length() ==0 || fromFile.isDirectory() || !fromFile.isFile() || !fromFile.canRead()) {

Log.e(TAG,"copyFile: " + fromFile +" usb file not exist.");

Toast.makeText(context,"源文件不存在", Toast.LENGTH_SHORT).show();

return false;

}

if (!(new File(toFolderPath).exists())) {

new File(toFolderPath).mkdir();

}

Runtime runtime = Runtime.getRuntime();

java.lang.Process process = runtime.exec("cp " + fromFilePath +" " + toFilePath);

//权限runtime.exec("chmod 777 "+toFilePath);

            process.waitFor();

Log.d(TAG,"copyFile completed from fromFilePath to " + toFilePath);

Toast.makeText(context,"拷贝成功", Toast.LENGTH_SHORT).show();

return true;

}catch (Exception e) {

Log.e(TAG,"copyFile " + e.toString());

Toast.makeText(context,"拷贝失败", Toast.LENGTH_SHORT).show();

e.printStackTrace();

return false;

}

}

}

```

你可能感兴趣的:(工具类,通过系统代码实现点击按钮拷贝U盘里的文件到指定的文件夹,工具类)