2017-02-15 8 views
0

JSON 응답을 받아들이고 데이터를 Excel 및 CSV 파일로 내보내는 메서드를 만들었습니다. 메서드가 파일 이름을 매개 변수로 받아 들여 File/FileHandle 개체를 메서드에서 반환하도록합니다.자바 메서드에서 파일 반환

아무에게도 도움이 될 수 있습니까?

나는 creteExcelcreateCSV에서 데이터를 내보낼 코드 아래 사용하고 있습니다. 상기 방법에서 createGrid

public void createExcel(PageModel pageModelRequest,PageModel pageModelResponse){ 
     FileOutputStream outputStream=new FileOutputStream(new File("GridData.xlsx")); 
     createGrid(pageModelResponse, sheet, workbook); 
     workbook.write(outputStream); 
     System.out.println("Excel Written");  
     outputStream.close(); 
     workbook.close(); 
} 

public void createCSV(PageModel pageModelRequest,PageModel pageModelResponse){ 
     String csvFile = "/Users/abhinak4/Desktop/GridData.csv";   
     FileWriter writer = new FileWriter(csvFile); 
     createGrid(pageModelRequest, pageModelResponse, writer); 
} 

, I는 엑셀 통합 문서를 생성하고, 및 I는 writer (writer.append)에 데이터를 추가하고있다.

알고 싶습니다. createExcelcreateCSV 메서드에 대한 매개 변수로 fileName을 어떻게 받아 들일 수 있으며 데이터가 들어있는 동일한 이름의 파일을 반환합니까?

+2

메소드의 서명 (반환 유형 및 매개 변수)을 변경할 수 없습니까? – anacron

+0

[가능한 것이 더 좋습니다 - 파일 이름을 사용하는 메소드의 매개 변수 유형으로 String 또는 File 사용] (http://stackoverflow.com/questions/17142469/which-is-better-using-string-or-file-as) -parameter-type-for-methods-that-take-f) – liorsolomon

+0

** Excel의 경우 ** fileName을 String 매개 변수로 보낼 수 있지만 FileOutputStream을 사용하여 데이터를 씁니다. 그래서 FileOutputStream을이 메소드 바로부터 돌려 주거나 File 객체로 변환해야 할 수 있습니까? 변환해야한다면, 어떻게해야합니까? ** CSV **의 경우 : fileWriter를 통해 데이터를 쓰고 있습니다. fileWriter를 직접 반환 할 수 있습니까, 아니면 파일 객체로 변환해야합니까? 그렇다면 어떻게 변환합니까? –

답변

1

나는 방법은 매개 변수로 파일 이름을 수락 할 : InputStream 당신은 String을 보낼 수하는 File 객체 또는 - createExcel(PageModel req, PageModel resp, File yourFileObj)

을 그리고 방법에서 File 개체를 반환 :을 서명이된다 : public File createExcel()

+0

** Excel **의 경우 : fileName을 String 매개 변수로 보내려고하지만 FileOutputStream을 사용하여 데이터를 씁니다. 그래서 FileOutputStream을이 메소드 바로부터 돌려 주거나 File 객체로 변환해야 할 수 있습니까? 변환해야한다면, 어떻게해야합니까? ** CSV **의 경우 : fileWriter를 통해 데이터를 쓰고 있습니다. fileWriter를 직접 반환 할 수 있습니까, 아니면 파일 객체로 변환해야합니까? 그렇다면 어떻게 변환합니까? –