-1
수정 된 CSV 파일이있는 서비스를 만들었습니다. 각 행의 마지막 열에 값을 추가하지만 파일을 덮어 쓸 때 문제점을 발견했습니다. 새 파일이 이전 파일과 다른 이름을 가진다면 성공하지만 동일한 이름의 파일을 원합니다. 이 문제를 해결하는 방법?파일 csv가 수정되었습니다.
public void modify() {
BufferedReader br=null;
BufferedWriter bw=null;
try {
File file = new File("C:\\Users\\c76266\\Documents\\bobby\\text\\sample2.csv");
File newfile = new File("C:\\Users\\c76266\\Documents\\bobby\\text\\sample2_new.csv");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) ;
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newfile)));
String line = null;
String [] addedColumn = {"newstring1", "newstring2"};
int i=0;
while((line = br.readLine())!=null){
String result = "\t"+addedColumn[i];
bw.write(line+result+System.lineSeparator());
i++;
}
} catch(Exception e){
System.out.println(e);
} finally {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
이 코드에서는 파일과 새 파일이 다른 이름이므로 잘 작동합니다. sample2.csv
및 sample2_new.csv
그러나 새 파일도 원하지만 sample2.csv
이지만 실패했습니다. 파일을 열면 빈 문자열. 감사.
파일을 저장 내용과 다음 SAMPLE2하는 문자열을 다시 쓰기 .csv – Anirudh
표준 파일 작업을 사용하여 동시에 읽기 및 쓰기 용 파일을 열 수 없습니다. 프로그래밍 언어는 텍스트 편집기가 아닙니다. 쓰기 위해 열면 곧 잘립니다. 대신 임시 파일을 사용하십시오. 기본 파일 조작에 대해 읽어보십시오. – Gnudiff
예외가 있습니까? 두 줄 이상인 경우 ** ArrayindexOutofBoundsexception **을 얻을 수 있습니다. – iwaduarte