Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jashan","noor1032");
PreparedStatement stmt=con.prepareStatement("select STUDENT_ID,STU_NAME,GENDER from STUDENT");
ResultSet rs=stmt.executeQuery();
FileWriter fw=new FileWriter("E:\\winter 2019\\COMP 230\\table.txt");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
fw.write(newline+rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
fw.close();
FileReader f1=new FileReader("E:\\\\winter 2019\\\\COMP 230\\\\table.txt");
BufferedReader br=new BufferedReader(f1);
int j;
String s;
while((j=br.read())!=-1)
{
char ch=(char)j;
s=new String (String.valueOf(ch));
System.out.print(s);
ta.setText(s);
}
f1.close();
这是我的代码,用于从表中检索数据并将其写入文件中,然后再次从文件中检索数据并将其显示在文本区域(ta)中。文件中的数据打印正常,但它只显示文本区域内的最后一个字符,而不是显示整个数据。可能是什么原因?
最佳答案
您可能需要考虑 Apache commons Utilities FileUtils
类。它有一个方法
public static String readFileToString(File file,
Charset encoding)
throws IOException
这确实应该可以解决您的所有问题。看here对于这个方法的javadoc。 您需要做的就是
ta.setText(FileUtils.readFileToString(new File("E:\\\\winter 2019\\\\COMP 230\\\\table.txt"), StandardCharsets.UTF_8));
关于java - 使用 filereader 从文件中读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144912/