Loading...

JAVA / / 2022. 11. 24. 14:30

FileOutputStream 파일쓰기

반응형
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.junit.Test;

public class FileTest {

	@Test
	public void test() {		
		try {
			FileOutputStream out = new FileOutputStream("C:/study/test.txt");
			
			String strTextString = "저장해주세요 제발";
			out.write(strTextString.getBytes());
            
			out.close();
            
			System.out.println("저장됐겠지..");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

내용이 바뀌면 똑같은 파일이 덮어쓰기된다.

내용이 똑같아도 이름 바뀌서 또 생성되지 않는다.

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Test;

import giosis.web.core.search.model.ModelRows;

public class FileTest2 {

	@Test
	public void test() {		
		try {
			FileOutputStream out = new FileOutputStream("C:/study/test.xls"); // 파일 경로 수정
			
//			String strTextString = "내용이 변경되었어요";
//			out.write(strTextString.getBytes());
			
			HSSFWorkbook wb = new HSSFWorkbook(); //엑셀파일 생성
			HSSFSheet sheet = wb.createSheet("result"); //시트 생성
			
			Row row = null; //행 객체 생성
			Cell cell = null; //열 객체 생성
			
			//첫 행 입력
			row = sheet.createRow(0);
			
			cell = row.createCell(0);
			cell.setCellValue("GD_NO");
			cell = row.createCell(1);
			cell.setCellValue("GD_NM");
			cell = row.createCell(2);		
			cell.setCellValue("NICKNAME");
			cell = row.createCell(3);	
			cell.setCellValue("MGR_NM");
	
			int line = 1; //row번째 행
			
			ArrayList<ModelRows> resultList = new ArrayList<>();
			
			HashMap<String, String> result1 = new HashMap<>();
			result1.put("GD_NO", "1");
			result1.put("GD_NM", "1");
			result1.put("NICKNAME", "1");
			result1.put("MGR_NM", "1");
			
			HashMap<String, String> result2 = new HashMap<>();
			result2.put("GD_NO", "2");
			result2.put("GD_NM", "2");
			result2.put("NICKNAME", "2");
			result2.put("MGR_NM", "2");
			
			HashMap<String, String> result3 = new HashMap<>();
			result3.put("GD_NO", "3");
			result3.put("GD_NM", "3");
			result3.put("NICKNAME", "3");
			result3.put("MGR_NM", "3");
			
			resultList.add(new ModelRows(result1, "Y"));
			resultList.add(new ModelRows(result2, "Y"));
			resultList.add(new ModelRows(result3, "Y"));
			
			for(int i = 0; i < resultList.size(); i++) {
				
				// quary 결과에서 데이터 가져오기
				String gdNo = resultList.get(i).getGdNo();
				String gdNm = resultList.get(i).getGdNm();
				String nickName = resultList.get(i).getNickName();
				String mgrNm = resultList.get(i).getMgrNm();
				
				// 데이터 입력
				row = sheet.createRow(line);
				cell = row.createCell(0);
				cell.setCellValue(gdNo);
				cell = row.createCell(1);
				cell.setCellValue(gdNm);
				cell = row.createCell(2);		
				cell.setCellValue(nickName);
				cell = row.createCell(3);	
				cell.setCellValue(mgrNm);
				
				line++;		
			}
			
			wb.write(out);
			out.close();
			System.out.println("저장됐겠지");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

 

해결할 점

1. 서버 로컬이 아니라 클라이언트한테 파일이 저장되어야 한다.

2. 경로를 지정할 수 있어야한다.

 

 

반응형