Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 코틀린
- nodejs
- 자바스크립트
- TextView
- 스프링부트
- GoogleMaps
- 랜덤번호
- SpringBoot
- RecyclerView
- 안드로이드
- Hook
- JavaScript
- 랜덤넘버
- stylesheet
- 리액트
- Linux
- scrollview
- Kotlin
- Javscript
- button
- React
- Android
- Java
- JS
- TypeScript
- npm
- 구글맵스
- 오버라이딩
- fragment
- array
Archives
- Today
- Total
타닥타닥 개발자의 일상
자바 java File로 파일 생성하기 / 폴더 생성 / 파일 리스트 확인 / 읽기 전용 변환 / 읽기 가능 여부 확인하기 본문
코딩 기록/Java
자바 java File로 파일 생성하기 / 폴더 생성 / 파일 리스트 확인 / 읽기 전용 변환 / 읽기 가능 여부 확인하기
NomadHaven 2021. 12. 13. 23:45
1.파일 조사하기
입력 | 출력 |
File file = new File("c:\\"); //file은 c:에 있는 파일 //파일 조사 String filelist[] = file.list(); for (int i= 0; i< filelist.length; i++) { System.out.println(filelist[i]); } |
!@EqBCx $Recycle.Bin $WinREAgent .rnd Documents and Settings DumpStack.log.tmp eclipse hiberfil.sys Java Kings myfile OneDriveTemp pagefile.sys PerfLogs Program Files Program Files (x86) ProgramData Recovery swapfile.sys System Volume Information Temp Users Windows //C:에 있는 파일들 |
2.파일과 폴더 구분하여 파일 조사하기
입력 | 출력 |
File file = new File("c:\\"); //file은 c:에 있는 파일 File filelist[] =file.listFiles(); //c:에 있는 파일목록을 배열형 파일자료로 for (int i = 0; i < filelist.length; i++) { if(filelist[i].isFile()) { System.out.println("[파일]"+filelist[i].getName()); } //.isFile이란 boolean 이용해서 파일인지 아닌지 확인 else if(filelist[i].isDirectory()) { System.out.println("[폴더]"+filelist[i].getName()); //.isDirectory란 boolean 이용해서 파일인지 아닌지 확인 } } |
[폴더]!@EqBCx [폴더]$Recycle.Bin [폴더]$WinREAgent [파일].rnd [폴더]Documents and Settings [파일]DumpStack.log.tmp [폴더]eclipse [파일]hiberfil.sys [폴더]Java [폴더]Kings [폴더]myfile [폴더]OneDriveTemp [파일]pagefile.sys [폴더]PerfLogs [폴더]Program Files [폴더]Program Files (x86) [폴더]ProgramData [폴더]Recovery [파일]swapfile.sys [폴더]System Volume Information [폴더]Temp [폴더]Users [폴더]Windows |
3.파일 생성하기
입력 |
File newFile = new File("c:\\myfile\\newfile.text"); //c에 있는 myfile폴더에 newfile 텍스트 파일 만들기 try { if(newFile.createNewFile()) { System.out.println("파일 생성 성공"); }else { System.out.println("파일 생성 실패"); } }catch(IOException e) { System.out.println("파일 생성하지 못했습니다."); } |
4.폴더 생성하기
입력 |
File newDir = new File("c:\\myfile\\myimage"); //c에 있는 my file에 my image라는 폴더 생성 if(newDir.mkdir()) { System.out.println("폴더 생성 성공"); }else { System.out.println("폴더 생성 실패"); } |
5. file 존재 여부 확인
입력 |
File newFile = new File("c:\\myfile\\newfile.text"); //c에 있는 myfile폴더에 newfile 텍스트 파일 만들기 if(newFile.exists()){ //newFile이 있는지 확인 System.out.println("newfile.text가 존재합니다."); }else { System.out.println("newfile.text가 존재합니다."); } |
6.읽기 전용으로 만들기
입력 |
File newFile = new File("c:\\myfile\\newfile.text"); newFile.setReadOnly(); //읽기 전용으로만 만들어져서 내용 수정이 불가능한 파일된다. |
7.쓰기 가능한 파일인지 확인
입력 |
File newFile = new File("c:\\myfile\\newfile.text"); if(newFile.canWrite()) { System.out.println("쓰기 가능합니다"); }else { System.out.println("쓰기 불가능합니다."); } |
8.파일 삭제
입력 |
File newFile = new File("c:\\myfile\\newfile.text"); newFile.delete(); |
'코딩 기록 > Java' 카테고리의 다른 글
Java FileWriter, BufferedWriter, PrintWriter 로 파일 생성해서 내용 입력, 입력된 내용 다시 배열로 출력하기 (0) | 2021.12.14 |
---|---|
자바java FileWriter BufferedWriter 이용해서 파일에 글쓰기 /PrintWriter로 쓰여진 문장 출력하기 (0) | 2021.12.14 |
java로 2차원 배열을 1차원으로 변경해서 리턴하는 함수 작성하기 (0) | 2021.12.12 |
java로 입력된 숫자가 정수인지 실수인지 확인 가능한 함수 작성하기 (0) | 2021.12.12 |
java로 두점의 거리 구하는 함수 만들고 출력하기 (0) | 2021.12.12 |
Comments