Jacoco
Jacoco

Reputation: 3

Java: Use URL as Path object

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.

Upvotes: 0

Views: 706

Answers (2)

Jacoco
Jacoco

Reputation: 3

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.

Upvotes: 0

Wintermute
Wintermute

Reputation: 61

URL url=getClass().getResource("https://websiteYouTryToDownloadTheFileFrom/yourfile.txt"); 
File yourFile=new File(url.toURI());

Upvotes: 1

Related Questions