简体   繁体   English

如何将值写入文本文件并将值存储为 inputStream

[英]How to write values to text file and to store the values as inputStream

I need your help in writing the values to a text file and to convert the written data to inputStream in order to attach it to a button.我需要您帮助将值写入文本文件并将写入的数据转换为 inputStream 以便将其附加到按钮。 I was able to do that for a String whihc is hard-coded, refer to the below method:我能够对硬编码的字符串执行此操作,请参阅以下方法:

private StreamedContent txtFile;
        public void createTxt(ActionEvent actionEvent) {

    try {
        String string = "This is a String.\nWe are going to convert it to InputStream.\n";
        InputStream inputStream = new ByteArrayInputStream(string.getBytes(Charset.forName("UTF-8")));
        txtFile = new DefaultStreamedContent(inputStream, "application/txt", "sample.txt");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Now, i need to read the data from the database, so I have done the following:现在,我需要从数据库中读取数据,所以我做了以下工作:

private StreamedContent txtFile;
        public void createTxt(ActionEvent actionEvent) {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement();

        String sql = "SELECT id, name, amount FROM Employee";
        ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                int id  = rs.getInt("id");
                int age = rs.getString("name");
                String first = rs.getInt("amount");
               } 
            rs.close();
        InputStream inputStream = new ByteArrayInputStream(string.getBytes(Charset.forName("UTF-8")));
        txtFile = new DefaultStreamedContent(inputStream, "application/txt", "sample.txt");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

So now I need your help to write the fetched values from the database separated by "|"所以现在我需要你的帮助来写从数据库中提取的值,用“|”分隔and a new line after reading each record and to convert the full to inputStream .并在读取每条记录后换行并将完整记录转换为inputStream

Here is the answer:这是答案:

private StreamedContent txtFile;
        public void createTxt(ActionEvent actionEvent) {

try {
    Class.forName("com.mysql.jdbc.Driver");
    StringBuilder builder = new StringBuilder();
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();

    String sql = "SELECT id, name, amount FROM Employee";
    ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()){
            int id  = rs.getInt("id");
            int age = rs.getString("name");
            String first = rs.getInt("amount");
            builder.append(id+"|"+age+"|"+first+"\n");
           } 
        rs.close();
        String result = builder.toString();
    InputStream inputStream = new ByteArrayInputStream(result.getBytes(Charset.forName("UTF-8")));
    txtFile = new DefaultStreamedContent(inputStream, "application/txt", "sample.txt");
} catch (Exception e) {
    e.printStackTrace();
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM