Reputation: 21
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();
This is my code to retrieve data from a table and write it into a file and then again retrieve data from the file and display it in a textarea(ta). The data printing in file is ok but it is only displaying the last character inside the textarea rather than displaying whole data. What could be the reason?
Upvotes: 1
Views: 73
Reputation: 3018
Try this code... you overwrote the value in the textfield everytime instead of concatenating the string and then writing it to the textfield.
String s;
StringBuilder text = new StringBuilder();
while((j=br.read())!=-1)
{
char ch=(char)j;
s=new String (String.valueOf(ch));
System.out.print(s);
text.append(s);
}
ta.setText(text.toString());
f1.close();
Upvotes: 0
Reputation: 7792
You might want to consider Apache commons Utilities FileUtils
class. It has a method
public static String readFileToString(File file,
Charset encoding)
throws IOException
This really should solve all your problems. Look here for javadoc of this method. All you will need to do is
ta.setText(FileUtils.readFileToString(new File("E:\\\\winter 2019\\\\COMP 230\\\\table.txt"), StandardCharsets.UTF_8));
Upvotes: 1
Reputation: 109547
System.out.print does not flush, and the error is already mentioned: ta.setText(s)
will be repeatedly overwrite ta
with the current character, finally leaving the last character. Also a single backslash is doubled once in a normal "..." string.
But I wanted to mention the new classes Path, Paths, and Files
. And try-with-resources that closes the files, the connection, the statement and the result set. Also the reading can be done line wise. Where readLine
delivers the read line without newline, or null on end-of-file.
I have used the Unicode format UTF-8 to store the file, in order to be able to combine any script/special accents.
Path path = Paths.get("E:\\winter 2019\\COMP 230\\table.txt");
Class.forName("oracle.jdbc.driver.OracleDriver");
try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"...", "...");
PreparedStatement stmt = con.prepareStatement(
"select STUDENT_ID,STU_NAME,GENDER from STUDENT");
ResultSet rs = stmt.executeQuery();
PrintWriter pw = new PrintWriter(Files.newBufferedWriter(path))) {
pw.print('\uFEFF'); // Maybe write Unicode BOM for Windows Notepad
while (rs.next()) {
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
pw.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}
try (BufferedReader br = Files.newBufferedReader(path)) {
String s;
while((s = br.readLine())!= null) {}
System.out.println(s);
ta.setText(ta.getText() + s + "\r\n");
}
}
Upvotes: 0