Accents in a file name using Java in Solaris

I have a problem where I cannot write accent files in a file name in Solaris.

Given the following code

public static void main(String[] args) {
    System.out.println("Charset = "+ Charset.defaultCharset().toString());
    System.out.println("testéörtkuoë");
    FileWriter fw = null;
    try {
        fw  = new FileWriter("testéörtkuoë");
        fw.write("testéörtkuoëéörtkuoë");
        fw.close();

I get the following output

Charset = ISO-8859-1
test??rtkuo?

and I get a file called "test ?? rtkuo?"

Based on the information I found on StackOverflow, I tried to invoke a Java application by adding "-Dfile.encoding = UTF-8" at startup. This returns the following result

Charset = UTF-8
testéörtkuoë

But the file name is still "test ?? rtkuo?"

Any help is greatly appreciated.

Stef

+3
source share
5 answers

ISO-8859-1. , , , .

ISO-8859-1, , "":

eb e9 f6

UTF-8, , "":

c3ab c3a9 c3b6

.

, Unicode escape-. , , , .

:

ë    \u00EB
é    \u00E9
ö    \u00F6

, Unicode.

-Dfile.encoding = UTF-8 , JVM .

, Windows.

- OpenSolaris UTF-8 .

+4

java io apis, ? ? , , .

+1

, :

ls > testéörtkuoë

( ), , .

0

. , sysout.println, , ls .

, file.encoding , JVM

:

  • .
  • ls
  • File.list()
  • environment file.encoding
  • the environment user.(language|country)

:

LC_* ​​ , /etc/defaut/init, set, .

$ set | grep LC
LC_ALL=pt_BR.ISO8859-1
LC_COLLATE=pt_BR.ISO8859-1
LC_CTYPE=pt_BR.ISO8859-1
LC_MESSAGES=C
LC_MONETARY=pt_BR.ISO8859-1
LC_NUMERIC=pt_BR.ISO8859-1
LC_TIME=pt_BR.ISO8859-1

$ locale
LANG=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_ALL=

The solution was a simple LANG export. This environment variable really affects jvm

LANG=pt_BR.ISO8859-1
export LANG
0
source

Java uses the default encoding for the operating system when reading and writing files. Now you should never rely on this. It is always useful to specify the encoding correctly.

In Java, you can use the following to read and write:

Reading:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath),"UTF-8"));

Record:

PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8")));
0
source

Source: https://habr.com/ru/post/1706828/


All Articles