ADF从txt文件读取数据并分配给变量

时间:2014-11-28 05:13:28

标签: java oracle-adf jdeveloper

我想从文本文件中提取数据并在它们可以提交到数据库之前将其分配给变量。我能够读取文本文件中的所有数据,遗憾的是我无法将它们分开并分配给某个变量。我的代码如下:

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

介意给我一些建议吗?

1 个答案:

答案 0 :(得分:2)

编辑通过分隔符

拆分该行

我假设您的输入文件包含一行中的每个条目。

在这种情况下,请使用此代码替换onUploadFile

它使用Scanner逐行阅读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);

    }
}