2014-11-28 5 views
0

데이터베이스에 커밋하기 전에 텍스트 파일에서 데이터를 추출하여 변수에 할당하고 싶습니다. 불행히도 나는 그들을 분리하고 특정 변수에 할당하지 못했습니다 텍스트 파일 안의 모든 데이터를 읽을 수 있습니다. 내 코드는 아래와 같습니다 :ADF txt 파일에서 데이터 읽기 및 변수에 할당

private RichInputFile inputFile; 
private UploadedFile file; 
private String fileContent; 
private String fileName; 
private InputStream inputstream; 

public void onFileUploadValueChangeListener(ValueChangeEvent valueChangeEvent) { 
    resetValue(); 
    file = (UploadedFile)valueChangeEvent.getNewValue(); 
    try { 

     inputstream = file.getInputStream(); 
     System.out.println("inputstream Name: " +inputstream); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void onUploadFile(ActionEvent actionEvent) { 

    if (file != null && inputstream != null) { 

     fileName = file.getFilename(); 
     System.out.println("Notepad Name: " +fileName); 
     StringWriter writer = new StringWriter(); 

     try { 

      IOUtils.copy(inputstream, writer); 
      fileContent = writer.toString(); 
      System.out.println("File Content: " +fileContent); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("Notepad Content: " +writer); 
    } 

    if (inputFile != null) { 

     inputFile.resetValue(); 
     inputFile.setValid(true); 

    } 
} 

public void resetValue() { 

    if (fileName != null) 
     fileName = null; 

    if (fileContent != null) 
     fileContent = null; 

    if (inputstream != null) 
     inputstream = null; 
} 

public void setInputFile(RichInputFile inputFile) { 
    this.inputFile = inputFile; 

} 

public RichInputFile getInputFile() { 
    return inputFile; 
} 

public void setFile(UploadedFile file) { 
    this.file = file; 
} 

public UploadedFile getFile() { 
    return file; 
} 

public String getFileContent() { 
    return fileContent; 
} 

public String getFileName() { 
    return fileName; 
} 

의견을 보내주십시오.

+0

정확하게 잘못 되었나요? – User404

+0

테스트 파일의 데이터 : 16/05/2014 17:19:00 6685 00:00:31 내가 얻은 결과는 16/05/2014 17:19:00 6685 00:00:31 같은 줄에. 나는 그들을 적절하게 분리하고 싶다 .xz ~ – goh6319

+0

은 두 줄 또는 하나의 파일에있는 데이터인가? – MihaiC

답변

2

편집 분할 분리

나는 당신의 입력 파일을 믿고있어 통한 라인은 한 줄에 각 항목이 포함되어 있습니다.

이 경우 onUploadFile을이 코드로 바꿉니다.

을 사용하여 라인 단위로 inputstream을 읽습니다. 각 줄마다 원하는 것을하십시오.

public void onUploadFile(ActionEvent actionEvent) { 

    if (file != null && inputstream != null) { 
     fileName = file.getFilename(); 
     System.out.println("Notepad Name: " + fileName); 

     final Scanner scanner = new Scanner(inputstream); 
     while (scanner.hasNextLine()) { 
      String line = scanner.nextLine(); 
      //do somthing with the current line, like store each one in a list 
      System.out.println(line); 
      String[] lineElements=line.split(" "); 
      //16/05/2014 17:19:00 6685 00:00:31 
      //in your case lineElements[0] should be a date 
      //lineElements[1] is a timestamp linked to the before date 
      //lineElements[2] is a number (int) 
      //lineElements[3] is a timestamp 
      //build string from elements [0] and [1] then transform it to a date 
      //add logic for adding to database... 
     } 
    } 

    if (inputFile != null) { 

     inputFile.resetValue(); 
     inputFile.setValid(true); 

    } 
} 
+0

안녕하세요 귀하의 코드가 잘 작동합니다. 하지만 난 단지 당신이 차동 데이터 형식으로 갈 궁금해? 예를 들어 메모장에서 읽은 데이터는 날짜, 문자열 및 숫자로 구성됩니다. 어떻게 차등화하고 테이블에 저장할 수 있습니까? – goh6319

+0

이전에 줄의 각 요소를 테스트하지 않으면 잘 할 수 없습니다. 일반적으로이 경우 테이블의 구조와 파일 구조를 알고 있으므로 미리 일치시켜야합니다. 대안은 이전에 Integer.parseInt(), toString(), 날짜 변환 등을 사용하여 라인의 각 요소를 테스트하는 것입니다. 귀하의 경우에 – MihaiC

+0

당신은 "공백"문자로 줄을 나눈 다음 각 요소를 테스트해야하지만이 방법은 권장하지 않습니다. 파일에 항상 날짜, 정수, 문자열 (예 :)이 순서대로 포함되어 있어야합니다. 그래서 split [0]과 같은 원소를 얻으면 그 원소가 날짜라는 것을 알게됩니다. String [] split = line.split ("");로 줄을 나눕니다. – MihaiC

관련 문제