Notice
Recent Posts
Recent Comments
Link
SeouliteLab
[Java/자바] 파일 소유자 이름 확인 본문
Java 프로그램에서 파일의 소유자 이름을 확인하는 방법을 알아보겠습니다. 파일의 소유자 이름을 확인하는 것은 파일 시스템을 관리하거나 보안 상의 이유로 유용할 수 있습니다.
예제 1: 파일의 소유자 이름 확인
import java.nio.file.*;
public class FileOwnerExample {
public static void main(String[] args) {
try {
Path filePath = Paths.get("data/sample.txt");
String owner = Files.getOwner(filePath).getName();
System.out.println("File owner: " + owner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
위 예제는 "data/sample.txt" 파일의 소유자 이름을 확인하는 Java 프로그램입니다. Files 클래스의 getOwner() 메서드를 사용하여 파일의 소유자를 확인합니다.
예제 2: 다른 파일의 소유자 이름 확인
import java.nio.file.*;
public class AnotherFileOwnerExample {
public static void main(String[] args) {
try {
Path filePath = Paths.get("data/anotherfile.txt");
String owner = Files.getOwner(filePath).getName();
System.out.println("File owner: " + owner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 예제는 다른 파일인 "data/anotherfile.txt"의 소유자 이름을 확인합니다. 이를 통해 다양한 파일에 대해 소유자 정보를 확인하는 방법을 알 수 있습니다.
예제 3: 디렉토리의 소유자 이름 확인
import java.nio.file.*;
public class DirectoryOwnerExample {
public static void main(String[] args) {
try {
Path dirPath = Paths.get("data");
String owner = Files.getOwner(dirPath).getName();
System.out.println("Directory owner: " + owner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
위 예제는 "data" 디렉토리의 소유자를 확인합니다. 파일뿐만 아니라 디렉토리의 소유자도 확인할 수 있습니다.
예제 4: 소유자 정보 출력
import java.nio.file.*;
import java.nio.file.attribute.*;
public class FileOwnerAttributesExample {
public static void main(String[] args) {
try {
Path filePath = Paths.get("data/sample.txt");
UserPrincipal owner = Files.getOwner(filePath);
System.out.println("File owner: " + owner.toString());
FileAttribute<?>[] attributes = Files.readAttributes(filePath, "posix:owner");
System.out.println("File owner (POSIX): " + attributes[0].value());
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 예제는 파일의 소유자 정보를 출력하는 데에 추가적인 방법을 제공합니다. Files 클래스의 readAttributes() 메서드를 사용하여 소유자 정보를 가져올 수 있습니다.
예제 5: 파일 소유자 변경
import java.nio.file.*;
import java.nio.file.attribute.*;
public class ChangeFileOwnerExample {
public static void main(String[] args) {
try {
Path filePath = Paths.get("data/sample.txt");
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
UserPrincipal owner = lookupService.lookupPrincipalByName("newowner");
Files.setOwner(filePath, owner);
System.out.println("File owner changed successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 예제는 파일의 소유자를 변경하는 방법을 보여줍니다. FileSystems 클래스의 getUserPrincipalLookupService() 메서드를 사용하여 UserPrincipalLookupService를 가져와서 파일의 소유자를 변경합니다.
예제 6: 파일 소유자 확인 후 권한 부여
import java.nio.file.*;
import java.nio.file.attribute.*;
public class CheckAndGrantPermissionsExample {
public static void main(String[] args) {
try {
Path filePath = Paths.get("data/sample.txt");
UserPrincipal owner = Files.getOwner(filePath);
if (owner.getName().equals("admin")) {
Set<PosixFilePermission> permissions = new HashSet<>();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(filePath, permissions);
System.out.println("Permissions granted to owner.");
} else {
System.out.println("Owner does not have admin privileges.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 예제는 파일 의 소유자를 확인하고, 소유자가 특정 사용자인 경우에만 권한을 부여하는 방법을 보여줍니다. setPosixFilePermissions() 메서드를 사용하여 POSIX 권한을 설정할 수 있습니다.
사용된 디렉토리 및 파일
위 예제에서 사용된 디렉토리와 파일은 다음과 같습니다:
- 디렉토리: data
- 파일: sample.txt, anotherfile.txt
'프로그래밍' 카테고리의 다른 글
[Java/자바] 강제적으로 OutOfMemoryError 발생시키기 (0) | 2024.03.10 |
---|---|
[Java/자바] Java 10 - var 키워드로 변수 선언 (지역 변수 타입 추론하기) (0) | 2024.03.10 |
[Java/자바] 파일 읽기, 쓰기 권한 설정 방법 (0) | 2024.03.10 |
[Java/자바] byte 배열을 String으로 변환하는 방법 (0) | 2024.03.10 |
[Java/자바] byte 배열을 String으로 변환하는 방법 (0) | 2024.03.09 |