0

I am using some code from a different program. I don't fully understand how it works, but I know how I can use it. The program uses a text file from your pc, but the text file I want to use is not a file I have downloaded. It's 'an online file' as a link that starts with https://, that I want to use. For the program to work, it needs the file to be a Path object, but I can't figure out, how to make the URL into a Path, without the program stopping and giving an error. I have tried using the [url].toURI(). Didn't work. I have tried different methods of making the URL, but I couldn't find a method, that works.

It would make most sense for the program to not download the file, but it is a possibility, if it's not possible to make the other thing work (I already tried, and I also had problems with that. Most methods I could find didn't work, because some lines got mixed, which makes it not work with the program).

Edit: As I expected, it's not possible to do, so I need to find out how I can download the text file, as the exact same file. The lines needs to stay the same, something can't be moved to a different line.

asked May 20, 2020 at 16:04
2
  • 1
    You can't convert a URL into a Path, because a Path is a representation of an object on a locally mounted filesystem. Commented May 20, 2020 at 16:06
  • I would use something like Files.copy(url.openStream(), Files.createTempFile()); but you will need to close (and maybe buffer) the stream and save the Path object to a variable. Commented May 20, 2020 at 20:35

2 Answers 2

1
URL url=getClass().getResource("https://websiteYouTryToDownloadTheFileFrom/yourfile.txt"); 
File yourFile=new File(url.toURI());
answered May 20, 2020 at 20:32

Comments

0

I found out how to do it:

First of, as I said in the edit, it's not possible to use URL directly as a path. As a workaround, I made a temporary file, which this website helped me with.

After that, I needed to print the text file from the URL to the temporary file. I used the NIO method from here.

I hope this can help, if others have the same problem.

answered May 21, 2020 at 15:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.