java本地地址怎么写( 二 )


targetFile为目标文件,file为源文件 * * @param targetFile * @param file */ public static void copyFile(File targetFile, File file) { if (targetFile.exists()) { System.out.println("文件" + targetFile.getAbsolutePath() + "已经存在,跳过该文件!"); return; } else { createFile(targetFile, true); } System.out.println("复制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath()); try { InputStream is = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(targetFile); byte[] buffer = new byte[1024]; while (is.read(buffer) != -1) { fos.write(buffer); } is.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static String[] listFile(File dir) { String absolutPath = dir.getAbsolutePath(); String[] paths = dir.list(); String[] files = new String[paths.length]; for (int i = 0; i < paths.length; i++) { files[i] = absolutPath + "/" + paths[i]; } return files; } public static void createFile(String path, boolean isFile) { createFile(new File(path), isFile); } public static void createFile(File file, boolean isFile) { if (!file.exists()) { if (!file.getParentFile().exists()) { createFile(file.getParentFile(), false); } else { if (isFile) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } else { file.mkdir(); } } } } } 。

java本地地址怎么写

文章插图