Menu ▾ ▴

Cannot read a datafile with .txt extension

Help
2015-01-15
2015-01-18
  • Andriy Samson

    Andriy Samson - 2015-01-15

    Hi,

    Recently I have a pleasure to use a CsvJdbc driver. I use a BomReader to deal with BOM characters. It works fine, but after I start to use the BomReader, it seems that the property fileExtension is ignored. With BomReader the driver can read ONLY the files with .csv extension.
    For example, when I change the extension to .txt, I got the error message:
    java.sql.SQLException: C:\test\datafile.csv (The system cannot find the file specified)

    Here is a test case that shows the problem. Before running the test, create a folder c:/temp/ and put in a datafile.csv data file, but change the extension to .txt. The driver cannot read the file.

    public static void main(String[] args) {
        try {
            Class.forName("org.relique.jdbc.csv.CsvDriver");
            Properties props = new Properties();
            props.put("fileExtension", ".txt");
            props.put("separator", ";");
            props.put("charset", "UTF-8");
    
            CsvConnection conn = (CsvConnection) DriverManager.getConnection("jdbc:relique:csv:class:" + BomReader.class.getName(), props);
            Statement stmt = conn.createStatement();
            // suppose to have folder c:\test\  with CSV file   "datafile.txt"
            ResultSet results = stmt.executeQuery("SELECT * FROM  C:\\test\\datafile");
            ResultSetMetaData meta = results.getMetaData();
            while (results.next()) {
                for (int i = 0; i < meta.getColumnCount(); i++) {
                    System.out.println(meta.getColumnName(i + 1) + " " + results.getString(i + 1));
                }
            }
            conn.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    How can I fix this problem to be able to read files with other than .csv extension?

    Thanks.

     
  • Simon Chenery

    Simon Chenery - 2015-01-15

    Your BomReader class is opening and reading the CSV file directly.

    The name of the table is passed to the BomReader.getReader(Statement, String) method. In your example, the table name is C:\test\datafile This method is opening the file and reading and returning the contents.

    You need to change class BomReader to open the file with a different suffix.

    The CsvJdbc fileExtension property is not used in this situation.

    See the TableReader example in http://csvjdbc.sourceforge.net/doc.html for an example.

     
  • Andriy Samson

    Andriy Samson - 2015-01-18

    You are right, I completely forget about it.
    Thanks a lot.

     

Log in to post a comment.