Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
  1. #21
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Posted too soon, it didn't create the .txt file

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    it didn't create the .txt file
    How/where does your code create a file?
    You did not post any code that would create a file.

  3. #23
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Java Code:
    try{
    // Create file 
    FileWriter writer;
        
          writer = new FileWriter( "C:\\Home_Directories\\" + gradYear + "\\" + username + ("pass.txt"));
          writer.write(password);
          writer.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
    
    }
    }
    }
    This creates the txt file, but it puts it in C:\\Home_Directories\\" + gradYear then calls the .txt file: 'username'pass.txt..

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    Sorry, I'm having a hard time understanding what you want to do and what the code you posted is supposed to do.
    Does the posted code compile and execute AND do what you want it to do?
    To see what the path you are creating is:
    On line 4 create a String that is the fullpath and name to the file that you want to write.
    Java Code:
    String fullPath = "C:\\Home_Directories\\" + gradYear + "\\" + username + ("pass.txt");   // Create the full path 
    system.out.println("fullPath=" + fullPath); // show it
    The print it out with println before trying to create the file with the FileWriter class.
    The printout will show you where the FileWriter class is trying to write the file.

  5. #25
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    I've got it now!

    Just need to make another java app, that finds the folder and reads the .txt file..

  6. #26
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Java Code:
    import javax.swing.JOptionPane;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.File;
    
    public class folder {
    public static void main(String[] args) throws Exception {
    boolean status;
    // Prompt for Student Username
    String username = JOptionPane.showInputDialog(null,  "What is the Students Username?");
    
    // Prompt for Student Year Level
    String yearLevelString = JOptionPane.showInputDialog(null,  "What is the Students Year Level?");
    
    // Convert String to int
    int yearLevel = Integer.parseInt(yearLevelString); 
    
    // Correspond Year level to Graduation Year 
    
    int gradYear;
    
    if (yearLevel == 3) {
    gradYear = 2012;
    }
    else if (yearLevel == 2) {
    gradYear = 2013;
    }
    else if (yearLevel == 1) {
    gradYear = 2014;
    }
    else if (yearLevel == 0) {
    gradYear = 2015;
    }
    else {
    gradYear = 2016;
    } 
    
    
    // Create Path Variable for the Folder
    String path = "C:\\Home_Directories\\" + gradYear + "\\" + username;
    
    
    // Create folder
    status = new File(path).mkdirs();
    
    JOptionPane.showMessageDialog(null, "Folder created @" + path);
    
    // Set Password
    
    String password = JOptionPane.showInputDialog(null,  "Password?");
    
    try{
    // Create file 
    FileWriter writer;
        
          writer = new FileWriter( "C:\\Home_Directories\\" + gradYear + "\\" + username + "\\" +("pass.txt"));
          writer.write(password);
          writer.close();
        } catch (IOException e) {
          e.printStackTrace();
    
    }
    }
    }

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    A comment on the code posted:
    The String: path created on line 41 should be used on line 57. You should not duplicate the same stuff in two places.
    Create it in one place and use it in multiple places.

  8. #28
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Yeah i realise that, however i couldn't get it to work putting (path)
    so i resorted to repeating myself just to get it working.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    i couldn't get it to work putting (path)
    When you get errors, post the full text of the message here.

  10. #30
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Ok, I'm Probably really starting to bug you know but..

    I now want the program to tell if the user is on windows or linux/unix
    and if its in linux create the directory at ~Home\ if not then at C:\..

    I think System.getProperty("os.name") is needed..
    followed by if os.name == windows (or what have you)

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    Sorry, I don't don't know enough about other OSs to help.
    The System properties seems like a likely place.

  12. #32
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Ok fair enough.
    Do you know how system properties work?
    for example
    Java Code:
    public class OSinfo
      
     {
    public static void main(String[] args)
     {
    String nameOS = "os.name";
    System.out.println ("\nName Of OS: " + System.getProperty(nameOS));
    if (System.getProperty(nameOS) == "Windows 7") {
    System.out.println ("get on ubuntu");
    } else {
    System.out.println ("legend");
    }
     } 
      }
    how would i get this IF statement to work?
    So if the user is on windows 7 it will say get on ubuntu, if the user is on another OS it will say legend.
    Thanks.

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    When comparing Strings use the equals() method, not the == operator. == is for primitives not objects

    Look at the Properties class. It has methods to display the contents of a Properties object.
    Look at the System class. It has a method to get all the system's properties into a Properties object which you can display using the method from above.

    Look at what is shown there for ideas on what is available.
    Last edited by Norm; 11-16-2011 at 07:38 PM.

  14. #34
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    lol told you I was a noob :)
    Java Code:
    public class OSinfo
      
     {
    public static void main(String[] args)
     {
    String nameOS = "os.name";
    System.out.println ("\nName Of OS: " + System.getProperty(nameOS));
    if (nameOS) equals("Windows 7"); {
    System.out.println ("get on ubuntu");
    } else {
    System.out.println ("legend");
    }
     } 
      }
    error says 'else' without 'if' :S

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help please, Playing with folders and files.

    Look at the statements before the else to see why the compiler got confused about the else.
    There must be some problem before line 10 that confused the compiler.
    Look at line 8 in post #32 where there was not a problem. What is different?

  16. #36
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Java Code:
    public class OSinfo
      
     {
    public static void main(String[] args)
     {
    String nameOS = "os.name";
    System.out.println ("\nName Of OS: " + System.getProperty(nameOS));
    if (System.getProperty(nameOS).equals("Windows 7")) {
    System.out.println ("get on ubuntu");
    } else {
    System.out.println ("legend");
    }
     } 
      }

  17. #37
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    So now i just need to set if windows to create the directory at C:\
    else create at Home\
    or i could add this code to the launcher (which i haven't made yet) and just mimic my current code changing the directory.
    and the launcher could decide to run the code with windows directories or the code with linux directories.

  18. #38
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    Hello, me again.
    Could you help me with my 'if's

    Java Code:
    try{
    String nameOS = System.getProperty("os.name");
    
    if (nameOS.equals("Windows 7")) { 
    String path = "C:\\Home_Directories\\" + gradYear + "\\" + username;
    }
    else{
    String path = "Home\\Home_Directories\\" + gradYear + "\\" + username;
    }
      }catch (Exception e){
      System.out.println("Exception caught = "+e.getMessage());
      }
    
    
    // Create folder
    status = new File(path).mkdirs();
    
    }
    }
    I don't know how to use if to define a string.

  19. #39
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Help please, Playing with folders and files.

    I have it, was looking at it all wrong.

    Java Code:
    String pathWin = "C:\\Home_Directories\\" + gradYear + "\\" + username;
    
    String pathUni = "Home\\Home_Directories\\" + gradYear + "\\" + username;
    
    try{
    String nameOS = System.getProperty("os.name");
    
    if (nameOS.equals("Windows 7")) { 
    status = new File(pathWin).mkdirs();
    }
    else{
    status = new File(pathUni).mkdirs();
    }
    }catch (Exception e){
    System.out.println("Exception caught = "+e.getMessage());

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Playing mp3 files in Ubuntu
    By granadajose in forum Advanced Java
    Replies: 2
    Last Post: 09-07-2011, 08:26 PM
  2. Access files in folders and sub-folders
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-18-2011, 09:29 PM
  3. how to display computer folders and files in GUI
    By nashbender in forum AWT / Swing
    Replies: 12
    Last Post: 04-22-2011, 04:28 AM
  4. Folders to group source files
    By Skiller in forum New To Java
    Replies: 6
    Last Post: 03-31-2011, 08:20 PM
  5. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 09:41 PM

Tags for this Thread

Posting Permissions

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