import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * Java实现文件复制、剪切、删除操作 * 文件指文件或文件夹 * 文件分割符统一用"\\" */ public class FileOperateDemo { /** * 复制文件或文件夹 * @param srcPath 源文件或源文件夹的路径 * @param destDir 目标文件所在的目录 * @return */ public static boolean copyGeneralFile(String srcPath, String destDir) { boolean flag = false; File file = new File(srcPath); if(!file.exists()) { // 源文件或源文件夹不存在 return false; } if(file.isFile()) { // 文件复制 flag = copyFile(srcPath, destDir); } else if(file.isDirectory()) { // 文件夹复制 flag = copyDirectory(srcPath, destDir); } return flag; } /** * 默认的复制文件方法,默认会覆盖目标文件夹下的同名文件 * @param srcPath * 源文件绝对路径 * @param destDir * 目标文件所在目录 * @return boolean */ public static boolean copyFile(String srcPath, String destDir) { return copyFile(srcPath, destDir, true/**overwriteExistFile*/); // 默认覆盖同名文件 } /** * 默认的复制文件夹方法,默认会覆盖目标文件夹下的同名文件夹 * @param srcPath 源文件夹路径 * @param destPath 目标文件夹所在目录 * @return boolean */ public static boolean copyDirectory(String srcPath, String destDir) { return copyDirectory(srcPath, destDir, true/**overwriteExistDir*/); } /** * 复制文件到目标目录 * @param srcPath * 源文件绝对路径 * @param destDir * 目标文件所在目录 * @param overwriteExistFile * 是否覆盖目标目录下的同名文件 * @return boolean */ public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) { boolean flag = false; File srcFile = new File(srcPath); if (!srcFile.exists() || !srcFile.isFile()) { // 源文件不存在 return false; } //获取待复制文件的文件名 String fileName = srcFile.getName(); String destPath = destDir + File.separator +fileName; File destFile = new File(destPath); if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { // 源文件路径和目标文件路径重复 return false; } if(destFile.exists() && !overwriteExistFile) { // 目标目录下已有同名文件且不允许覆盖 return false; } File destFileDir = new File(destDir); if(!destFileDir.exists() && !destFileDir.mkdirs()) { // 目录不存在并且创建目录失败直接返回 return false; } try { FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos = new FileOutputStream(destFile); byte[] buf = new byte[1024]; int c; while ((c = fis.read(buf)) != -1) { fos.write(buf, 0, c); } fos.flush(); fis.close(); fos.close(); flag = true; } catch (IOException e) { e.printStackTrace(); } return flag; } /** * * @param srcPath 源文件夹路径 * @param destPath 目标文件夹所在目录 * @return */ public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) { if(destDir.contains(srcPath)) return false; boolean flag = false; File srcFile = new File(srcPath); if (!srcFile.exists() || !srcFile.isDirectory()) { // 源文件夹不存在 return false; } //获得待复制的文件夹的名字,比如待复制的文件夹为"E:\\dir\\"则获取的名字为"dir" String dirName = srcFile.getName(); //目标文件夹的完整路径 String destDirPath = destDir + File.separator + dirName + File.separator; File destDirFile = new File(destDirPath); if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { return false; } if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) { // 目标位置有一个同名文件夹且不允许覆盖同名文件夹,则直接返回false return false; } if(!destDirFile.exists() && !destDirFile.mkdirs()) { // 如果目标目录不存在并且创建目录失败 return false; } File[] fileList = srcFile.listFiles(); //获取源文件夹下的子文件和子文件夹 if(fileList.length==0) { // 如果源文件夹为空目录则直接设置flag为true,这一步非常隐蔽,debug了很久 flag = true; } else { for(File temp: fileList) { if(temp.isFile()) { // 文件 flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir); // 递归复制时也继承覆盖属性 } else if(temp.isDirectory()) { // 文件夹 flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir); // 递归复制时也继承覆盖属性 } if(!flag) { break; } } } return flag; } /** * 删除文件或文件夹 * @param path * 待删除的文件的绝对路径 * @return boolean */ public static boolean deleteFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { // 文件不存在直接返回 return flag; } flag = file.delete(); return flag; } /** * 由上面方法延伸出剪切方法:复制+删除 * @param destDir 同上 */ public static boolean cutGeneralFile(String srcPath, String destDir) { boolean flag = false; if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { // 复制和删除都成功 flag = true; } return flag; } public static void main(String[] args) { /** 测试复制文件 */ System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/")); // 一般正常场景 System.out.println(copyGeneralFile("d://notexistfile", "d://test/")); // 复制不存在的文件或文件夹 System.out.println(copyGeneralFile("d://test/test.html", "d://test/")); // 待复制文件与目标文件在同一目录下 System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/")); // 覆盖目标目录下的同名文件 System.out.println(copyFile("d://test/", "d://test2", false)); // 不覆盖目标目录下的同名文件 System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); // 复制文件到一个不可能存在也不可能创建的目录下 System.out.println("---------"); /** 测试复制文件夹 */ System.out.println(copyGeneralFile("d://test/", "d://test2/")); System.out.println("---------"); /** 测试删除文件 */ System.out.println(deleteFile("d://a/")); } }
更多java实现文件复制、剪切文件和删除示例相关文章请关注PHP中文网!

JVMは、Javaコードをマシンコードに変換し、リソースを管理することで機能します。 1)クラスの読み込み:.classファイルをメモリにロードします。 2)ランタイムデータ領域:メモリ領域を管理します。 3)実行エンジン:実行バイトコードを解釈またはコンパイルします。 4)ローカルメソッドインターフェイス:JNIを介してオペレーティングシステムと対話します。

