


Ausführliche Erläuterung des Hinzufügens, Löschens und Kopierens von Dateien und Verzeichnissen in Java
Dieser Artikel stellt hauptsächlich das Hinzufügen, Löschen und Kopieren von Java-Dateien und -Verzeichnissen vor. Er hat einen gewissen Referenzwert.
Es wird häufig bei der Entwicklung mit Java verwendet wie das Hinzufügen, Löschen und Kopieren von Dateien und Verzeichnissen. Ich habe eine Widget-Klasse geschrieben. Teilen Sie es mit allen, ich hoffe, Sie können mich korrigieren:
package com.wangpeng.utill; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.PrintWriter; /** * @author wangpeng * */ public class ToolOfFile { /** * 创建目录 * * @param folderPath * 目录目录 * @throws Exception */ public static void newFolder(String folderPath) throws Exception { try { java.io.File myFolder = new java.io.File(folderPath); if (!myFolder.exists()) { myFolder.mkdir(); } } catch (Exception e) { throw e; } } /** * 创建文件 * * @param filePath * 文件路径 * @throws Exception */ public static void newFile(String filePath) throws Exception { try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } } catch (Exception e) { throw e; } } /** * 创建文件,并写入内容 * * @param filePath * 文件路径 * @param fileContent * 被写入的文件内容 * @throws Exception */ public static void newFile(String filePath, String fileContent) throws Exception { // 用来写入字符文件的便捷类 FileWriter fileWriter = null; // 向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自己主动行刷新的新 PrintWriter printWriter = null; try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } fileWriter = new FileWriter(myFile); printWriter = new PrintWriter(fileWriter); printWriter.print(fileContent); printWriter.flush(); } catch (Exception e) { throw e; } finally { if (printWriter != null) { printWriter.close(); } if (fileWriter != null) { fileWriter.close(); } } } /** * 复制文件 * * @param oldPath * 被拷贝的文件 * @param newPath * 复制到的文件 * @throws Exception */ public static void copyFile(String oldPath, String newPath) throws Exception { InputStream inStream = null; FileOutputStream outStream = null; try { int byteread = 0; File oldfile = new File(oldPath); // 文件存在时 if (oldfile.exists()) { inStream = new FileInputStream(oldfile); outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } /** * 复制文件 * @param inStream 被拷贝的文件的输入流 * @param newPath 被复制到的目标 * @throws Exception */ public static void copyFile(InputStream inStream, String newPath) throws Exception { FileOutputStream outStream = null; try { int byteread = 0; outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, byteread); } outStream.flush(); } catch (Exception e) { throw e; } finally { if (outStream != null) { outStream.close(); } if (inStream != null) { inStream.close(); } } } /** * 复制目录 * * @param oldPath * 被复制的目录路径 * @param newPath * 被复制到的目录路径 * @throws Exception */ public static void copyFolder(String oldPath, String newPath) throws Exception { try { (new File(newPath)).mkdirs(); // 假设目录不存在 则建立新目录 File a = new File(oldPath); String[] file = a.list(); File tempIn = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { tempIn = new File(oldPath + file[i]); } else { tempIn = new File(oldPath + File.separator + file[i]); } if (tempIn.isFile()) { copyFile(tempIn.getAbsolutePath(), newPath + "/" + (tempIn.getName()).toString()); } else if (tempIn.isDirectory()) {// 假设是子目录 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { throw e; } } /** * 删除文件 * * @param filePathAndName */ public static void delFileX(String filePathAndName) { File myDelFile = new File(filePathAndName); myDelFile.delete(); } /** * 删除目录 * * @param path */ public static void delForder(String path) { File delDir = new File(path); if (!delDir.exists()) { return; } if (!delDir.isDirectory()) { return; } String[] tempList = delDir.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } else if (temp.isDirectory()) { // 删除完里面全部内容 delForder(path + "/" + tempList[i]); } } delDir.delete(); } public static void main(String[] args) { String oldPath = "F:/test/aaa/"; String newPath = "F:/test/bbb/"; try { // ToolOfFile.newFolder("F:/test/hello/"); // ToolOfFile.newFile("F:/test/hello/world.txt","我爱你,the world! "); ToolOfFile.copyFolder(oldPath, newPath); // ToolOfFile.delForder("F:/test/hello"); } catch (Exception e) { e.printStackTrace(); } System.out.println("OK"); } }
Das obige ist der detaillierte Inhalt vonAusführliche Erläuterung des Hinzufügens, Löschens und Kopierens von Dateien und Verzeichnissen in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

In dem Artikel werden Maven und Gradle für Java -Projektmanagement, Aufbau von Automatisierung und Abhängigkeitslösung erörtert, die ihre Ansätze und Optimierungsstrategien vergleichen.

In dem Artikel werden benutzerdefinierte Java -Bibliotheken (JAR -Dateien) mit ordnungsgemäßem Versioning- und Abhängigkeitsmanagement erstellt und verwendet, wobei Tools wie Maven und Gradle verwendet werden.

In dem Artikel wird in der Implementierung von mehrstufigem Caching in Java mithilfe von Koffein- und Guava-Cache zur Verbesserung der Anwendungsleistung erläutert. Es deckt die Einrichtungs-, Integrations- und Leistungsvorteile sowie die Bestrafung des Konfigurations- und Räumungsrichtlinienmanagements ab

In dem Artikel werden mit JPA für Objektrelationszuordnungen mit erweiterten Funktionen wie Caching und faulen Laden erläutert. Es deckt Setup, Entity -Mapping und Best Practices zur Optimierung der Leistung ab und hebt potenzielle Fallstricke hervor. [159 Charaktere]

Mit der Klassenbelastung von Java wird das Laden, Verknüpfen und Initialisieren von Klassen mithilfe eines hierarchischen Systems mit Bootstrap-, Erweiterungs- und Anwendungsklassenloadern umfasst. Das übergeordnete Delegationsmodell stellt sicher


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

PHPStorm Mac-Version
Das neueste (2018.2.1) professionelle, integrierte PHP-Entwicklungstool

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

EditPlus chinesische Crack-Version
Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

SublimeText3 Englische Version
Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!

SAP NetWeaver Server-Adapter für Eclipse
Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.