0

This code should get the absolute path, append a string from a preferences file and then append ".json" to match the necessary file. I tried using "+" to concatenate strings, but it was giving the same output as the StringBuilder.append()

    StringBuilder pt= new StringBuilder(path);
    pt.append(System.getProperty("file.separator"));
    pt.append("lib");
    pt.append(System.getProperty("file.separator"));
    pt.append("ling");
    pt.append(System.getProperty("file.separator"));
    String lingua =PrefManager.getPref("lingua")+("=");
    System.out.println(lingua);
    pt.append(lingua);
    System.out.println("com extensão"+pt.toString());
    String file = pt.toString();
    System.out.println(file);
    System.out.println(file);
    Object obj = parser.parse(new FileReader(file));

This is my console output:

=t-br
=om extensão/home/mateus/BrinoBuildScript/Filesx64/lib/ling/pt-br
=home/mateus/BrinoBuildScript/Filesx64/lib/ling/pt-br
=home/mateus/BrinoBuildScript/Filesx64/lib/ling/pt-br
java.io.FileNotFoundException: /home/mateus/BrinoBuildScript/Filesx64/lib/ling/p= (No such file or directory)

How can a variable have three different outputs to console? what should I do to fix this?

Mateus Terra
  • 159
  • 1
  • 9
  • Why three different outputs? Which output do you expect? – IQV Feb 08 '17 at 12:32
  • The outputs are the same, apart from the "com extensão" text you preprend in the first output of that variable... – john16384 Feb 08 '17 at 12:32
  • Also you never append .json or what did you expect? – Xander Feb 08 '17 at 12:34
  • I don't get the output you have, `com extensão` is printed like `=om extensão` (like every line) then, in `lingua`, the last character (`=`) dissapear. But for the rest, seems ok for me (from the code I read) – AxelH Feb 08 '17 at 12:35
  • 1
    Mateus, if you allow me, avoid using files and folders with portuguese language special chars like **ão**, **í**, **ç** and so forth. Same way with files and folders with their names containing blank spaces... this is only useful to make troubles for us, devs. Please, try this and say what happens. Yet: don't use these chars in var names, function names, classes and methods too. Regards from Brazil. – statosdotcom Feb 08 '17 at 12:49
  • @statosdotcom, I don't use them. The "com extensão" is part of a log only. – Mateus Terra Feb 15 '17 at 19:48
  • @Xander, I expected /pt-br= (the = was a replacement for .json i was using to test the code) – Mateus Terra Feb 15 '17 at 19:50

1 Answers1

0

Mateus. Your console output is printing the last character of the line on the first column. Rendered correctly, your console output should look like this:

pt-br=
com extensão\home\mateus\BrinoBuildScript\Filesx64\lib\ling\pt-br=
\home\mateus\BrinoBuildScript\Filesx64\lib\ling\pt-br=
\home\mateus\BrinoBuildScript\Filesx64\lib\ling\pt-br=

In this output, you can see that String file is correctly set (although you probably don't intend to have a trailing '=').

While you are being careful to use the system path separator, the concatenation is a bit clumsy. If you are using an old version of Java, ou may try to compose your path using the File class:

final String path = "\\home\\mateus\\BrinoBuildScript\\Filesx64";
final File libFolder = new File(path, "lib");
final File lingFolder = new File(libFolder, "ling");
final File languageFolder = new File(lingFolder, PrefManager.getPref("lingua"));
System.out.println(languageFolder.getAbsolutePath());

If you are using a recent version of Java, you may use the Paths API (which does handle platform-specific path separators):

final Path p = Paths.get(path, "lib", "ling", PrefManager.getPref("lingua"));
System.out.println(p);

See the Java tutorial on the Paths API here:

https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

  • thanks, will take a look on the paths API. I managed to do the concatenation of the .json with string.format("%s.%s") – Mateus Terra Feb 15 '17 at 19:45