0

We know if we want to address a file, we could do it to something like this:

// file_path is the path file to current position of program
String address = file_path + File.separator + "output.txt";

But I made a mistake and set file separator twice, like this:

String address = file_path + File.separator + File.separator + "output.txt";

These code are just an example and I get the file path by Java library which is cross platform and there is not any mistake. I show them like this to show my mistake more clearly.

In fact, I want to rename a text file from a_text.txt to b_text.txt but I set two file separator before these names! something like //b_text.txt not /b_text.txt

In Windows there is no error and file could read or edit, but what about in other Operation Systems like Linux, Mac and Solaris?

Since I don't have those systems, I want to know about it. Although I will solve that mistake as soon as possible but I'm curious to know about it anyway.

bdshahab
  • 189
  • 1
  • 12
  • 1
    The mere fact that you're specifying it as a windows path means that it wouldn't work meaningfully on those platforms. – Andy Turner Dec 11 '17 at 13:06
  • The code you have shown will not return any errors on any OS: it's just a string concatenation... Maybe you use that string later on and that's where an exception may be thrown, but you haven't shown the corresponding code. – assylias Dec 11 '17 at 13:06
  • Like mentioned, that path format only makes sense on Windows. However in general it fully depends where you will use that string. Many classes/libraries will de-duplicate the slash character. – Gorazd Rebolj Dec 11 '17 at 13:09
  • Maybe you can check: https://stackoverflow.com/a/412495/9041712 -- it's a better way to get file path. – Yu-Lin Chen Dec 11 '17 at 13:14
  • Your problem will not be with the double separator but the `D:` which doesn't exists on Unix-like systems. – Jean-Baptiste Yunès Dec 11 '17 at 13:14
  • I will use that String as a file address and later I use it in my program but I only put the origin part of problem that maybe cause some problem in some OS. – bdshahab Dec 11 '17 at 13:21
  • @Jean-BaptisteYunès my program could detect path automatically but it's obvious I'm not familiar with Unix and other OSs! – bdshahab Dec 11 '17 at 13:22
  • @GorazdRebolj I use it for renaming a text file. In Windows that file will remain without any problem but in other OS, I don't know! – bdshahab Dec 11 '17 at 13:23
  • @bdshahab Depending on *how* you use that string, you may or may not have issues. Are you creating a `new File(...)`? or a `Paths.get(...)`? or something else? Without more information it's impossible to answer your question. – assylias Dec 11 '17 at 13:36
  • @assylias OK! I get the file path correctly and there is not any mistake, but I want to rename a text file from a_text.txt to b_text.txt but I set two file separator before these names! something like //b_text.txt not /b_text.txt – bdshahab Dec 11 '17 at 14:17

1 Answers1

0

As @Yu-LinChen stated, use Path if you're using Java 7 or higher. Everything else will be platform dependent.

Moreover, as the D: suggests you're also using an absolute path, so it will be even more restricted to a certain environment. Consider making the path configurable, e.g. in a properties file, and read the configuration using Path.get(pathFromProperty). This will work with both D:\some\path on Windows and with /some/path on Unix-like systems.

gucce
  • 597
  • 4
  • 12
  • That was an example, I used a similar way to get the path and everything is fine, but when I want to rename a file, I add two file separator! That's the problem, although that's OK in Windows but I'm not sure about other OS. – bdshahab Dec 11 '17 at 13:35
  • The question is, why don't you use `Path`when renaming the file? `Path` never produces duplicate separators. I think I haven't understood your question clearly. – gucce Dec 12 '17 at 14:25
  • I used Path, but I add another file separator after that! I made a mistake but in Windows it's OK to have suchthings: file_path + "//" + file_name.txt – bdshahab Dec 12 '17 at 17:17
  • If you add file separators manually to a `Path` you're not using `Path` correctly. I suggest you have a look at the tutorial provided by Oracle (or any other tutorial on `Path`): https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html – gucce Dec 14 '17 at 14:08
  • I get path of program location once and then I manually set the file path for sub folders in that location. I add double file separator in one of my statements which is fine in Windows and file in that location could be renamed but I don't know if it's fine in other operation systems. That's my question! – bdshahab Dec 14 '17 at 19:42