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

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

1. ## 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. ## 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. ## Re: Create a file named .txt and store it in directory

Originally Posted by Norm
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.”

Originally Posted by Norm
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. ## 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.