Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Create a file named .txt and store it in directory

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a file named .txt and store it in directory

    Hello, I am having a hard time understanding this logic. I need to create a file named javaprogramming.txt. This file should be stored in the following directory: c:\data\java\it215. If the directory does not exist it should be created. If the file and or directory cannot be created for any reason the following message should be displayed to the user: “An error occurred creating javaprogramming.txt. Please contact your System Administrator.”

    First, since java is case sensitive I thought we had to save our files as JavaProgramming rather then javaprogramming (uppercase not lowercase)? Then if I save a file under .txt it won't be a program just a txt. Im so confused on the logic of this question. I am sure I am over thinking it but I still don't get it. I did do my reasearch and come up with lots of websites but no help. I have seen several different examples but not sure I get the point of the logic in the question I guess.

    import java.io.File;

    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = ""; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(new MyFilter()); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
              System.out.println(listOfFiles[i].getName());
      }
    }
    }
    Now, I also have seen the try and catch statements too which I thought would be key in what I am trying to do. Then as I read on and found in my "Java for Dummies" book and there are different examples. Then I also thought maybe it would look like this:
    if (!createPath( path ))
    	    System.out.println( "Failed to create path " + path );
     
     
    	public boolean createPath( string path ) {
    	    File filePath = File( path );
     
    	    if (!filePath.exists())  //if the dirs do not exist
    	        try {
    	            filePath.mkdirs();
    	        }
    	        catch( SecurityException ) { return false }
    	    return true;
    	}
    Can anyone help me understand the logic behind the question above so that I can understand this? Why would we ever want to add a directory name to a file name in java anyway? I very confused.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Create a file named .txt and store it in directory

    if I save a file under .txt it won't be a program just a txt
    The file extension is used by theWindows OS to find the program to open/process the file. You can write a program source to a file with a .txt extension, however the javac command requires its source's extension be .java.
    Does your assignment say what text you are to put into the .txt file?

    Why would we ever want to add a directory name to a file name in java anyway?
    You mis-understand the assignment. It says to create a .txt file and put it in a specific directory.
    If the path to the directory does NOT exist, you are to create that path.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a file named .txt and store it in directory

    Quote Originally Posted by Norm View Post
    The file extension is used by theWindows OS to find the program to open/process the file. You can write a program source to a file with a .txt extension, however the javac command requires its source's extension be .java.
    Does your assignment say what text you are to put into the .txt file?
    this is What the assignment says:
    Write the statements necessary to create a file named javaprogramming.txt. This file should be stored in the following directory: c:\data\java\it215. If the directory does not exist it should be created. If the file and or directory cannot be created for any reason the following message should be displayed to the user: “An error occurred creating javaprogramming.txt. Please contact your System Administrator.”

    Quote Originally Posted by Norm View Post
    You mis-understand the assignment. It says to create a .txt file and put it in a specific directory.
    If the path to the directory does NOT exist, you are to create that path.
    How do I create a path i a .txt file in java? I am so confused. Thank you for your reply

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Create a file named .txt and store it in directory

    Look at the File class for method to create a directory.
    Do the project in small, easy steps. When you get all the techniques figured out, merge them into the big program.
    Write a simple test program using the File class to create a directory at one level and then try changing it to create multilevel directories. After each execution check what was created and delete it for the next test.

    Write another test program to create a file and write something to that file.

    Go to this site and search for Files:http://download.oracle.com/javase/tu...ybigindex.html
    There is a section on Reading, Writing and Creating Files.
    And another for directories.
    Last edited by Norm; June 28th, 2011 at 03:51 PM.

  5. The Following User Says Thank You to Norm For This Useful Post:

    JavaPF (June 28th, 2011)

Similar Threads

  1. Replies: 2
    Last Post: April 21st, 2011, 10:29 AM
  2. Help with making the user input to store in file
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 06:52 AM
  3. Replies: 2
    Last Post: January 21st, 2011, 01:07 AM
  4. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 04:29 PM
  5. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 03:39 AM