파일 사이즈 구하는 방법
1. Files
- java.nio.file.Files 클래스의 size 메서드는 파일의 크기를 byte 단위로 리턴합니다.
-
package file; import java.io.File; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileSize { public static void main(String[] args) throws IOException { System.out.println("\n-----------Files을 이용한 파일 크기 설정"); FilesEx files = new FilesEx(); files.files(); } } class FilesEx { public void files() throws IOException { Path path = Paths.get("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png"); float bytes = Files.size(path); float kb = bytes / 1024; float mb = kb / 1024; float gb = mb / 1024; System.out.println("bytes : " + bytes); System.out.println("kb : " + kb); System.out.println("mb : " + mb); System.out.println("gb : " + gb); } } /* 출력 -----------Files을 이용한 파일 크기 설정 bytes : 721856.0 kb : 704.9375 mb : 0.6884155 gb : 6.722808E-4 */
2. FileChannel
- java.nio.channels.FileChannel 클래스의 size()는 byte 단위의 파일의 크기를 리턴합니다.
-
package file; import java.io.File; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileSize { public static void main(String[] args) throws IOException { System.out.println("\n-----------FileChannel을 이용한 파일 크기 설정"); FileChannelEx fileChannel = new FileChannelEx(); fileChannel.fileChannel(); } } class FileChannelEx { public void fileChannel() throws IOException { Path path = Paths.get("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png"); FileChannel fileChannel = FileChannel.open(path); float bytes = fileChannel.size(); float kb = bytes / 1024; float mb = kb / 1024; float gb = mb / 1024; System.out.println("bytes : " + bytes); System.out.println("kb : " + kb); System.out.println("mb : " + mb); System.out.println("gb : " + gb); } } /* 출력 -----------FileChannel을 이용한 파일 크기 설정 bytes : 721856.0 kb : 704.9375 mb : 0.6884155 gb : 6.722808E-4 */
3. File
- java.io.File의 length() 메서드는 파일의 byte 단위 크기를 리턴합니다.
-
package file; import java.io.File; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileSize { public static void main(String[] args) throws IOException {= System.out.println("\n-----------File을 이용한 파일 크기 설정"); FileEx file = new FileEx(); file.file(); } } class FileEx { public void file() throws IOException { File file = new File("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png"); float bytes = file.length(); float kb = bytes / 1024; float mb = kb / 1024; float gb = mb / 1024; System.out.println("bytes : " + bytes); System.out.println("kb : " + kb); System.out.println("mb : " + mb); System.out.println("gb : " + gb); } } /* 출력 -----------File을 이용한 파일 크기 설정 bytes : 721856.0 kb : 704.9375 mb : 0.6884155 gb : 6.722808E-4 */
최종 코드
더보기
더보기
package file;
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileSize {
public static void main(String[] args) throws IOException {
System.out.println("\n-----------Files을 이용한 파일 크기 설정");
FilesEx files = new FilesEx();
files.files();
System.out.println("\n-----------FileChannel을 이용한 파일 크기 설정");
FileChannelEx fileChannel = new FileChannelEx();
fileChannel.fileChannel();
System.out.println("\n-----------File을 이용한 파일 크기 설정");
FileEx file = new FileEx();
file.file();
}
}
class FilesEx {
public void files() throws IOException {
Path path = Paths.get("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png");
float bytes = Files.size(path);
float kb = bytes / 1024;
float mb = kb / 1024;
float gb = mb / 1024;
System.out.println("bytes : " + bytes);
System.out.println("kb : " + kb);
System.out.println("mb : " + mb);
System.out.println("gb : " + gb);
}
}
class FileChannelEx {
public void fileChannel() throws IOException {
Path path = Paths.get("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png");
FileChannel fileChannel = FileChannel.open(path);
float bytes = fileChannel.size();
float kb = bytes / 1024;
float mb = kb / 1024;
float gb = mb / 1024;
System.out.println("bytes : " + bytes);
System.out.println("kb : " + kb);
System.out.println("mb : " + mb);
System.out.println("gb : " + gb);
}
}
class FileEx {
public void file() throws IOException {
File file = new File("C:\\Users\\namji\\OneDrive\\바탕 화면\\image\\meta.png");
float bytes = file.length();
float kb = bytes / 1024;
float mb = kb / 1024;
float gb = mb / 1024;
System.out.println("bytes : " + bytes);
System.out.println("kb : " + kb);
System.out.println("mb : " + mb);
System.out.println("gb : " + gb);
}
}
/*
출력
-----------Files을 이용한 파일 크기 설정
bytes : 721856.0
kb : 704.9375
mb : 0.6884155
gb : 6.722808E-4
-----------FileChannel을 이용한 파일 크기 설정
bytes : 721856.0
kb : 704.9375
mb : 0.6884155
gb : 6.722808E-4
-----------File을 이용한 파일 크기 설정
bytes : 721856.0
kb : 704.9375
mb : 0.6884155
gb : 6.722808E-4
*/
'JAVA' 카테고리의 다른 글
[JAVA] - 자바 등차수열, 등비수열 예제 (0) | 2024.10.02 |
---|---|
[JAVA] - try-catch와 if-else 의 차이점 (1) | 2024.09.23 |
[JAVA] - String 클래스 repeat() 메서드 (0) | 2024.08.22 |
[JAVA] - Exception을 Custom하여 사용해보기 (0) | 2024.08.04 |
[JAVA] - Stream API 기본 문법 및 활용 예제 (0) | 2024.07.22 |