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

    Default Help please, Playing with folders and files.

    Hello, this is my first post, if i do something wrong please troll me :)

    I am working on a simple java project:
    At the moment I am just trying to create a folder and assign a password to the folder. The idea being when the user inputs his name and password the program will open his folder.
    The password can (for now) be stored in a .txt file within the users folder.

    This is the code I have so far:

    import javax.swing.JOptionPane;
    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 of study?");

    // 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();

    // Set Password

    String password = JOptionPane.showInputDialog(null, "Enter Password");

    //Is where I am stuck.
    }
    }

    I am aware I may need to put more research into JoptionPane.../

    Thank you for any advice :)

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

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

    assign a password to the folder
    Can you explain how this will work? What does it mean to assign a password to a folder? Is this a security feature of your OS?

  3. #3
    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.

    Hi, I understand that java can't implement a security feature of the OS, or so I am led to believe?
    If you can adjust windows folder options in java then that could be an option, but I don't think you can..

    The password is for the java program to find the folder, so within the app (after user has created the folder and password) you will be asked for username year and a password.
    the java app will find the folder based on username and year, then read a text file inside the folder called 'pass.txt' if the input password maches the one in the pass.txt,
    the folder is opened in explorer (for now, i am looking to make a java folder viewer, if that makes sense??)

    I would then aim to encrypt the folder so only the java program can open it, if this is possible?

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

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

    Writing a program to take user input and then find and read a file and then show the contents of a folder should be no problem.

  5. #5
    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'm a total noob sorry :(

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

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

    Break the project up into small simple steps and solve one at a time.
    What are the features you want the program to have?
    Start with one of the features and write a small test program to work out how to provide that feature.
    When you get several of the feature working as small simple programs, try merging them into the larger program you are trying to create.

  7. #7
    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 want the program to put a .txt file in the directory containing the password, I've googled but can't find anything that works.

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

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

    I want the program to put a .txt file in the directory containing the password
    Several steps here
    1) Find the directory with the password file. How do you do that? Do you ask the user? Do you store the path to the directory in some persistent storage (like the registry on Windows) or in a .ini file or do you hard code it in the program?
    2) Get the path to that directory and build an output class object with the path and the file name
    3) write the file.

    Each is a different problem. For testing and development, hardcode the path in your program for now and delay writing the code that finds the path. Concentrate on creating and writing the file.

  9. #9
    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 in my code the folder is created here:
    String path = "C:\\Home_Directories\\" + gradYear + "\\" + username;


    // Create folder
    status = new File(path).mkdirs();

    // Here they enter a password. the password entered here goes in the text file, the text file goes in the above directory.

    String password = JOptionPane.showInputDialog(null, "Enter Password");

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

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

    Now you need to look at how to write a text file. There are lots of sample programs on the forum. Search for Writer to find some.

  11. #11
    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 know i could use this:
    Java Code:
    import java.io.*;
    class FileWrite 
    {
     public static void main(String args[])
      {
      try{
      // Create file 
      FileWriter fstream = new FileWriter("pass.txt");
      BufferedWriter out = new BufferedWriter(fstream);
      out.write(+ password); //not sure on this??
      //Close the output stream
      out.close();
      }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      }
      }
    }
    but how do I put it in my current code?
    And how do i set the directory?
    Last edited by Norm; 11-15-2011 at 07:13 PM. Reason: added cod tags

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

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

    What does the API doc for the FileWriter say about its constructor?
    Try putting a path there and executing the code and see if it writes where you want it to.

  13. #13
    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 added this to the end of the code.
    I can't figure out how to create the file in "C:\\Home_Directories\\" + gradYear + "\\" + username;..

    Java Code:
    String password = JOptionPane.showInputDialog(null,  "Password?");
    
    try{
    // Create file 
    FileWriter writer;
        
          writer = new FileWriter("pass.txt");
          writer.write(password);
          writer.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();

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

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

    how to create the file in "C:\\Home_Directories\\" + gradYear + "\\" + username;..
    Does the code you posted create a file?
    What if you replace the argument to the FileWriter constructor with the name of the file you want to create?

  15. #15
    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.

    that code saves a .txt file containing the password in the same directory i launch the program from.

    I am trying to have the .txt file save at C:\\Home_Directories\\" + gradYear + "\\" + username
    instead.

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

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

    I am trying to have the .txt file save at ...
    Then you need to put the desired path in the constructor. Not just the filename but the full path to the file.

  17. #17
    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:
    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 returns one error, can not find symbol and points (^) to username
    ^

  18. #18
    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.

    You play chess much?

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

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

    writer = new FileWriter( "C:\\Home_Directories\\" + gradYear + "\\" + username ("pass.txt"));
    The syntax of: username("pass.txt") looks like a method call. Is username a method that returns a String?
    What are you trying to do?

  20. #20
    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.

    Username, as well as gradYear, is created here:
    Java Code:
     // 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;
    }
    This information is then used to create a directory

    Java Code:
    // 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);
    I really appreciate your help.

Page 1 of 2 12 LastLast

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
  •