Search code examples
javaoracle-adfjdeveloper

ADF Read Data From txt File And Assign to Variable


I would like to extract data from text file and assign it into variable before they can commit to database. I able to read all the data inside the text file, unfortunately i fail to separate them and assign to certain variable. My code as below:

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;
}

Mind to provide me some suggestion?


Solution

  • EDIT Split the line via separator

    I'm assuming your input file contains each entry on one line.

    In that case, replace onUploadFile with this code.

    It uses Scanner to read the inputstream line by line. Do what you want with each line.

    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);
    
        }
    }