Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将值写入文本文件并将值存储为inputStream_Java_File_Text_File Io_Inputstream - Fatal编程技术网

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

Java 如何将值写入文本文件并将值存储为inputStream,java,file,text,file-io,inputstream,Java,File,Text,File Io,Inputstream,我需要您的帮助,将值写入文本文件,并将写入的数据转换为inputStream,以便将其附加到按钮。对于硬编码的字符串,我可以这样做,请参考以下方法: private StreamedContent txtFile; public void createTxt(ActionEvent actionEvent) { try { String string = "This is a String.\nWe are going to convert it to

我需要您的帮助,将值写入文本文件,并将写入的数据转换为inputStream,以便将其附加到按钮。对于硬编码的字符串,我可以这样做,请参考以下方法:

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

}
现在,我需要从数据库中读取数据,因此我已完成以下操作:

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

}
因此,现在我需要您的帮助,在读取每条记录后,从数据库中写入由“|”分隔的获取值,并在其中添加一行新行,然后将完整值转换为
inputStream

,答案如下:

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

}

您到底有什么问题?@KPrince36我需要将内容字符串写入文本文件,但为了存储,我需要将其转换为InputStream。请看教程。我现在需要将JDBC教程链接到您吗?例如,将从数据库读取的数据放入
StringBuilder
中。请阅读教程,我不相信你自己已经写了一半的代码。