23-2: JAVA read&write

jk·2024년 1월 31일
0

kdt 풀스택

목록 보기
44/127



1. 문자스트림에 대하여 설명하시오.

  • Character stream can write and read characters directly.



2. 앞서 프로그래밍한 phone.txt 파일을 읽어 화면에 출력 하시오.

//
//code
//
import java.io.*;
import java.util.*;
class Const {
    static final String FILE_NAME = "phone.txt";
    static final int TOKENS_COUNT = 2;
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printlnAndReset() {
        System.out.println(print);
        print.setLength(0);
    }
    private static void printAndReset() {
        System.out.print(print);
        print.setLength(0);
    }
    static <T> void println(T t) {
        print.append(t);
        printlnAndReset();
    }
    static void println() {
        printlnAndReset();
    }
    static <T> void print(T t) {
        print.append(t);
        printAndReset();
    }
    static void print() {
        printAndReset();
    }
}
class Functions {
    private static Map<String, String> map = new HashMap<>();
    static void run() {
        while(true) {
            try {
                readTelephoneDirectory();
                printTelephoneDirectory();
                break;
            } catch(Exception e) {
                e.printStackTrace();
            };
        };
    }
    private static void readTelephoneDirectory() {
        try(BufferedReader bufferedReader 
            = new BufferedReader(new FileReader(Const.FILE_NAME))) {
            while(true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                };
                StringTokenizer stringTokenizer = new StringTokenizer(line, "\s");
                if (stringTokenizer.countTokens() >= Const.TOKENS_COUNT) {
                    String name = stringTokenizer.nextToken();
                    String phone = stringTokenizer.nextToken();
                    try {
                        Integer.valueOf(phone);
                        map.put(phone, name);
                    } catch(NumberFormatException e) {
                    };
                };
            };
        } catch(IOException e) {
            e.printStackTrace();
        };
    }
    private static void printTelephoneDirectory() {
        Set<String> set = map.keySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            Object phone = iterator.next();
            String name = map.get(phone);
            Print.print(name);
            Print.print("\s");
            Print.print(phone);
            Print.print("\n");
        };
    }
}
class ReadTelephoneDirectoryMain {
    public static void main(String[] args) {
        Functions.run();
    }
}
/*print
jk 01012345678
hhoop 01023456789
*/



3.system.ini 파일을 하나 만들고, 소문자 모두를 대문자로 바꾸어 출력하라.

//
//code
//
import java.io.*;
class Const {
    static final String FILE_NAME = "system.ini";
    static final String FILE_TEXT = "I know I am clumsy.\nSo I should be more careful to live in safer way.";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printAndReset() {
        System.out.print(print);
        justReset();
    }
    static <T> void print(T t) {
        justAppend(t);
        printAndReset();
    }
    static <T> void print() {
        printAndReset();
    }
    static <T> void resetAndPrint(T t) {
        justReset();
        justAppend(t);
        printAndReset();
    }
    static <T> void justAppend(T t) {
        print.append(t);
    }
    static void justReset() {
        print.setLength(0);
    }
    static StringBuilder getPrint() {
        return print;
    }
}
class Functions {
    static void run() {
        while(true) {
            try {
                writeText();
                readTextWithUpperCase();
                break;
            } catch(Exception e) {
                e.printStackTrace();
            };
        };
    }
    private static void writeText() {
        try (BufferedWriter bufferedWriter 
            = new BufferedWriter(new FileWriter(Const.FILE_NAME))) 
        {
            bufferedWriter.write(Const.FILE_TEXT);
        } catch(IOException e) {
            e.printStackTrace();
        };
    }
    private static void readTextWithUpperCase() {
        try (BufferedReader bufferedReader
            = new BufferedReader(new FileReader(Const.FILE_NAME)))
        {
            while (bufferedReader.ready()) {
                Print.justAppend(bufferedReader.readLine());
                Print.justAppend("\n");
            };
            Print.resetAndPrint(Print.getPrint().toString().toUpperCase());
        } catch(IOException e) {
            e.printStackTrace();
        };
    }
}
class TextToUpperCaseMain {
    public static void main(String[] args) {
        Functions.run();
    }
}
/*print
I KNOW I AM CLUMSY.
SO I SHOULD BE MORE CAREFUL TO LIVE IN SAFER WAY.
*/



4.system.ini 파일에 라인 번호를 붙여 출력하라.

//
//code
//
import java.io.*;
class Const {
    static final String PATH = "";
    static final String FILE_NAME = "system.ini";
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printAndReset() {
        System.out.print(print);
        justReset();
    }
    static <T> void print(T t) {
        justAppend(t);
        printAndReset();
    }
    static <T> void print() {
        printAndReset();
    }
    static <T> void resetAndPrint(T t) {
        justReset();
        justAppend(t);
        printAndReset();
    }
    static <T> void justAppend(T t) {
        print.append(t);
    }
    static void justReset() {
        print.setLength(0);
    }
    static StringBuilder getPrint() {
        return print;
    }
}
class Functions {
    static void run() {
        while(true) {
            try {
                readTextWithLineNumber();
                break;
            } catch(Exception e) {
                e.printStackTrace();
            };
        };
    }
    private static void readTextWithLineNumber() {
        try (BufferedReader bufferedReader
            = new BufferedReader(new FileReader(fileAddress(Const.FILE_NAME))))
        {
            int num = 0;
            while (bufferedReader.ready()) {
                Print.justAppend(++num);
                Print.justAppend("\t");
                Print.justAppend(bufferedReader.readLine());
                Print.justAppend("\n");
            };
            Print.print();
        } catch(IOException e) {
            e.printStackTrace();
        };
    }
    private static String fileAddress(String name, String path) {
        return(path.concat(name));
    }
    private static String fileAddress(String name) {
        return(Const.PATH.concat(name));
    }
}
class ReadTextWithLineNumberMain {
    public static void main(String[] args) {
        Functions.run();
    }
}
/*print
1       I know I am clumsy.
2       So I should be more careful to live in safer way.
*/
profile
Brave but clumsy

0개의 댓글