1

I'm exporting a SQL table into a txt using java. All works fine except a minus problem which is the accent mark on some letter. In particular I have problem with the 'è' because in my txt file is transmuted into 'è'. I don't know why, but if i try to export the record of my table that contains that field (so a single record writing) there's no issues; If i try to export the whole table (thousands of record), it gives me this problem.

I've already tried the replace method that the String class offers, but same result. I've also tried to use che escape character in unicode, but still the same problem. Any hints?

EDIT here's my code:

if(array_query.equals(versamenti_tasi)) {
                        File myOutputDir = new File("C:\\comuni\\prova\\009_001_Versamenti");
                        if(!myOutputDir.exists())
                            myOutputDir.mkdir(); //Returns a boolean if you want to check it was successful.
                        PrintStream o = new PrintStream(myOutputDir + "\\" + array_query[x].substring(26) + ".txt"); 
                        System.setOut(o);

while (rs.next()) {
    out = "";
    final_index = 0;
    for(int i =1; i <= rsmd.getColumnCount(); i++) {
        if(rs.getString(i) == null) {
           final_index += rsmd.getColumnDisplaySize(i);
           out += "" + str_valore;
           out = out.substring(0, final_index);
        }else{
           final_index += rsmd.getColumnDisplaySize(i);
           out += rs.getString(i).trim() + str_valore;
           out = out.substring(0, final_index);

        }

    }
    System.out.println(out);
}

briefily, the writing part is in the loop; str_valore is an empty string of 250 char, that's because I need to take the field's original size of my db; rs is my resultset and rsmd is the metadata resultset

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • 2
    Well, how do you export these tables to the text file? It seems that your way to export a single record somehow uses a different encoding to your way to export the entire table. – Hulk Jan 28 '19 at 09:48
  • 2
    It's either the encoding used for the database, or the encoding used for the output file. – Maurice Perry Jan 28 '19 at 09:49
  • 1
    I don't know if it related or not, https://stackoverflow.com/questions/16208517/java-%C3%A9-becomes-%C3%83-how-to-fix-it – Dang Nguyen Jan 28 '19 at 09:51
  • @Hulk I just initialize a new printstream and I set it as my default out, so i can use a simple system.out.println(). – Serphentelm Jan 28 '19 at 09:53
  • Please edit the question to show us what you are doing - in both cases, the one that works and the one that doesn't. There is a difference somewhere, but we can only spot it if we see the code. – Hulk Jan 28 '19 at 10:05
  • now i've edited my eclipse encoding preferences setting utf-8 as my text file encoding; if i use the .replace('è', 'e') will work, but of course if i can achieve on having an accent mark wuold be better – Serphentelm Jan 28 '19 at 10:17
  • "I have problem with the 'è' because in my txt file is transmuted into 'è'. I don't know why": because of incorrect character encoding. – Raedwald Jan 28 '19 at 13:27

1 Answers1

1

I do not know which Eclipse setting you've changed, but I doubt that this affects your PrintStream (it probably reads/presents files as UTF-8).

To set the encoding for your PrintStream use the appropriate constructor:

change this line

PrintStream o = new PrintStream(myOutputDir + "\\" + array_query[x].substring(26) + ".txt");

to

PrintStream o = new PrintStream(myOutputDir + "\\" + array_query[x].substring(26) + ".txt", 
                    "UTF-8");

(or whatever encoding that is configured for your DB schema.)

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • thanks, this solved my problem. I changed the settings under preferences > general > workspace - Text file encoding, but as you said, that didn't solve th problem – Serphentelm Jan 28 '19 at 13:52