티스토리 뷰

반응형

✏️ 8장 실습문제 - 1번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("전화번호 입력 프로그램입니다.");

        try {
            FileWriter fileWriter = new FileWriter("c:\\temp\\phone.txt");
            while (true) {
                System.out.print("이름 전화번호 >> ");
                String string = scanner.nextLine();
                if(string.equals("그만")) {
                    System.out.println("c:\\temp\\phone.txt에 저장하였습니다.");
                    break;
                }
                fileWriter.write(string,0,string.length());
                fileWriter.write("\n");
            }
            fileWriter.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 2번

// 한 글자씩 읽어서 출력하기
package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("전화번호 입력 프로그램입니다.");

        try {
            int c;
            FileReader fileReader = new FileReader("c:\\temp\\phone.txt");
            System.out.println("c:\\temp\\phone.txt를 출력합니다.");
            while ((c=fileReader.read())!=-1) {
                System.out.print((char)c);
            }
            fileReader.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

// 한 줄씩 읽어서 출력하기
package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("전화번호 입력 프로그램입니다.");
        try {
            String str;
            BufferedReader bufferedReader = new BufferedReader(new FileReader("c:\\temp\\phone.txt"));
            System.out.println("c:\\temp\\phone.txt를 출력합니다.");
            while ((str= bufferedReader.readLine())!=null) {
                System.out.println(str);
            }
            bufferedReader.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 3번

// 한 글자씩 읽기
package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int c;
            FileReader fileReader = new FileReader("c:\\windows\\system.ini");
            while ((c= fileReader.read())!=-1) {
                c = Character.toUpperCase(c);
                System.out.println((char)c);
            }
            fileReader.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

// 한 줄씩 읽기
package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            String str;
            BufferedReader bufferedReader = new BufferedReader(new FileReader("c:\\windows\\system.ini"));
            while ((str= bufferedReader.readLine())!=null) {
                str = str.toUpperCase();
                System.out.println(str);
            }
            bufferedReader.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 4번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            String str;
            int index=1;
            BufferedReader bufferedReader = new BufferedReader(new FileReader("c:\\windows\\system.ini"));
            System.out.println("c:\\windows\\system.ini 파일을 읽어 출력합니다.");
            while ((str= bufferedReader.readLine())!=null) {
                str = str.toUpperCase();
                System.out.print("\t"+index+": ; ");
                System.out.println(str);
                index++;
            }
            bufferedReader.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 5번

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class SolveProblem {

    private static boolean compareFile(FileInputStream a, FileInputStream b) throws IOException {
        byte[] src = new byte[1024];
        byte[] dest = new byte[1024];

        int srcCnt = 0, destCnt = 0;
        while(true) {
            srcCnt = a.read(src,0,src.length);
            destCnt = b.read(dest,0,dest.length);
            if(srcCnt != destCnt)
                return false;

            if(srcCnt == -1)
                break;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        FileInputStream src = null;
        FileInputStream dest = null;

        System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우,. 파일은 프로젝트 폴더에 있어야 합니다.");
        System.out.print("첫번재 파일 이름을 입력하세요 >> ");
        String input1 = sc.nextLine();
        System.out.print("두번째 파일 이름을 입력하세요 >> ");
        String input2 = sc.nextLine();

        System.out.println(input1+"와 "+input2+"를 비교합니다.");

        try {
            src = new FileInputStream(input1);
            dest = new FileInputStream(input2);
            if(compareFile(src, dest))
                System.out.println("파일이 같습니다.");
            else
                System.out.println("파일이 다릅니다.");

            if(src != null) src.close();
            if(dest != null) dest.close();
        } catch(Exception e) {
            System.out.println("error");
        }

        sc.close();
    }
}

 

✏️ 8장 실습문제 - 6번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우,. 파일은 프로젝트 폴더에 있어야 합니다.");
            System.out.print("첫번재 파일 이름을 입력하세요 >> ");
            String input1 = scanner.nextLine();
            System.out.print("두번째 파일 이름을 입력하세요 >> ");
            String input2 = scanner.nextLine();
            BufferedWriter writer= new BufferedWriter(new FileWriter("c:\\temp\\append.txt"));
            BufferedReader fileReader = new BufferedReader(new FileReader(input1));
            BufferedReader fileReader1 = new BufferedReader(new FileReader(input2));

            String str;
            while ((str=fileReader.readLine())!=null) {
                writer.write(str);
            }
            while ((str=fileReader1.readLine())!=null) {
                writer.write(str);
            }
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 7번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        File src = new File("images/가위.jpg");
        File dest = new File("images/바위.jpg");

        try {
            System.out.println("a.jpg 를 b.jpg로 복사합니다.");
            System.out.println("10%마다 *를 출력합니다.");
            FileInputStream fileInputStream = new FileInputStream(src);
            FileOutputStream fileOutputStream = new FileOutputStream(dest);
            long f_size = (src.length()/10);
            byte[] buf = new byte[(int)f_size];
            while(true){
                int n = fileInputStream.read(buf);
                fileOutputStream.write(buf,0,n);

                if(n<buf.length)
                    break;
                System.out.print("*");
            }
            fileOutputStream.close();
            fileInputStream.close();
        }
        catch (IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 8번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        File file = new File("c:\\");
        File [] subFiles = file.listFiles();

        int max=0,index=0;
        for(int i=0;i< subFiles.length;i++){
            if(max<(int)subFiles[i].length()){
                max=(int)subFiles[i].length();
                index=i;
            }
        }
        System.out.println("가장 큰 파일은 "+subFiles[index].getPath()+(int)subFiles[index].length()+"바이트");
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 9번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count=0;

        File file = new File("c:\\temp");
        File [] subFiles = file.listFiles();

        System.out.println("c:\\temp\\디렉터리의 .txt 파일을 모두 삭제합니다 ... ");
        for(int i=0;i< subFiles.length;i++){
            String string = subFiles[i].getPath();
            int index = string.lastIndexOf(".txt");
            if(index!=-1){
                File f = new File(string);
                System.out.println(string+" 삭제");
                count++;
                f.delete();
            }
        }
        System.out.println("총 "+count+"개의 .txt 파일을 삭제하였습니다.");
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 10번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashMap<String,String> map = new HashMap<>();

        try {
            BufferedReader bf = new BufferedReader(new FileReader("c:\\temp\\phone.txt"));
            String str;
            String [] s = new String[2];

            while((str=bf.readLine())!=null){
                StringTokenizer stringTokenizer = new StringTokenizer(str," ");
                int index=0;
                while(stringTokenizer.hasMoreTokens()){
                    s[index]=stringTokenizer.nextToken();
                    index++;
                }
                map.put(s[0],s[1]);
            }


            System.out.println("총 "+map.size()+"개의 전화번호를 읽었습니다.");
            while(true){
                System.out.print("이름>> ");
                String name = scanner.next();
                if(name.equals("그만"))
                    break;

                if(map.containsKey(name)){
                    System.out.println(map.get(name));
                }
                else
                    System.out.println("찾는 이름이 없습니다.");
            }
        }
        catch(IOException e){
            System.out.println("입출력 오류");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 11번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try{
            Vector<String>v = new Vector<>();
            BufferedReader bf = new BufferedReader(new FileReader("c:\\temp\\words.txt"));
            String str;
            while((str= bf.readLine())!=null){
                v.add(str);
            }

            System.out.println("프로젝트 폴더 밑의 words.txt 파일을 읽었습니다 ...");
            while(true){
                System.out.print("단어>> ");
                String word = scanner.next();
                boolean find = false;
                for(int i=0;i<v.size();i++){
                    String s = v.get(i);
                    if(word.length()>s.length())
                        continue;
                    String sub = s.substring(0,word.length());
                    if(sub.equals(word)){
                        System.out.println(s);
                        find=true;
                    }
                }
                if(!find)
                    System.out.println("발견할 수 없음");
            }
        }
        catch(IOException e){
            System.out.println("입출력 오류");
        }

        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 12번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try{
            Vector<String>v = new Vector<>();
            BufferedReader bf = new BufferedReader(new FileReader("c:\\temp\\java.txt"));
            String str;
            while((str= bf.readLine())!=null){
                v.add(str);
            }

            System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
            while(true){
                System.out.print("검색할 단어나 문장>> ");
                String word = scanner.next();
                for(int i=0;i<v.size();i++){
                    String s = v.get(i);
                    if(s.contains(word))
                        System.out.println(i+":"+s);
                }
            }
        }
        catch(IOException e){
            System.out.println("입출력 오류");
        }

        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 13번

package ex;
import java.io.*;
import java.util.*;

public class SolveProblem {

    public static void listPrint(File file) {

        File[] subfiles = file.listFiles();

        for (int i = 0; i < subfiles.length; i++) {
            File f = subfiles[i];
            if (f.isFile())
                System.out.print("file\t");
            else if (f.isDirectory())
                System.out.print("dir\t\t");
            System.out.print(f.length() + "바이트\t");
            System.out.println(f.getName());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("***** 파일 탐색기입니다. *****");
        System.out.println("\t[c:\\]");
        String Path = "c:\\";
        File file = new File(Path);
        listPrint(file);
        System.out.println("............. 생략하였습니다 ..........");
        while (true) {
            System.out.print(">>");
            String str = scanner.next();
            if (str.equals("그만"))
                break;
            if (str.equals("..")) {
                for (int i = Path.length() - 1; i >= 0; i--) {
                    if (Path.charAt(i) == '\\') {
                        Path = Path.substring(0, i);
                        break;
                    }
                }
            } else {
                Path = Path +"\\"+ str;
            }
            System.out.println("\t[" + Path + "]");
            File f = new File(Path);
            listPrint(f);
            System.out.println("............. 생략하였습니다 ..........");
        }
        scanner.close();
    }
}

 

✏️ 8장 실습문제 - 14번

package ex;

import java.io.*;
import java.util.*;

public class SolveProblem {

    public static void listPrint(File file) {

        File[] subfiles = file.listFiles();
        if(subfiles==null)
            return;

        System.out.println("\t[" + file.getPath() + "]");

        for (int i = 0; i < subfiles.length; i++) {
            File f = subfiles[i];
            if (f.isFile()) {
                System.out.print("file\t");
            }
            else if (f.isDirectory()) {
                System.out.print("dir\t\t");
            }
            System.out.print(f.length() + "바이트\t");
            System.out.println(f.getName());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("***** 파일 탐색기입니다. *****");
        System.out.println("\t[c:\\]");
        String Path = "c:\\";
        File file = new File(Path);
        listPrint(file);
        System.out.println("............. 생략하였습니다 ..........");
        while (true) {
            System.out.print(">>");
            String str = scanner.nextLine();
            if (str.equals("그만"))
                break;
            if (str.equals("..")) {
                for (int i = Path.length() - 1; i >= 0; i--) {
                    if (Path.charAt(i) == '\\') {
                        Path = Path.substring(0, i);
                        break;
                    }
                }
            }
            else if(str.contains("mkdir")){
                StringTokenizer stringTokenizer = new StringTokenizer(str," ");
                String [] s = new String[2];
                int index=0;
                while(stringTokenizer.hasMoreTokens()){
                    s[index]=stringTokenizer.nextToken();
                    index++;
                }
                File f = new File(Path+"\\"+s[1]);
                if(f.exists()) {
                    System.out.println("이미 존재하는 파일입니다.");
                }
                else{
                    System.out.println(s[1]+" 디렉터리를 생성하였습니다.");
                    f.mkdir();
                    listPrint(new File(Path));
                }
            }
            else if(str.contains("rename")){
                StringTokenizer stringTokenizer = new StringTokenizer(str," ");
                String [] s = new String[3];
                int index=0;
                while(stringTokenizer.hasMoreTokens()){
                    s[index]=stringTokenizer.nextToken();
                    index++;
                }
               /* for(int i=0;i<s.length;i++)
                    System.out.println(s[i]);*/
                File f = new File(Path+"\\"+s[1]);
                File f2 = new File(Path+"\\"+s[2]);
                if(f.exists()){
                    System.out.println("이미 존재하는 파일입니다.");
                }
                else{
                    System.out.println(s[1]+"을 "+s[2]+"로 변경하였습니다.");
                    f.renameTo(f2);
                    listPrint(new File(Path));
                }
            }
            else {
                Path = Path +"\\"+ str;
                File f = new File(Path);
                listPrint(f);
            }
            System.out.println("............. 생략하였습니다 ..........");
        }
        scanner.close();
    }
}
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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 29 30 31
글 보관함