0

I'm using a URL to import a font. But since not in all cases will internet be present nor do I want to download a file everytime I run the program I want to use the same method of using a url but instead point it to a font in the same location as the program.

Here is what I tried:

File fontfile = new File("TexasLED.ttf");
File.toURI(fontfile).toURL(fontfile);
URL fontUrl = new URL("fontfile");

Though I'm getting this error:

Error: F:\Computer Science\draw.java:250: toURI() in java.io.File cannot be applied to (java.io.File)

I know I'm doing something wrong. Does anyone know what and how I could fix it?

V2:

File fontfile = new File("TexasLED.ttf"); fontfile.toURI().toURL(); URL fontUrl = new URL("fontfile");

Error:

java.net.MalformedURLException: no protocol: fontfile
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

3 Answers3

1
File fontfile = new File("TexasLED.ttf");
// sanity check!
System.out.println("fontFile.exists(): " + fontFile.exists());
URL fontURL = fontFile.toURI().toURL();
System.out.println("fontURL: " + fontURL);

E.G.

import java.io.File;
import java.net.URL;

class WhatIsMyURI {

    public static void main(String[] args) throws Exception {
        File file = new File("WhatIsMyURI.java");
        // sanity check!
        System.out.println("file.exists(): " + file.exists());
        URL url = file.toURI().toURL();
        System.out.println("url: " + url);
    }
}

Output

file.exists(): true
url: file:/I:/projects/numbered/all/744/WhatIsMyURI.java
Press any key to continue . . .
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Okay it's still the same V2 error you see when I use this. It says that the file does exist. Here is the path it is giving: `fontURL: file:/F:/Computer%20Science/TexasLED.ttf` – ComputerLocus Dec 04 '11 at 04:16
  • *"V2 error"* 1) Is that a 'blow up on launch' type of error, or just 'missed London by 3 degrees' error? Or to put that another way, what DYM? 2) So given the `File` is not at the listed path, *what path is it located at?* It's your PC & font, don't expect me to know. – Andrew Thompson Dec 04 '11 at 04:21
  • the only thing I could think is wrong is the / before the F. Other then that it is all right. – ComputerLocus Dec 04 '11 at 04:26
  • *"the only thing I could think is wrong is the / before the F"* Did you not notice the '/' before the 'I' in my URL? That is just how URLs of file systems work. – Andrew Thompson Dec 04 '11 at 04:31
  • Well I don't see why what I am doing is not working. It is the right file and everything. In case this helps this is a little more of the code [here](http://pastebin.com/MR52QSNv) – ComputerLocus Dec 04 '11 at 04:35
  • Open the CLI (command prompt). Navigate to the directory in which the font is located. Type 'dir' for a directory listing. Right click on the CLI to and select Mark from the menu. Drag the mouse across the directory listing and hit Enter to copy the details the clipboard. Paste the clipboard content as an edit in your question. Let me know you've done it - I want to see the listing with my own eyes. – Andrew Thompson Dec 04 '11 at 04:42
  • [Here is a picture](http://localhostr.com/files/gpoee76/TexasLED.ttf%20Properties.png) – ComputerLocus Dec 04 '11 at 05:02
  • `F:/Computer_Science` Uses an **underscore** to separate `Computer` & `Science`. The URI you posted suggests that the `File` that was used is `F:/Computer Science` (I.E. a space character was incorrectly used when forming the directory name). – Andrew Thompson Dec 04 '11 at 05:09
  • No I renamed the directory to Computer_Science to see if that was the issue. I tried Computer Science as well but both gave same error. – ComputerLocus Dec 04 '11 at 05:21
0

You can just download the file once and do the following:

File f = new File("TexasLED.ttf");
if(f.exists()) {
    System.out.println("Already exists on computer");
}
else {
    URL fontURL = f.toURI().toURL();
    //Proceed to download font
}

This checks to see if the font exists on your computer, and only if it doesn't does it try to download the new one. This is really the only safe way of doing this that I have seen.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • The thing is it downloads to a temp location. Thus meaning it will be deleted after a little. – ComputerLocus Dec 04 '11 at 04:38
  • How are you downloading the file? Try this for copying the file permanently: [link]http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java – Jon Egeland Dec 04 '11 at 05:25
  • Still I'd prefer to not even have to download it. This is for a school thing and the site could be blocked so I don't want to take chances. I want to use a specific font already in the folder. – ComputerLocus Dec 04 '11 at 05:31
  • Well, if you can't always connect to the internet, but you always want to use the font, you have to download it at least once and save it somewhere. Or just download it arbitrarily and use it as a plain `File` object. – Jon Egeland Dec 04 '11 at 06:02
0

I have figured out how to do it myself. I dragged the file into my web browser and I got this for the url:

file:///F:/Computer_Science/TexasLED.ttf"

So I simply used that URL in it and it worked:

URL fontUrl = new URL("file:///F:/Computer_Science/TexasLED.ttf");

Instead I will probably check the current location and use that + file at the beginning.

Thanks for trying to help anyways!

ComputerLocus
  • 3,448
  • 10
  • 47
  • 96