|
|
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.OutputStream; |
|
import java.io.UnsupportedEncodingException; |
|
import java.util.Scanner; |
|
|
|
public class Main4 { |
|
|
|
public static void main(String[] args) { |
|
Scanner sc = new Scanner(System.in); |
|
// 읽을 파일의 경로 |
|
String path = "C:\\ecount\\data\\첨부_02.데이터 파일.txt"; |
|
// 읽은 내용이 담겨질 스트림 (byte[]) |
|
byte[] data = null; |
|
// 읽은 내용이 변환될 문자열 |
|
String read_string = null; |
|
/** 파일 읽기 */ |
|
InputStream in = null; |
|
|
|
|
|
try { |
|
// 경로를 확인하고, 파일을 오픈함 |
|
in = new FileInputStream(path); |
|
// 읽은 내용을 담기 위한 배열은 파일의 용량만큼 사이즈를 할당한다. |
|
// in.available() --> 열려 있는 파일의 크기 |
|
data = new byte[in.available()]; |
|
// 파일 읽기 - 파라미터로 전달된 배열 안에, 파일의 내용을 담아준다. |
|
in.read(data); |
|
System.out.println("[INFO] 파일 읽기 성공 >> " + path); |
|
} catch (FileNotFoundException e) { |
|
System.out.println("[ERROR] 저장 경로를 찾을 수 없습니다. >> " + path); |
|
//e.printStackTrace(); |
|
} catch (IOException e) { |
|
System.out.println("[ERROR] 파일 읽기에 실패했습니다. >> " + path); |
|
//e.printStackTrace(); |
|
} catch (Exception e) { |
|
System.out.println("[ERROR] 알수없는 에러가 발생했습니다. >> " + path); |
|
//e.printStackTrace(); |
|
} finally { |
|
if(in != null) { |
|
try { |
|
in.close(); |
|
} catch (IOException e) { |
|
System.out.println("[ERROR] 파일 닫기 실패"); |
|
//e.printStackTrace(); |
|
} |
|
} |
|
} |
|
|
|
// data 배열에 내용이 있다면, 문자열로 변환하여 출력 |
|
if(data != null) { |
|
// 문자열로 변환시에는 저장된 인코딩으로 지정해 준다. |
|
try { |
|
read_string = new String(data, "utf-8"); |
|
String[] read_string_split = read_string.split("\n"); |
|
int first_num = sc.nextInt(); |
|
int second_num = sc.nextInt(); |
|
|
|
|
|
String[] sum_st = new String[1001]; |
|
String write_string_split =""; |
|
String write_string_result=""; |
|
|
|
for(int i=first_num-1; i<second_num; i++) { |
|
write_string_split +=read_string_split[i]+"\n"; |
|
int point=0; |
|
String value_int = read_string_split[i].substring(22, 23); |
|
|
|
if(value_int.equals("B")) { |
|
String add_point = read_string_split[i].substring(18, 19); |
|
if(add_point.equals("A")) { |
|
point=5; |
|
}else if(add_point.equals("B")) { |
|
point=15; |
|
}else { |
|
point=20; |
|
} |
|
int sum = Integer.parseInt(read_string_split[i].substring(11, 14).trim())*point; |
|
sum_st[i] = String.valueOf(sum); |
|
write_string_result += read_string_split[i]+" 결과값(단가*할인포인트): "+sum+"\r\n"; |
|
|
|
} |
|
} |
|
|
|
|
|
//list에서 첫번째 있는 값을 구함 |
|
|
|
// ------------------------------------------------------------------------------------------ |
|
String path2 = "C:\\ecount\\data\\ans4.txt"; |
|
// 파일에 저장할 내용 |
|
String division ="----------------------------------------------------------------------------------"+"\r\n"; |
|
/** 특정 인코딩 방식 적용 */ |
|
// 객체나 배열이 선언과 할당에 대한 블록이 서로 분리되어 있을 경우 |
|
// 명시적으로 빈 영역임을 알리기 위해 null로 초기화 한다. |
|
byte[] buffer = null; |
|
byte[] buffer2 = null; |
|
byte[] buffer3 = null; |
|
// 문자열을 "utf-8"로 인코딩해서 저장 => 영어, 숫자 : 1byte, 한글 : 3byte |
|
try { |
|
|
|
buffer = write_string_split.getBytes("utf-8"); |
|
buffer2 = write_string_result.getBytes("utf-8"); |
|
buffer3 = division.getBytes("utf-8"); |
|
} catch (UnsupportedEncodingException e) { |
|
System.out.println("encoding 지정 에러"); |
|
//e.printStackTrace(); |
|
} |
|
|
|
/** 파일 저장 절차 시작 */ |
|
OutputStream out = null; |
|
// 파일이 없으면 파일을 만들면서, 오픈시킴 |
|
try { |
|
out = new FileOutputStream(path2, false); |
|
out.write(buffer); |
|
out.write(buffer3); |
|
out.write(buffer2); |
|
|
|
System.out.println("[INFO] 파일 저장됨 >> " + path2); |
|
} catch (FileNotFoundException e) { |
|
System.out.println("[ERROR] 저장 경로를 찾을 수 없습니다."); |
|
//e.printStackTrace(); |
|
} catch (IOException e) { |
|
System.out.println("[ERROR] 저장에 실패했습니다."); |
|
//e.printStackTrace(); |
|
} catch (Exception e) { |
|
System.out.println("[ERROR] 알 수 없는 에러가 발생했습니다."); |
|
//e.printStackTrace(); |
|
} finally { |
|
if(out != null) { |
|
try { |
|
out.close(); |
|
} catch (IOException e) { |
|
System.out.println("파일 닫기 실패"); |
|
//e.printStackTrace(); |
|
} // 파일 스트림 닫기 |
|
} |
|
} |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
System.out.println("[ERROR] encoding 지정 에러"); |
|
//e.printStackTrace(); |
|
} catch (ArrayIndexOutOfBoundsException e) { |
|
System.out.println("[ERROR] 찾는 데이터 범위 초과"); |
|
System.out.println("잘못된 데이터 값 입력."); |
|
//e.printStackTrace(); |
|
}catch (IndexOutOfBoundsException e) { |
|
System.out.println("[ERROR] 데이터가 더 필요합니다. 데이터의 범위를 더 늘려주세요."); |
|
//e.printStackTrace(); |
|
} |
|
} |
|
} |
|
} |