Search code examples
javautf-8jcifs

JCIFS - SMBFileOutputStream as UTF8


How do i write file as UTF8? i already set the system property but not working.

Below is the sample code.

SmbFileOutputStream sfos = null;
        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(wipDomain,wipUsername,wipPassword);
            System.setProperty("jcifs.encoding", "UTF8");
            logger.info("Path: " +path);
            SmbFile sFile = new SmbFile(path, auth);
            sfos = new SmbFileOutputStream(sFile);
            sfos.write(content.getBytes());
            return true;
        } catch (IOException e) {

            logger.error(e.getMessage());
            return false;
        } finally {
            if (sfos != null){
                try {
                    sfos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

Solution

  • content.getBytes("UTF-8")
    

    Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.