![]() |
| ||
| Putting files in Jlist ListFile class takes care of writing the files to Jlist Basically there is a Jlist in my GUI and I want it to display the files stored in my user folder. But for some reason it doesnt work the Jlist say that there is Null file. [code = java] package milestonemyworkspace; import java.util.*; import java.io.*; import javax.swing.JOptionPane; import java.lang.reflect.Array; /** * Handles the account details and login access. * * @author Abimar Skrubel * @version 1.0 */ public class User { private String name; //Username private String password; //The Special Picture private boolean access; public User() { } /** * Sets the username */ public void setName(String name) { this.name = name; } /** * Sets the password */ public void setPassword(String password) { this.password = password; } /** * Gets the username */ public String getName() { return name; } /** * Gets the password */ public String getPassword() { return password; } /* * Saves user information to a text file */ public void saveUser() { File nameDir = new File("H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + name); nameDir.mkdir(); //creates directory File fileo = new File("H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + name + ".dat"); try { DataOutputStream ou = new DataOutputStream(new FileOutputStream(fileo)); ou.writeBytes(name + " , " + password); ou.close(); } catch (Exception e) { System.out.println(e.toString()); } } /** * What is called after getting username and password for your account right */ public void login() { } /** * Reads the users account Information from the text file and matches it to user input to see if it is correct */ public void loadUser() { BufferedReader reader = null; try{ reader = new BufferedReader( new FileReader("H:\\SDI\\MileStoneMyWorkSpace\\MileStoneMyWorkSpace\\users\\" + name + ".dat")); System.out.println("Results..."); String line; line = reader.readLine(); while(line != null) { System.out.println(line); if(line.equals(name + " , " + password)) { System.out.println("Right u aree "); access = true; } else { System.out.println("WRONGGG"); access = false; } line = reader.readLine(); } System.out.println(); } catch(FileNotFoundException e){ System.out.println("Unable to find the file " + name + ".dat" ); } catch(IOException e) { System.out.println("Error found reading the file: " + name + ".dat" ); } finally{ if(reader != null) { //Catch any exception but nothing can be done about it. try{ reader.close(); } catch(IOException e) { System.out.println("Error on closing: " + name + ".dat"); } } } } /** * Gets user access * If user input it's correct password and username it has access * @return boolean */ public boolean getAccess() { return access; } } package milestonemyworkspace; import java.util.*; import java.io.*; /** * Lists files in fileList * * @author Abimar Skrubel * @version 1.0 */ public class ListFiles { private ArrayList files; private User user; public ListFiles() { files = new ArrayList(); User user = new User(); } //Write files to array list object public void writeFiles() { user.getName(); try{ File fileo = new File( "H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + user.getName() + "/fileList.dat"); DataOutputStream ou = new DataOutputStream(new FileOutputStream(fileo)); ou.writeBytes(user.getName()); ou.close(); } catch (Exception e) { System.out.println(e.toString()); } } //read files to put in J list public void readFiles() { BufferedReader reader = null; try{ reader = new BufferedReader( new FileReader("H:\\SDI\\MileStoneMyWorkSpace\\MileStoneMyWorkSpace\\users\\" + user.getName() + "fileList.dat")); System.out.println("Results..."); String line; line = reader.readLine(); while(line != null) { System.out.println(line); line = reader.readLine(); } System.out.println(); } catch(FileNotFoundException e){ System.out.println("Unable to find the file " + user.getName() + ".dat" ); } catch(IOException e) { System.out.println("Error found reading the file: " + user.getName() + ".dat" ); } finally{ if(reader != null) { //Catch any exception but nothing can be done about it. try{ reader.close(); } catch(IOException e) { System.out.println("Error on closing: " + user.getName() + ".dat"); } } } } public Object getFiles() { return files; } } package milestonemyworkspace; import java.util.*; import java.io.*; import javax.swing.JOptionPane; import java.lang.reflect.Array; /** * Handles the account details and login access. * * @author Abimar Skrubel * @version 1.0 */ public class User { private String name; //Username private String password; //The Special Picture private boolean access; public User() { } /** * Sets the username */ public void setName(String name) { this.name = name; } /** * Sets the password */ public void setPassword(String password) { this.password = password; } /** * Gets the username */ public String getName() { return name; } /** * Gets the password */ public String getPassword() { return password; } /* * Saves user information to a text file */ public void saveUser() { File nameDir = new File("H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + name); nameDir.mkdir(); //creates directory File fileo = new File("H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + name + ".dat"); try { DataOutputStream ou = new DataOutputStream(new FileOutputStream(fileo)); ou.writeBytes(name + " , " + password); ou.close(); } catch (Exception e) { System.out.println(e.toString()); } } /** * What is called after getting username and password for your account right */ public void login() { } /** * Reads the users account Information from the text file and matches it to user input to see if it is correct */ public void loadUser() { BufferedReader reader = null; try{ reader = new BufferedReader( new FileReader("H:\\SDI\\MileStoneMyWorkSpace\\MileStoneMyWorkSpace\\users\\" + name + ".dat")); System.out.println("Results..."); String line; line = reader.readLine(); while(line != null) { System.out.println(line); if(line.equals(name + " , " + password)) { System.out.println("Right u aree "); access = true; } else { System.out.println("WRONGGG"); access = false; } line = reader.readLine(); } System.out.println(); } catch(FileNotFoundException e){ System.out.println("Unable to find the file " + name + ".dat" ); } catch(IOException e) { System.out.println("Error found reading the file: " + name + ".dat" ); } finally{ if(reader != null) { //Catch any exception but nothing can be done about it. try{ reader.close(); } catch(IOException e) { System.out.println("Error on closing: " + name + ".dat"); } } } } /** * Gets user access * If user input it's correct password and username it has access * @return boolean */ public boolean getAccess() { return access; } } [/code] |
| All times are GMT -4. The time now is 2:12 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC