Java에서 일반적으로 사용되는 도구 클래스에는 문자열 및 MD5 암호화 및 복호화 클래스가 포함됩니다.
우리 Java 프로그래머는 프로젝트를 개발할 때 일부 도구 클래스를 자주 사용합니다. 오늘은 프로젝트에서 사용할 수 있는 두 가지 도구 클래스를 공유하겠습니다.
1. 문자열 도구 클래스
package com.itjh.javaUtil; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 文件相关操作辅助类。 * * @author 宋立君 * @date 2014年06月24日 */ public class FileUtil { private static final String FOLDER_SEPARATOR = "/"; private static final char EXTENSION_SEPARATOR = '.'; /** * 功能:复制文件或者文件夹。 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * 源文件 * @param outputFile * 目的文件 * @param isOverWrite * 是否覆盖(只针对文件) * @throws IOException */ public static void copy(File inputFile, File outputFile, boolean isOverWrite) throws IOException { if (!inputFile.exists()) { throw new RuntimeException(inputFile.getPath() + "源目录不存在!"); } copyPri(inputFile, outputFile, isOverWrite); } /** * 功能:为copy 做递归使用。 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * @param outputFile * @param isOverWrite * @throws IOException */ private static void copyPri(File inputFile, File outputFile, boolean isOverWrite) throws IOException { // 是个文件。 if (inputFile.isFile()) { copySimpleFile(inputFile, outputFile, isOverWrite); } else { // 文件夹 if (!outputFile.exists()) { outputFile.mkdir(); } // 循环子文件夹 for (File child : inputFile.listFiles()) { copy(child, new File(outputFile.getPath() + "/" + child.getName()), isOverWrite); } } } /** * 功能:copy单个文件 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * 源文件 * @param outputFile * 目标文件 * @param isOverWrite * 是否允许覆盖 * @throws IOException */ private static void copySimpleFile(File inputFile, File outputFile, boolean isOverWrite) throws IOException { // 目标文件已经存在 if (outputFile.exists()) { if (isOverWrite) { if (!outputFile.delete()) { throw new RuntimeException(outputFile.getPath() + "无法覆盖!"); } } else { // 不允许覆盖 return; } } InputStream in = new FileInputStream(inputFile); OutputStream out = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int read = 0; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); } /** * 功能:删除文件 * * @author 宋立君 * @date 2014年06月24日 * @param file * 文件 */ public static void delete(File file) { deleteFile(file); } /** * 功能:删除文件,内部递归使用 * * @author 宋立君 * @date 2014年06月24日 * @param file * 文件 * @return boolean true 删除成功,false 删除失败。 */ private static void deleteFile(File file) { if (file == null || !file.exists()) { return; } // 单文件 if (!file.isDirectory()) { boolean delFlag = file.delete(); if (!delFlag) { throw new RuntimeException(file.getPath() + "删除失败!"); } else { return; } } // 删除子目录 for (File child : file.listFiles()) { deleteFile(child); } // 删除自己 file.delete(); } /** * 从文件路径中抽取文件的扩展名, 例如. "mypath/myfile.txt" -> "txt". * @author 宋立君 * * @date 2014年06月24日 * @param 文件路径 * @return 如果path为null,直接返回null。 */ public static String getFilenameExtension(String path) { if (path == null) { return null; } int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return null; } int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); if (folderIndex > extIndex) { return null; } return path.substring(extIndex + 1); } /** * 从文件路径中抽取文件名, 例如: "mypath/myfile.txt" -> "myfile.txt"。 * @author 宋立君 * * @date 2014年06月24日 * @param path * 文件路径。 * @return 抽取出来的文件名, 如果path为null,直接返回null。 */ public static String getFilename(String path) { if (path == null) { return null; } int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path); } /** * 功能:保存文件。 * * @author 宋立君 * @date 2014年06月24日 * @param content * 字节 * @param file * 保存到的文件 * @throws IOException */ public static void save(byte[] content, File file) throws IOException { if (file == null) { throw new RuntimeException("保存文件不能为空"); } if (content == null) { throw new RuntimeException("文件流不能为空"); } InputStream is = new ByteArrayInputStream(content); save(is, file); } /** * 功能:保存文件 * * @author 宋立君 * @date 2014年06月24日 * @param streamIn * 文件流 * @param file * 保存到的文件 * @throws IOException */ public static void save(InputStream streamIn, File file) throws IOException { if (file == null) { throw new RuntimeException("保存文件不能为空"); } if (streamIn == null) { throw new RuntimeException("文件流不能为空"); } // 输出流 OutputStream streamOut = null; // 文件夹不存在就创建。 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } streamOut = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) { streamOut.write(buffer, 0, bytesRead); } streamOut.close(); streamIn.close(); } }
2. MD5 도구 클래스
package com.itjh.javaUtil; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 文件相关操作辅助类。 * * @author 宋立君 * @date 2014年06月24日 */ public class FileUtil { private static final String FOLDER_SEPARATOR = "/"; private static final char EXTENSION_SEPARATOR = '.'; /** * 功能:复制文件或者文件夹。 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * 源文件 * @param outputFile * 目的文件 * @param isOverWrite * 是否覆盖(只针对文件) * @throws IOException */ public static void copy(File inputFile, File outputFile, boolean isOverWrite) throws IOException { if (!inputFile.exists()) { throw new RuntimeException(inputFile.getPath() + "源目录不存在!"); } copyPri(inputFile, outputFile, isOverWrite); } /** * 功能:为copy 做递归使用。 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * @param outputFile * @param isOverWrite * @throws IOException */ private static void copyPri(File inputFile, File outputFile, boolean isOverWrite) throws IOException { // 是个文件。 if (inputFile.isFile()) { copySimpleFile(inputFile, outputFile, isOverWrite); } else { // 文件夹 if (!outputFile.exists()) { outputFile.mkdir(); } // 循环子文件夹 for (File child : inputFile.listFiles()) { copy(child, new File(outputFile.getPath() + "/" + child.getName()), isOverWrite); } } } /** * 功能:copy单个文件 * * @author 宋立君 * @date 2014年06月24日 * @param inputFile * 源文件 * @param outputFile * 目标文件 * @param isOverWrite * 是否允许覆盖 * @throws IOException */ private static void copySimpleFile(File inputFile, File outputFile, boolean isOverWrite) throws IOException { // 目标文件已经存在 if (outputFile.exists()) { if (isOverWrite) { if (!outputFile.delete()) { throw new RuntimeException(outputFile.getPath() + "无法覆盖!"); } } else { // 不允许覆盖 return; } } InputStream in = new FileInputStream(inputFile); OutputStream out = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int read = 0; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); } /** * 功能:删除文件 * * @author 宋立君 * @date 2014年06月24日 * @param file * 文件 */ public static void delete(File file) { deleteFile(file); } /** * 功能:删除文件,内部递归使用 * * @author 宋立君 * @date 2014年06月24日 * @param file * 文件 * @return boolean true 删除成功,false 删除失败。 */ private static void deleteFile(File file) { if (file == null || !file.exists()) { return; } // 单文件 if (!file.isDirectory()) { boolean delFlag = file.delete(); if (!delFlag) { throw new RuntimeException(file.getPath() + "删除失败!"); } else { return; } } // 删除子目录 for (File child : file.listFiles()) { deleteFile(child); } // 删除自己 file.delete(); } /** * 从文件路径中抽取文件的扩展名, 例如. "mypath/myfile.txt" -> "txt". * @author 宋立君 * * @date 2014年06月24日 * @param 文件路径 * @return 如果path为null,直接返回null。 */ public static String getFilenameExtension(String path) { if (path == null) { return null; } int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return null; } int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); if (folderIndex > extIndex) { return null; } return path.substring(extIndex + 1); } /** * 从文件路径中抽取文件名, 例如: "mypath/myfile.txt" -> "myfile.txt"。 * @author 宋立君 * * @date 2014年06月24日 * @param path * 文件路径。 * @return 抽取出来的文件名, 如果path为null,直接返回null。 */ public static String getFilename(String path) { if (path == null) { return null; } int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path); } /** * 功能:保存文件。 * * @author 宋立君 * @date 2014年06月24日 * @param content * 字节 * @param file * 保存到的文件 * @throws IOException */ public static void save(byte[] content, File file) throws IOException { if (file == null) { throw new RuntimeException("保存文件不能为空"); } if (content == null) { throw new RuntimeException("文件流不能为空"); } InputStream is = new ByteArrayInputStream(content); save(is, file); } /** * 功能:保存文件 * * @author 宋立君 * @date 2014年06月24日 * @param streamIn * 文件流 * @param file * 保存到的文件 * @throws IOException */ public static void save(InputStream streamIn, File file) throws IOException { if (file == null) { throw new RuntimeException("保存文件不能为空"); } if (streamIn == null) { throw new RuntimeException("文件流不能为空"); } // 输出流 OutputStream streamOut = null; // 文件夹不存在就创建。 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } streamOut = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) { streamOut.write(buffer, 0, bytesRead); } streamOut.close(); streamIn.close(); } }
문자열 조작 클래스와 일반적으로 사용되는 도구의 MD5 암호화 및 암호 해독 클래스에 대한 자세한 내용은 Java, PHP 중국어 웹사이트를 팔로우하세요!

이 기사에서는 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 및 Gradle을 사용하여 접근 방식과 최적화 전략을 비교합니다.

이 기사에서는 Maven 및 Gradle과 같은 도구를 사용하여 적절한 버전 및 종속성 관리로 사용자 정의 Java 라이브러리 (JAR Files)를 작성하고 사용하는 것에 대해 설명합니다.

이 기사는 카페인 및 구아바 캐시를 사용하여 자바에서 다단계 캐싱을 구현하여 응용 프로그램 성능을 향상시키는 것에 대해 설명합니다. 구성 및 퇴거 정책 관리 Best Pra와 함께 설정, 통합 및 성능 이점을 다룹니다.

이 기사는 캐싱 및 게으른 하중과 같은 고급 기능을 사용하여 객체 관계 매핑에 JPA를 사용하는 것에 대해 설명합니다. 잠재적 인 함정을 강조하면서 성능을 최적화하기위한 설정, 엔티티 매핑 및 모범 사례를 다룹니다. [159 문자]

Java의 클래스 로딩에는 부트 스트랩, 확장 및 응용 프로그램 클래스 로더가있는 계층 적 시스템을 사용하여 클래스로드, 링크 및 초기화 클래스가 포함됩니다. 학부모 위임 모델은 핵심 클래스가 먼저로드되어 사용자 정의 클래스 LOA에 영향을 미치도록합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기
