如何将值写入文本文件并将值存储为 inputStream
How to write values to text file and to store the values as 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.\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();
}
}
我需要你的帮助将值写入文本文件并将写入的数据转换为 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();
}
}