오보에블로그
자바실습 Week10 - FILE IO 본문
Problem 1
1. Write a program that inputs alphabetic characters ‘a’ to ‘z’ in a single line in a text file.
2. The text file must exist in the project folder.
3. Text file name is “alpha.txt”.
4. The text file must exist in the project folder.
5. If “alpha.txt” does not exist in your project folder, your program should automatically generate a text file named “alpha.txt”.
Problem 1 result
Problem 1 code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.*; import java.io.*; public class MakingAlpha { public static void main(String[] args) { File f = new File("/Users/obo/Documents/java/ProblemWeek10/alpha.txt");//다음경로에 alpha.txt 생성. >>이것을 f라고 한다. int c; try { FileWriter fr = new FileWriter(f);//f파일에 텍스트를 쓰는 출력스트림 fr 생성. fr.write("abcdefghijklmnopqrstuvwxyz");//"abcdefghijklmnopqrstuvwxyz"를 파일에기록. fr.close();//스트림을 닫는다. 더이상 스트림에 기록할 수 없다. }catch(IOException e) { System.out.println("error"); } //catch-try를 통해 오류가 발생했을때(파일경로명이틀리거나 어떤상황으로 파일을 열수 없을경우) catch(){...}를 통해 예외처리할 수 있다. } } | cs |
Problem 2
1. Create “source.txt” in the project folder.
2. Enter the following text in your “source.txt”.
-> Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.
3. Write a program that automatically creates two text files and puts above two sentences into each text file.(Sentences are separated by ‘.’)
4. The two text file names are “part1.txt” and “part2.txt”
You must use the “source.txt” you created here in the next two problems.
Problem 2 result
Problem 2 code
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 32 33 34 35 | import java.util.*; import java.io.*; public class Problem2 { public static void main(String[] args) { File f = new File("./part1.txt"); File f2 = new File("./part2.txt");////다음경로에 part1.txt,part2.txt 생성.('./'은 현재폴더에 다음 텍스트 생) >>이것을 f,f2라고 한다. int c; try { FileReader sourcefr = new FileReader("./source.txt");//source.txt파일에 텍스트를 읽는 스트림 sourcefr 생성. FileWriter ffw = new FileWriter(f);//f파일에 텍스트를 쓰는 출력스트림 ffw 생성. FileWriter f2fw = new FileWriter(f2);//f2파일에 텍스트를 쓰는 출력스트림 f2fw 생성. while((c = sourcefr.read()) !='.' ) {//sourcefr을 통해 읽은 c가 .이 아닐때까지 f파일에 기록. ffw.write(c); } ffw.write("."); ffw.close(); while((c = sourcefr.read()) != -1) {//sourcefr을 통해 읽은 c가 EOF가 아닐때까지 f2파일에 기록. f2fw.write(c); } f2fw.close(); sourcefr.close(); }catch(IOException e) { e.printStackTrace(); System.out.println("error"); } } } | cs |
Problem 3
2. Print the numbers of alphabet characters in alphabetical order.
Problem 3 result
Problem 3 code
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 32 33 34 35 36 | import java.util.*; import java.io.*; public class AlphaberNumber { public static void main(String[] args) { File f = new File("./part1.txt"); File f2 = new File("./part2.txt"); int intArray[] = new int[27]; int c; try { FileReader sourcefr = new FileReader("./source.txt"); while((c = sourcefr.read()) != -1 ) { if(!(65<=c && c<= 90 || 97<=c && c<=122))//대문자 알파벳이거나 소문자 알파벳이면 알맞은 배열 위치에 +1. continue; else if(c < 97) c += 32; intArray[c-97]++; } sourcefr.close(); for(int k :intArray) { System.out.println(k); } }catch(IOException e) { e.printStackTrace(); System.out.println("error"); } } } | cs |
Problem 4
3. Enter the words from “source.txt” into “wordlist.txt” one line at a time.(words are separated by ” “.)
Problem 4 result
Problem 4 code
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 32 33 | import java.util.*; import java.io.*; public class MakingWordList { public static void main(String[] args) { File f = new File("./wordlist.txt"); int c; try { FileReader sourcefr = new FileReader("./source.txt"); FileWriter fw = new FileWriter(f); while((c = sourcefr.read()) != -1 ) { if(c == ' ') { fw.write("\n");//' '(빈칸)이면 '\n'기록. continue; } else if((65<=c && c<= 90 || 97<=c && c<=122))//대문자 알파벳 또는 소문자 알파벳이면 파일에 기록.(, ,> ,} , -, 와 같은 문자들은 파일에 기록하지 않기위해 법위 구체화) fw.write(c); } sourcefr.close(); fw.close(); }catch(IOException e) { e.printStackTrace(); System.out.println("error"); } } } | cs |
'STEADYSTUDY > Etc' 카테고리의 다른 글
[Python3] 프로그램 축소기 (0) | 2019.07.08 |
---|---|
2447_별찍기 - 10 (0) | 2018.02.06 |
Collection & Generics (0) | 2017.11.16 |
추상클래스 (abstract class) 사용 (0) | 2017.11.08 |
클래스 상속과 객체 (0) | 2017.11.01 |