Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An Easier Way to Specify Path & File Name to Java?  RSS feed

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, I need to read text files in a specific directory, then archive them.

Since Windows' "\" is an escape character, I'll need to substring into strPath and copy one char at a time to an array and whenever there is an "\", I'll need to insert a second "\" immediately after that to escape it. Then I'll have to convert the array to a string object...

That's a lot of busy work...

Is there a simpler way ?!?


 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... Windows will also use the "/", slash, character for separators in path.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not compare Strings with == operator.
AvoidTheEqualityOperator (this is a link)

Also, you need to escape backslashes only if you use them in a string literal. There is no need to escape anything when you are reading a path from a file.

Also, why are you converting booleans to strings and then compare those strings to get boolean values?

You could write: Also, canRead() returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise, so the first check is redundant.

So, you could write:which can be simplified to
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AhFai Chan wrote:Since Windows' "\" is an escape character, I'll need to substring into strPath and copy one char at a time to an array and whenever there is an "\", I'll need to insert a second "\" immediately after that to escape it. Then I'll have to convert the array to a string object...

A pile of assumptions, most of which are NOT true.

First: if you dislike '\'s (and I sympathise with you there), you can simply run replace('\', '/') on the string in question (as long as it's a path), because Java (NOT Windows AFAIK; unless things have changed) will convert forward slashes into back ones for you if it works out it's running on a Windows environment.

Second: Whenever you run into a problem, work out what the problem IS - which may not be what you see - before you decide to implement a solution.

HIH

Winston
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Well... Windows will also use the "/", slash, character for separators in path.


Yes, "/" works, users will just have to input the path and file name with "/" rather than '"\"
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Do not compare Strings with == operator.
AvoidTheEqualityOperator (this is a link)



All suggestions duly noted with thanks.
Will read up on AvoidEqualityOperator tonight.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
...
because Java (NOT Windows AFAIK; unless things have changed) will convert forward slashes into back ones for you if it works out it's running on a Windows environment.


Ahhh, OK, gotcha. Thanks.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe there is a option you can change so the path separator will be "/" and if you are the one programming it, then not a big deal. If memory serves correctly, and it rarely does, Linux and Unix also use "/" for path separators.

AhFai Chan wrote:
Les Morgan wrote:Well... Windows will also use the "/", slash, character for separators in path.


Yes, "/" works, users will just have to input the path and file name with "/" rather than '"\"
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse stores all it's readable files(NOT Java files) under the project folder. So then you have the files and the src folder in one place. To access if the file exist, you can use this



You can read the file using the FileInputStream class or RandomAccessFile
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the File class is old and clunky, and should only be used when interfacing with older code. Use Path instead.

If you want to do this in a truly "separator agnostic" way, you can use Paths.get():
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means that every time you move your file from one pc to another you have to change the path.
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That argument holds regardless of whether you use Path, File or String.

You can use Paths.get() to create relative paths.

You can also resolve() paths against each other. It's a nifty tool, much better than mucking around with path strings, and File objects.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Note that the File class is old and clunky, and should only be used when interfacing with older code. Use Path instead.

If you want to do this in a truly "separator agnostic" way, you can use Paths.get():


This is helpful.

Thanks for sharing.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Note that the File class is old and clunky, and should only be used when interfacing with older code. Use Path instead.

If you want to do this in a truly "separator agnostic" way, you can use Paths.get():


I was looking up the Oracle documentation for Path and its methods only to find that there is also a Paths object and the method get(string1, string2,... etc)
is a Paths method.

When the documentation says "Path is an interface", does this mean that "interface" is an object?

Can you please explain the difference between Path and Paths in simple terms ?
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the Java™ Tutorials.
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AhFai Chan wrote:Can you please explain the difference between Path and Paths in simple terms ?

Path represents an actual path on a file system. It's the type we're interested in. It's an interface, because it may have different implementations across different file systems.

Paths is just a utility or helper class, that makes it easy for us to get instances of Path.

This is actually a common thing: The Java designers will create a type for us to use, and then make a utility class of which the name is the plural form. Another example is Collection and Collections.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
AhFai Chan wrote:Can you please explain the difference between Path and Paths in simple terms ?

Path represents an actual path on a file system. It's the type we're interested in. It's an interface, because it may have different implementations across different file systems.

Paths is just a utility or helper class, that makes it easy for us to get instances of Path.

This is actually a common thing: The Java designers will create a type for us to use, and then make a utility class of which the name is the plural form. Another example is Collection and Collections.


Thank you to all of you.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!