Java는 java.lang 패키지에 래퍼 클래스라는 특정 클래스를 제공합니다. 이러한 클래스의 개체는 그 안에 있는 기본 데이터 유형을 래핑합니다.
래퍼 클래스를 사용하여 ArrayList, HashMap 등과 같은 다양한 Collection 개체에 기본 데이터 유형을 추가할 수도 있습니다. 또한 래퍼 클래스를 사용하여 네트워크를 통해 기본 값을 전달할 수도 있습니다.
예
import java.util.Scanner;
public class WrapperExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value: ");
int i = sc.nextInt();
//Wrapper class of an integer
Integer obj = new Integer(i);
//Converting Integer object to String
String str = obj.toString();
System.out.println(str);
//Comparing with other object
int result = obj.compareTo(new Integer("124"));
if(result==0) {
System.out.println("Both are equal");
}else{
System.out.println("Both are not equal");
}
}
} 출력
Enter an integer value: 1211 1211 Both are not equalHow to create and use directories in Java?
디렉토리 생성
파일 클래스와 마찬가지로 java.nio 패키지의 Files 클래스는 createTempFile()을 제공합니다. 의 접두사와 접미사를 나타내는 두 개의 String 매개변수를 수락하고 지정된 세부 정보로 임시 파일을 생성하는 메서드입니다.
createDirectory () 파일 메소드 클래스는 필요한 디렉토리의 경로를 수락하고 새 디렉토리를 생성합니다.
예
다음 예제에서는 Files 클래스의 createDirectory() 메서드를 사용하여 새 디렉터리를 만듭니다.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\sample_directory ";
Path path = Paths.get(pathStr);
//Creating a directory
Files.createDirectory(path);
System.out.println("Directory created successfully");
}
} 출력
Directory created successfully
확인하면 생성된 디렉토리를 -
로 볼 수 있습니다.

디렉토리의 내용 나열
newDirectoryStream() 파일 메소드 클래스는 주어진 경로에서 디렉토리를 열고 디렉토리의 내용을 제공하는 디렉토리 스트림을 반환합니다.
예
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesExample {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\ExampleDirectory";
Path path = Paths.get(pathStr);
System.out.println("Contents off the specified directory");
DirectoryStream stream = Files.newDirectoryStream(path);
for (Path file: stream) {
System.out.println(file.getFileName());
}
}
} 출력
Contents off the specified directory demo1.pdf demo2.pdf sample directory1 sample directory2 sample directory3 sample directory4 sample_jpeg1.jpg sample_jpeg2.jpg test test1.docx test2.docx
디렉토리 필터 사용
다음 예제에서는 지정된 경로의 디렉터리를 필터링하는 DirectoryStream.Filter를 사용하여 디렉터리를 필터링할 수 있습니다.
예
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\ExampleDirectory";
Path path = Paths.get(pathStr);
System.out.println("Directories in the specified directory");
DirectoryStream.Filter filter = new DirectoryStream.Filter(){
public boolean accept(Path file) throws IOException {
return (Files.isDirectory(file));
}
};
DirectoryStream list = Files.newDirectoryStream(path, filter);
for (Path file : list) {
System.out.println(file.getFileName());
}
}
} 출력
Directories in the specified directory hidden directory1 hidden directory2 sample directory1