Need More JAVA Help

Discussion in 'General Off-Topic Chat' started by BloodWolfJW, Oct 8, 2011.

Oct 8, 2011

Need More JAVA Help by BloodWolfJW at 5:05 PM (459 Views / 0 Likes) 5 replies

  1. BloodWolfJW
    OP

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    Country:
    United States
    Hey temps, need more help with JAVA. Need to put user.name into a filepath

    here's the code
    Code:
    import java.net.*;
    import java.io.*;
    
    public class Download
    {
    public static void main(String[] args)
    {
    String Username;
    Username = System.getProperty("user.name");
    
    try
    {
    
    System.out.println ("Connecting to ROMBox Server...\n");
    
    URL FCEUltraX = new URL("http://www.fileden.com/files/2010/12/14/3038162/Emulators/FCE%20Ultra%20X%20v2.1.5.zip");
    FCEUltraX.openConnection();
    InputStream reader = FCEUltraX.openStream();
    
    System.out.println ("Connected! Downloading File...");
    
    FileOutputStream writer = new FileOutputStream("C:/Users/USERNAME/Downloads/FCE Ultra X v2.1.5.zip");
    byte[] buffer = new byte[153600];
    int totalBytesRead = 0;
    int bytesRead = 0;
    
    while ((bytesRead = reader.read(buffer)) > 0)
    {
    writer.write(buffer, 0, bytesRead);
    buffer = new byte[153600];
    totalBytesRead += bytesRead;
    }
    
    System.out.println ("");
    
    System.out.println ("Done! Please Enjoy Your Download File!");
    writer.close();
    reader.close();
    }
    catch (MalformedURLException e)
    {
    e.printStackTrace();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    
    }
    
    }
    
     
  2. Mazor

    Member Mazor Z80 master arch

    Joined:
    Feb 14, 2008
    Messages:
    547
    Country:
    Sweden
    Even if USERNAME would be replaced by the user's name in your code, it would lead to a path that does not exist on Windows XP and below (and of course not on any operating system besides Windows). Furthermore, you're actually probably looking for the My documents folder which on an English Vista/7 would be "C:\Users\\My Documents", but again wouldn't be the same on Windows XP or a non-English Vista/7 installation. Besides all this, the My documents folder can also be set to a custom location by the user.

    In short, you need a way to programmatically get the My documents folder directly rather than just grabbing the username and attempting to use it in a statically set path. This is a good tip which applies to very many programming scenarios: Avoid statically programmed full paths as much as possible, use programmatically retrieved dynamic paths instead and you'll get much more reliability.

    I don't write desktop applications in Java and thus don't know any way to obtain the location of My documents in the language. If I for some reason was tasked with making an application in Java to do this, I'd probably just look for a wrapper for the Windows API and call SHGetSpecialFolderPath and if I wanted the application to be cross platform I'd google for another solution (a quick search yields for instance the Swing implementation javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory()).

    So tl;dr is that you should find a way to get the path of the My documents folder programmatically.
     
  3. BloodWolfJW
    OP

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    Country:
    United States
    To be honest, I have no intention of making it available outside the U.S. quite yet. I also have no intentions of making it available on XP yet as well. For now, I'm just worried about 7 64-Bit/32-Bit English. Later is when I'll support other such things as XP.
     
  4. Mazor

    Member Mazor Z80 master arch

    Joined:
    Feb 14, 2008
    Messages:
    547
    Country:
    Sweden
    If the only problem you wanted solved is how to append strings, then the answer is that it is done by using the + operator in many languages including Java. Thus:
    Code:
    String Username;
    ...
    FileOutputStream writer = new FileOutputStream("C:/Users/" + Username + "/Downloads/FCE Ultra X v2.1.5.zip");
    
    Why you're using forward slashes instead of backward slashes in your Windows path I don't know, but if it works I won't argue against it.

    Also, I made another quick search on how to get the My documents folder directly, this time searching for how to do it in .NET since you're using that already. Turns out you can use System.getProperty("user.home").
     
  5. BloodWolfJW
    OP

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    Country:
    United States
    I already knew about "user.name" but you have helped a lot. JAVA is weird in that if I type "C:\Users\Example\Downloads\FCE Ultra X v2.1.5.zip" it thinks I'm giving it commands to show the path, and how to display it. I know, confusing, right? I flipped when I figured out it did that. I was stuck for nearly 4 days until I figured it out. One of the things I hate about JAVA.
     
  6. Mazor

    Member Mazor Z80 master arch

    Joined:
    Feb 14, 2008
    Messages:
    547
    Country:
    Sweden
    Note that my post mentioned user.home, not user.name.

    And no, the behavior you are seeing is actually not weird at all and by no means exclusive to Java. If you set the string to simply "C:\Users\Example\Downloads\FCE Ultra X v2.1.5.zip" all sorts of undefined behavior are bound to happen as Java is one of the tons of programming languages that make use of \ as an escape character. In short, when there's a backslash in a string, what it does is mark that the next character should be treated differently than it normally would. For instance, '\n' indicates that the 'n' should be treated as a newline character, '\"' indicates that the quote should be part of the string instead of the character terminating it and so on. With your current code you are simply indicating that the U in Users, E in example and so on should be treated in an alternative way (there is however no alternative way to treat these characters defined in the language), you aren't actually putting any backslashes in the actual string.

    tl;dr: What you should be doing is "C:\\Users\\Example\\Downloads\\FCE Ultra X v2.1.5.zip", always escape your backslashes this way.
     

Share This Page