Results 1 to 11 of 11
  1. #1
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Problem with creating folders and directing file path

    Hello everyone,

    What I am trying to do is make my program create folders and files that are not created yet using this code:

    Java Code:
    String userHome = System.getProperty("user.home");
    Path thePath = Paths.get(userHome, "AppData", "Roaming", 
       "MyAddressBook", "MyAddressBook.txt");
    It doesn't seem to want to create to folders or files though.

    Also I want to make it to where this code:

    Java Code:
    table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;    
        File f = new File("C:\\users\\Jason\\AppData\\Roaming");
        is = new FileInputStream(f); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        
        
        save.addActionListener(new ActionListener() {
                 
         @Override
         public void actionPerformed(ActionEvent e) {
         BufferedWriter writer;
         try {
         File file = new File("C:\\users\\Jason\\AppData\\Roaming");
         writer = new BufferedWriter(new FileWriter("C:\\users\\Jason\\AppData\\Roaming",true));
         writer.write(textName.getText() + "\t" + textHPhone.getText() 
         + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
         + textCity.getText() + "\t" + textState.getText() + "\t" + 
         textZip.getText() + "\t" + textCountry.getText());
         writer.newLine();
         writer.close();
         } catch(FileNotFoundException ex) {
         } catch (IOException ex) {
         }
            Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
         }});
        }
    Reads and saves to the same place as the code creating the file instead of the way I have it right now.
    Basically, I am trying to make this program where it will work on any windows computer it is put on.
    Thanks for any help you can give me.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Problem with creating folders and directing file path

    For the first part, a Path is just that. A Path in the file system. It does not necessarily exist.
    All the Paths.get does is create a Path that represents the given values. It doesn't interact with the file system at all.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,003
    Rep Power
    6

    Default Re: Problem with creating folders and directing file path

    As backed up by the API documentation:

    Paths (Java Platform SE 7 )

    It makes no mention of anything being created. Its purely informational.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with creating folders and directing file path

    Quote Originally Posted by Tolls View Post
    For the first part, a Path is just that. A Path in the file system. It does not necessarily exist.
    All the Paths.get does is create a Path that represents the given values. It doesn't interact with the file system at all.
    Quote Originally Posted by gimbal2 View Post
    As backed up by the API documentation:

    Paths (Java Platform SE 7 )

    It makes no mention of anything being created. Its purely informational.
    What is the name of the code then that I would need to create folders and files within the path that I want?

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,003
    Rep Power
    6

    Default Re: Problem with creating folders and directing file path

    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    Lack of knowledge is not a problem, it is solved by performing research. I would strongly advise you to learn to like to do that, because you'll be doing it for the rest of your IT career. And if you don't do it, your IT career is never going to begin.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with creating folders and directing file path

    Quote Originally Posted by gimbal2 View Post
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    Lack of knowledge is not a problem, it is solved by performing research. I would strongly advise you to learn to like to do that, because you'll be doing it for the rest of your IT career. And if you don't do it, your IT career is never going to begin.
    Thanks for the link, I'll get started on reading that today. I have been trying to find the answer to my question for some time now but I guess I wasn't typing in the correct wording. I am sure as I progress in java I will learn more on how to look up things better with java.

    Thanks again

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Problem with creating folders and directing file path

    File.
    Which has mkdirs.
    And createNewFile.

    I don't know if there's an nio version that does it differently.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with creating folders and directing file path

    ok, I have got my program to start creating my files and folders needed. The trouble I an having now is when the program runs and I type in any information and hit save, it will save once and refresh the table but every time after that it writes over the first thing saved and does not refresh the table anymore. What I think is happening is that it keeps remaking the same file every time I hit the save button even though I put an if statement that if the file is not made to make it then. Here my code:

    Java Code:
    File fold = new File(System.getProperty("user.home") 
                + "/AppData/Roaming/MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        File tmp = new File(System.getProperty("user.home") + "/AppData/Roaming/"
                + "MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;    
        File f = new File(System.getProperty("user.home") + "/AppData/Roaming/"
                + "MyAddressBook/MyAddressBook.txt");
        is = new FileInputStream(f); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        
        
        save.addActionListener(new ActionListener() {
                 
         @Override
         public void actionPerformed(ActionEvent e) {
         BufferedWriter writer;
         try {
         File file = new File(System.getProperty("user.home") + "/AppData/Roaming/"
                + "MyAddressBook/MyAddressBook.txt");
         writer = new BufferedWriter(new FileWriter(System.getProperty("user.home") 
                 + "/AppData/Roaming/MyAddressBook/MyAddressBook.txt"));
         writer.write(textName.getText() + "\t" + textHPhone.getText() 
         + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
         + textCity.getText() + "\t" + textState.getText() + "\t" + 
         textZip.getText() + "\t" + textCountry.getText());
         writer.newLine();
         writer.close();
         } catch(FileNotFoundException ex) {
         } catch (IOException ex) {
         }
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
         }});
        }
                          
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    I am right in my assumptions? If so could you just give me a hint into how to fix it? I am not looking for the answer, just a hint. I would never learn java if I have the answers just given to me lol

  9. #9
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with creating folders and directing file path

    ok, I have been trying to find out how to fix the problem on my own and haven't gotten anywhere yet. I am going to keep trying, and I think I know where the code is messing up at (I think it's in the event handler) I just don't know how to fix it quite yet. I have found this to be the fun part of programming for me, trying to track down where the problem lies. Anyway I have modified and clean up my code some to make it look nicer. I would still like some hints into how to fix the problem mentioned in the posted above this one.

    The problem I am having:
    Quote Originally Posted by wigwom View Post
    ok, I have got my program to start creating my files and folders needed. The trouble I an having now is when the program runs and I type in any information and hit save, it will save once and refresh the table but every time after that it writes over the first thing saved and does not refresh the table anymore. What I think is happening is that it keeps remaking the same file every time I hit the save button even though I put an if statement that if the file is not made to make it then.
    Here's the new code:

    Java Code:
        File fold = new File(System.getProperty("user.home") 
                + "/AppData/Roaming/MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        final File tmp = new File(System.getProperty("user.home") + "/AppData/Roaming/"
                + "MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        
        
        save.addActionListener(new ActionListener() {
                 
         @Override
         public void actionPerformed(ActionEvent e) {
         BufferedWriter writer;
         try {
         writer = new BufferedWriter(new FileWriter(tmp));
         writer.write(textName.getText() + "\t" + textHPhone.getText() 
         + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
         + textCity.getText() + "\t" + textState.getText() + "\t" + 
         textZip.getText() + "\t" + textCountry.getText());
         writer.newLine();
         writer.close();
         } catch(FileNotFoundException ex) {
         } catch (IOException ex) {
         }
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
         }});
        }
                          
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Problem with creating folders and directing file path

    FileWriter has more than one constructor.
    The one you are using defaults to overwriting the existing contents, not appending.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with creating folders and directing file path

    Quote Originally Posted by Tolls View Post
    FileWriter has more than one constructor.
    The one you are using defaults to overwriting the existing contents, not appending.
    Sorry it took me so long to get back with you. Been a busy week for me, I got my program working and it's doing just the way I want it to. I would like to thanks you for taking the time and helping me out.

Similar Threads

  1. Replies: 8
    Last Post: 08-27-2013, 08:50 PM
  2. Create folders dynamically in diff system (known path + IP)
    By wiki.horizon in forum Advanced Java
    Replies: 5
    Last Post: 06-28-2011, 09:29 AM
  3. problem in file path
    By jperson in forum New To Java
    Replies: 2
    Last Post: 01-15-2010, 11:26 AM
  4. problem with directing java code to applet
    By neha_gautam46@yahoo.com in forum AWT / Swing
    Replies: 0
    Last Post: 04-01-2009, 09:25 AM
  5. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 03:32 PM

Posting Permissions

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