String Object to Path?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: String Object to Path?

  1. #1
    Join Date
    May 2009
    Posts
    115

    String Object to Path?

    Hey Guys,

    I was wondering if its possible to convert a string object into a path?
    Thing is I'm letting the user enter the location of a directory within a jtextfield.

    I'm going to use getText() to get the user entered text. But then I want to change it to a path so that I can create a new directory in the location the user specified.

    I tried reading up on : http://openjdk.java.net/projects/nio...ile/Paths.html
    But it doesn't seem to work:

    Am I implementing it wrong? :
    Path requiredPath = Paths.get(jTextField.getSelectedText());

  2. #2
    Join Date
    May 2009
    Posts
    115

    Re: String Object to Path?

    I wouldn't even mind changing it into a file object if there is that alternative?

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: String Object to Path?

    Of course you can change it into a File object and create the file/folder if it does not exist. I personally have never used Path objects, only File objects, so I don't know how they behave. Here is an example.

  4. #4
    Join Date
    May 2009
    Posts
    115

    Re: String Object to Path?

    File sessionDir = new File ( LocationTextField.getSelectedText() , jTextField.getText());

    where the LocationTextField.getSelectedText() is nothing but a string having the location like C:\Docs\Location

    And jTextField.getText() is merely XYZ, the name of the directory.

    ... when I try to do that, it just ends up creating the directory in the same location as the project's workspace ... and not in the actual location specified by LocationTextField.

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: String Object to Path?

    I might be wrong, but you are using LocationTextField.getSelectedText() instead of getText(). If there is nothing selected then it will be an empty string, right? So the file/path will be created in the current folder.

  6. #6
    Join Date
    May 2009
    Posts
    115

    Re: String Object to Path?

    whoops sorry ... that was a typo. Thanks for spotting it!
    It works now .. .thanks!

  7. #7
    Join Date
    Oct 2017
    Posts
    2

    Re: String Object to Path?

    If you want to change it to file object as an alternative I would suggest to refer link below with an explanation and example

    Directory in java.

  8. #8
    Join Date
    Aug 2017
    Posts
    32

    Re: String Object to Path?

    Use a library like Guava or Commons / IO. They have oneliner methods.

    Guava:

    Files.toString(file, charset);
    Commons / IO:

    FileUtils.readFileToString(file, charset);
    Without such a library, I'd write a helper method, something like this:

    public String readFile(File file, Charset charset) throws IOException {
    return new String(Files.readAllBytes(file.toPath()), charset);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)