Results 1 to 20 of 39
- 11-15-2011, 01:43 PM #1
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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 :)
- 11-15-2011, 01:58 PM #2
Re: Help please, Playing with folders and files.
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?assign a password to the folder
- 11-15-2011, 02:10 PM #3
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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?
- 11-15-2011, 02:13 PM #4
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.
- 11-15-2011, 02:16 PM #5
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
Re: Help please, Playing with folders and files.
I'm a total noob sorry :(
- 11-15-2011, 03:35 PM #6
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.
- 11-15-2011, 05:43 PM #7
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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.
- 11-15-2011, 05:48 PM #8
Re: Help please, Playing with folders and files.
Several steps hereI want the program to put a .txt file in the directory containing the password
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.
- 11-15-2011, 06:58 PM #9
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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");
- 11-15-2011, 07:05 PM #10
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-15-2011, 07:08 PM #11
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
Re: Help please, Playing with folders and files.
I know i could use this:
but how do I put it in my current code?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()); } } }
And how do i set the directory?Last edited by Norm; 11-15-2011 at 07:13 PM. Reason: added cod tags
- 11-15-2011, 07:13 PM #12
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.
- 11-16-2011, 02:38 PM #13
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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();
- 11-16-2011, 02:45 PM #14
Re: Help please, Playing with folders and files.
Does the code you posted create a file?how to create the file in "C:\\Home_Directories\\" + gradYear + "\\" + username;..
What if you replace the argument to the FileWriter constructor with the name of the file you want to create?
- 11-16-2011, 02:51 PM #15
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
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.
- 11-16-2011, 02:53 PM #16
Re: Help please, Playing with folders and files.
Then you need to put the desired path in the constructor. Not just the filename but the full path to the file.I am trying to have the .txt file save at ...
- 11-16-2011, 02:54 PM #17
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
Re: Help please, Playing with folders and files.
This returns one error, can not find symbol and points (^) to usernameJava 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();
^
- 11-16-2011, 02:58 PM #18
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
Re: Help please, Playing with folders and files.
You play chess much?
- 11-16-2011, 02:58 PM #19
Re: Help please, Playing with folders and files.
The syntax of: username("pass.txt") looks like a method call. Is username a method that returns a String?writer = new FileWriter( "C:\\Home_Directories\\" + gradYear + "\\" + username ("pass.txt"));
What are you trying to do?
- 11-16-2011, 03:05 PM #20
Member
- Join Date
- Nov 2011
- Location
- U.K
- Posts
- 27
- Rep Power
- 0
Re: Help please, Playing with folders and files.
Username, as well as gradYear, is created here:
This information is then used to create a directoryJava 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; }
I really appreciate your help.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);
Similar Threads
-
Playing mp3 files in Ubuntu
By granadajose in forum Advanced JavaReplies: 2Last Post: 09-07-2011, 08:26 PM -
Access files in folders and sub-folders
By kakinyim in forum CLDC and MIDPReplies: 0Last Post: 05-18-2011, 09:29 PM -
how to display computer folders and files in GUI
By nashbender in forum AWT / SwingReplies: 12Last Post: 04-22-2011, 04:28 AM -
Folders to group source files
By Skiller in forum New To JavaReplies: 6Last Post: 03-31-2011, 08:20 PM -
Searching directories for folders and .txt files
By XDrew574X in forum New To JavaReplies: 1Last Post: 03-29-2011, 09:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks