열심히 끝까지
[코딩 문제] - 6/27 강사님 문제 본문
강사님 코딩문제
다른 정수, 글자들도 넣어보고 한번 console에 출력해보기
---------내 방법
package class03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test021 {
public static void main(String[] args) {
File file = new File("D:\\0607RYO\\resource\\test.txt");
try {
FileInputStream fis = new FileInputStream(file);
int data;
while((data = fis.read()) != -1) {
System.out.print((char)data);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
System.out.println("정리 완료");
}
}
}
-------강사님 방법
package class03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test022 {
public static void main(String[] args) {
File file = new File("D:\\0607RYO\\resource\\test.txt");
// 해당 파일이 경로에 존재하지 않으면 새로 만들어줌!
// -> FNE을 줄이는데에 도움이 된다!
try {
file.createNewFile();
}catch(IOException e) {
e.printStackTrace();
}
finally {
System.out.println("로그 : 파일 객체 생성 완료");
}
String msg=""; // scope 올려주기
try {
FileInputStream fis = new FileInputStream(file);
// 해당 파일이 경로에 존재하지 않으면 예외가 발생한다!
int data;
while((data=fis.read()) != -1) { // 아스키코드로 읽어온다!
System.out.println((char)data);
msg+=(char)data;
}
} catch (Exception e) { // 파일이 없으면 에러가 뜬다.
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
System.out.println(msg);
int res = Integer.parseInt(msg);
// 문자열 -> 정수로 바꾸어 주는 것
// >> Integer.parseInt
res++;
System.out.println(res);
}
}
1234가 입력된 파일을 읽어와서 각 자리의 모든 수를 합한 것을 test2에 출력
------------내 방법
package class03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test031 {
public static void main(String[] args) {
final String path1 = "D:\\0607RYO\\resource\\";
final String path2 = ".txt";
String msg = "";
try {
FileInputStream fis = new FileInputStream(path1 + "test" + path2);
int data;
while((data=fis.read()) != -1) {
System.out.println((char)data);
msg += (char)data;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(msg);
int res = Integer.parseInt(msg);
int sum = 0;
while(res%10 != 0) { // 10으로 나눈 나머지 값 저장
sum += res%10;
res /= 10;
}
System.out.println("data의 숫자 하나씩 더한 값 : "+sum);
try {
FileOutputStream fos = new FileOutputStream(path1 + "test2" + path2);
// 왜 안되는거지...
fos.write(sum);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
---------------------------------강사님 방법 다시 해보면서 익힘
------------강사님 방법
package class03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test04 {
public static void main(String[] args) {
// 파일 받아오기 위한 밑작업(반복작업)
final String path1 = "D:\\0607RYO\\resource\\";
final String path2 = ".txt";
int sum = 0;
try {
FileInputStream fis = new FileInputStream(path1 + "test" + path2);
int data;
while((data=fis.read())!= -1) {
System.out.println(data);
char ch = (char)data;
String str = "";
str += ch;
System.out.println(ch);
int num = Integer.parseInt(str);
sum += num; // 아스키코드값 빼주기
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("sum = " + sum);
try {
FileOutputStream fos = new FileOutputStream(path1+"res"+path2);
fos.write(null); // "sum = "+sum
// 자바 파일입출력 문자열
// FileReader, FileWriter
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
'디바이스 융합 자바(Java)기반 풀스택 개발자 양성과정(과제)' 카테고리의 다른 글
[코딩 과제] - 6/29 MVC 자판기 프로그램 + 관리자 프로그램 (0) | 2022.06.29 |
---|---|
[코딩 예시] - 6/28 MVC 자판기 프로그램 + 관리자 추가 과제 (0) | 2022.06.28 |
[능력단위평가] - 6/27 문제 답안 (0) | 2022.06.27 |
[코딩 문제] - 6/24 강사님 문제 (0) | 2022.06.24 |
[코딩 문제] - 6/23 강사님 문제 (0) | 2022.06.23 |