简体   繁体   English

使用文件阅读器从文件中读取文本

[英]Reading text from file using filereader

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).这是我从表中检索数据并将其写入文件,然后再次从文件中检索数据并将其显示在 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?可能是什么原因?

You might want to consider Apache commons Utilities FileUtils class.您可能需要考虑 Apache commons Utilities FileUtils类。 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. 在此处查找此方法的 javadoc。 All you will need to do is您需要做的就是

ta.setText(FileUtils.readFileToString(new File("E:\\\\winter 2019\\\\COMP 230\\\\table.txt"), StandardCharsets.UTF_8));

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. System.out.print不flush,错误已经提过: ta.setText(s)会用当前字符反复覆盖ta ,最后留下最后一个字符。 Also a single backslash is doubled once in a normal "..." string.在普通的“...”字符串中,单个反斜杠也会加倍。

But I wanted to mention the new classes Path, Paths, and Files .但我想提一下新类Path, Paths, and Files And try-with-resources that closes the files, the connection, the statement and the result set.以及关闭文件、连接、语句和结果集的 try-with-resources。 Also the reading can be done line wise.也可以逐行阅读。 Where readLine delivers the read line without newline, or null on end-of-file.其中readLine提供没有换行符的读取行,或者在文件结尾处为 null。

I have used the Unicode format UTF-8 to store the file, in order to be able to combine any script/special accents.我使用 Unicode 格式 UTF-8 来存储文件,以便能够组合任何脚本/特殊口音。

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

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

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

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