JVMにより、Javaはプラットフォームを介して実行できます。 1)jvmは、bytecodeをロード、検証、実行します。 2)JVMの作業には、クラスの読み込み、バイトコード検証、解釈の実行、およびメモリ管理が含まれます。 3)JVMは、動的クラスの読み込みや反射などの高度な機能をサポートしています。

Javaアプリケーションは、次の手順を通じて異なるオペレーティングシステムで実行できます。1)ファイルまたはパスクラスを使用してファイルパスを処理します。 2)System.getEnv()を介して環境変数を設定および取得します。 3)MavenまたはGradleを使用して、依存関係を管理し、テストします。 Javaのクロスプラットフォーム機能は、JVMの抽象化レイヤーに依存していますが、特定のオペレーティングシステム固有の機能の手動処理が必要です。

Javaには、さまざまなプラットフォームでの特定の構成とチューニングが必要です。 1)-XMSや-XMXなどのJVMパラメーターを調整して、ヒープサイズを設定します。 2)ParallelGCやG1GCなどの適切なごみ収集戦略を選択します。 3)さまざまなプラットフォームに適応するようにネイティブライブラリを構成します。これらの測定により、Javaアプリケーションはさまざまな環境で最適に機能することができます。

Osgi、apachecommonslang、jna、andjvmoptionsareeffectiveforformplatform-specificchallengesinjava.1)osgimanagesdependenciesandisolatescomponents.2)apachecommonslangprovidesutilityfunctions.3)jnaallowsnativecode.4)

jvmmanagesgarbagecollectionacrossplatformseftivivivivitybyusagenerationalaphadadadaptingtosandhardwaredefferences.itemployscollectorslikeserial、parallel、cms、andg1、各sutitedfordifferentscenarios

Javaは、Javaの「Write and Averywherewhere」という哲学がJava Virtual Machine(JVM)によって実装されているため、変更なしで異なるオペレーティングシステムで実行できます。コンパイルされたJavaバイトコードとオペレーティングシステムの間の仲介者として、JVMはバイトコードを特定のマシン命令に変換し、JVMがインストールされた任意のプラットフォームでプログラムが独立して実行できることを確認します。

Javaプログラムの編集と実行は、BytecodeとJVMを通じてプラットフォームの独立性を達成します。 1)Javaソースコードを書き、それをbytecodeにコンパイルします。 2)JVMを使用して、任意のプラットフォームでByteCodeを実行して、コードがプラットフォーム間で実行されるようにします。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

ホットトピック









