Skip to main content

Solved : DM_DEFAULT_ENCODING: Reliance on default encoding


DM_DEFAULT_ENCODING: Reliance on default encoding

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

Lest assume you are reading a csv file using opencsv :

CSVReader reader = new CSVReader(new FileReader(csvFilePath), ',', '"', 0)

you will get this error as the file encoding is not mentioned and this can lead to data error while reading foreign text i.e unicode characters .

To solve this challenge use input stream reader and provide encoding as below :

CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(csvFilePath), "UTF-8"), ',', '"', 0) .

Hope this helps




Comments

Popular posts from this blog

Read excel contents as in the original file using java

I always struggle to read a string in excel which has a big number 999999999999 but when I read it using apache POI and print it I see the output is 1E+12, so after a bit of google I got a solution which worked for me if (cell.getCellType() == Cell. CELL_TYPE_NUMERIC ) { System.out.print( new BigDecimal(cell.getNumericCellValue()).toPlainString() ); } This approach works but when we require to read a number with leading zeros or an integer as an integer this approach is not efficient and hence the best approach to read an excel data as it is in the original sheet just convert the cell type to string before reading the data . cell=(XSSFCell) cells.next() ; cell.setCellType(Cell. CELL_TYPE_STRING ) ; System.out.print( cell.getRichStringCellValue().toString().trim()); Hope this will help you .

Remove leading repeated occurrence from a string

I used to struggle while working with data across different systems as few systems append zeros to the actual value ex. if the value for a unique id is "345683" the system will give "000345683" but when we need to use this value in to enter data in other system we just need the "345683" and as always regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string). String myDesiredString = inputString.replaceFirst("^0+(?!$)", "") Just a background for first time regex users . ^ anchor will make sure that the 0+ being matched is at the beginning of the input  The (?!$) negative lookahead ensures that not the entire string will be matched Replace the zero with desired character or series of characters to remove the repeated occurrence at the start public static void mai...