Your SlideShare is downloading. ×
자바스터디 4
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

자바스터디 4

891
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
891
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Separator
  • 2. file객체 생성
  • 3. file클래스의 method들
    listRoots()
    list ()

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

×