자바스터디 4

1,232 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,232
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

자바스터디 4

  1. 1. Separator<br />
  2. 2. file객체 생성<br />
  3. 3. file클래스의 method들<br />listRoots()<br />list ()<br />…<br />
  4. 4. file클래스의 method들<br />isDirectory(), isFile(), isHidden(), lastModified(), length()<br />
  5. 5. file클래스의 method들<br />getName(), getParent(), getPath()<br />exists(), getAbsolutePath(), canRead(), canWrite<br />
  6. 6. file클래스의 method들<br />createNewFile()<br />처음 실행시는 true값 출력!<br />createTempFile( , , ),deleteOnExit() <br />try {<br />File f2 = File.createTempFile("temp", ".lih", ff);<br />//접두어, 확장자 ->파일이름은 접두어에 따라 자동으로생성<br />System.out.println("f2 = " + f2);<br />f2.deleteOnExit();//프로그램 끝날때 파일 삭제<br />}catch(IOException e) {}<br />f1.deleteOnExit();<br />//System.out.println("bool = " + bool);<br />try {<br />Thread.sleep(3000);<br />}catch(InterruptedException e) {}<br />
  7. 7. file클래스의 method들<br />mkdir(), mkdirs(), setLastModified(),setReadOnly()<br />import java.io.*;<br />import java.util.*;<br />public class Exam_04 {<br />public static void main(String[] ar) {<br />File f = new File("aaa");<br />f.mkdir();<br />File ff = new File("bbb/ccc/ddd/eee");<br />ff.mkdirs();<br />f.setLastModified(new Date().getTime());<br />//파일의 마지막 수정시간을 바꾸어 줌<br />f.setReadOnly(); //읽기전용으로 만듦<br />}<br />}<br />
  8. 8. 1byte 입/출력<br />import java.io.*;<br />public class Exam_05 {<br />public static void main(String[] ar) throws IOException {<br />FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out);<br />//console에 대한 출력<br />File f = new File("aaa.txt");<br />FileOutputStream fos2 = new FileOutputStream(f, true);<br />//true 값은 파일에 기존 데이터에 추가를 시켜줌<br />byte[] by = new byte[]{'H', 'e', 'l', 'l', 'o', ' ', 'J', 'a', 'v', 'a‘,’ ‘};<br />//System.out.println("Hello Java");<br />fos1.write(by, 6, 4);<br />//6번재 위치에서부터 4개까지 찍음 (Java만 찍음)<br />fos2.write(by);<br />fos1.write(65);<br />//아스키코드값에 따른 문자를 출력 (A)<br />fos1.close();<br />fos2.close();<br />}<br />}<br />
  9. 9. 1byte 입/출력<br />import java.io.*;<br />public class Exam_06 {<br />public static void main(String[] ar) throws IOException {<br />FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out);<br />BufferedOutputStream bos1 = new BufferedOutputStream(fos1, 2048);//1024, 512<br />//로컬일때 2048byte버퍼가 효율적<br />DataOutputStream dos1 = new DataOutputStream(bos1);<br />//console에 대한 출력<br />FileOutputStream fos2 = new FileOutputStream(new File("bbb.txt"), false);<br />BufferedOutputStream bos2 = new BufferedOutputStream(fos2, 1024);<br />//파일일때 1024byte가 적정 <br />DataOutputStream dos2 = new DataOutputStream(bos2);<br />//파일에 대한 출력<br />dos1.writeInt(20);dos1.writeDouble(12.34);<br />dos1.flush();<br />dos1.writeChar('A');dos1.writeByte(' ');dos1.writeByte('B');<br />dos1.close();<br />dos2.writeInt(20);dos2.writeDouble(12.34);dos2.flush();<br />dos2.writeChar('A');dos2.writeByte(' ');dos2.writeByte('B');<br />dos2.close();<br />}<br />}<br />
  10. 10. 1byte 입/출력<br />
  11. 11. 1byte 입/출력<br />
  12. 12. 2byte 입/출력<br />import java.io.*;<br />public class Exam_09 {<br />public static void main(String[] ar) throws IOException {<br />//System.out.println("test 안녕하세요...");<br />//한글이 들어가는 2byte입력시 <br />//FileOutputStream fos = new FileOutputStream(FileDescriptor.out);<br />OutputStreamWriter osw = new OutputStreamWriter(System.out);<br />//2byte Text출력시 <br />BufferedWriter bw = new BufferedWriter(osw, 1024);<br />PrintWriter pw = new PrintWriter(bw);<br />File f = new File("ccc.txt");<br />FileWriter fw = new FileWriter(f);<br />BufferedWriter bw1 = new BufferedWriter(fw, 1024);<br />PrintWriter pw1 = new PrintWriter(bw1);<br />pw.println(10);//숫자도 출력가능 <br />pw.println("test 안녕하세요!");<br />pw1.println(10);<br />pw1.println("test 안녕하세요!");<br />pw.close();<br />pw1.close();<br />}<br />}<br />
  13. 13. 2byte 입/출력<br />
  14. 14. Scanner클래스<br />import java.io.*;<br />import java.util.*;<br />public class Exam_11 {<br />public static void main(String[] ar) throws IOException {<br />Scanner in = new Scanner(System.in); <br />System.out.print("문자열 = ");<br />String str = in.next();<br />System.out.print("숫자 = ");<br />int x = in.nextInt();<br />System.out.print("더블 숫자 = ");<br />double y = in.nextDouble();<br />System.out.println("str = " + str);<br />System.out.println("x = " + x);<br />System.out.println("y = " + y);<br />String input = "1 fish 2 fish red fish blue fish";<br /> Scanner s = new Scanner(input);<br /> s.useDelimiter(“ fish ");<br /> System.out.println(s.nextInt());<br /> System.out.println(s.nextInt());<br /> System.out.println(s.next());<br /> System.out.println(s.next());<br /> s.close(); <br />}<br />}<br />
  15. 15. Scanner클래스<br />import java.io.*;<br />import java.util.*;<br />public class Exam_12 {<br />public static void main(String[] ar) throws IOException {<br />if(ar.length < 1) System.exit(0);<br />String str = "";<br />for(int i = 0; i < ar.length; ++i) {<br />str += ar[i] + " ";<br />}<br />Scanner in = new Scanner(str);<br />int i = 0;<br />int tot = 0;<br />while(in.hasNextInt()) {<br />i++;<br />tot += in.nextInt();<br />}<br />System.out.println("총 개수 = "+ i + "개");<br />System.out.println("총합 = "+ tot + "점");<br />System.out.println("평균 = "+ tot / i + "점");<br />}<br />}<br />

×