Create a folder outside of src folder - java

I've tried several different things to get this to work, and none of them have. I'm trying to create a file inside of a folder in Java. The project needs to have several text files that all relate to each other, and it would be more manageable to have them all together in one folder. Ideally, this folder would be stored outside of scr/.
Here is my current code for it (I do check for file existence first):
File testFile = new File("\\appts\\Appointments" + name + ".txt");
try {
testFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
However, I get an IOException when I try to run the code. I have tried it how it is above, also /appts/Appointments, appts/Appointments, appts\\Appointments. I tried searching online but couldn't find anything that worked.
Edit: Here is how my project setup currently looks like:
Project_Folder
src
com
weebly
roboticplayer
appointmentbook
CLASSES
Here is how I want it to look:
Project_Folder
src
com
weebly
roboticplayer
appointmentbook
CLASSES
appts

There are two easy ways to do this:
1) Absolute path
"C:\\users\\....\\parent_folder\\filename.txt";
2) relative path,
. (Single dot) is current directory
..(double dots) is parent directory
For example, you want to create text files under project folder. And the following is your file structure.
Project_folder
src
Java_main_file.java
appts
You want to create a file under appts from Java_main_file.java
String filename = "..\\appts\\filename.txt"
Then, create your file with filename. Here is a link how to create a text file.
Note that you need to make sure the folder under which you create the files exists. If it doesn't exist, you will get an error.

You can't create a file and its non-existent parent directories in one step, but you can create the parent directories first with File.mkdirs() and then create the file.

If your using JDK7 you can use nio package.
Path path = Paths.get("C:\\appts\\Appointments");
Files.createDirectories(path);
If you want to do a path relative to your current folder:
FileSystems.getDefault().getPath("appts", "Appointments");
If you want to see the absolute pathname:
FileSystems.getDefault().getPath("appts", "Appointments").toAbsolutePath().toString();
If you need that file object:
FileSystems.getDefault().getPath("appts", "Appointments").toFile();
You can also do .toFile() after the call toAbsolutePath().

Related

getResourceAsStream returns null with correct path

my piece of code:
private InputStream assetFromJAR(String fileName) {
return getClass().getResourceAsStream("/view/assets/ims/" + fileName + ".png");
}
returns always null...
My structure is:
src
main
java <- source root
view
assets
ims
it does not make sence why is this happening, path is correct so why does it always return null?
Thanks for answers!
Maven java projects are structured in way to separate the *.java files and resource files(like *.properties, *.jpg, *.png, etc..)
For *.java files: src/main/java folder
For resources: src/main/resources
So place your resources files directly under "src/main/resources" or sub folder of resources directory(like "src/main/resources/view/assets/ims/" for your project).
Then you can easily access the resource files.
private InputStream assetFromJAR(String fileName) {
return getClass().getResourceAsStream("/view/assets/ims/" + fileName + ".png");
}
I'm going to start with some things that are obvious, because you haven't indicated what you've tried. I apologize if you've done, or are aware of this:
Paths that begin with "/" are found somewhere in the class path. Paths without the beginning "/", start from where the code is called. Your class path roots may not be what you think they are, or the .png file is not being moved from your source directory to your target directory.
Have you verified the file is in the target directory ( assuming your using maven for building), or wherever your .class files end up?
Have you verified what you're class path roots are?

Error trying to read from a text file [duplicate]

I have a file named "word.txt".
It is in the same directory as my java file.
But when I try to access it in the following code this file not found error occurs:
Exception in thread "main" java.io.FileNotFoundException: word.txt
(The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Hangman1.main(Hangman1.java:6)
Here's my code:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
Put the word.txt directly as a child of the project root folder and a peer of src
Project_Root
src
word.txt
Disclaimer: I'd like to explain why this works for this particular case and why it may not work for others.
Why it works:
When you use File or any of the other FileXxx variants, you are looking for a file on the file system relative to the "working directory". The working directory, can be described as this:
When you run from the command line
C:\EclipseWorkspace\ProjectRoot\bin > java com.mypackage.Hangman1
the working directory is C:\EclipseWorkspace\ProjectRoot\bin. With your IDE (at least all the ones I've worked with), the working directory is the ProjectRoot. So when the file is in the ProjectRoot, then using just the file name as the relative path is valid, because it is at the root of the working directory.
Similarly, if this was your project structure ProjectRoot\src\word.txt, then the path "src/word.txt" would be valid.
Why it May not Work
For one, the working directory could always change. For instance, running the code from the command line like in the example above, the working directory is the bin. So in this case it will fail, as there is not bin\word.txt
Secondly, if you were to export this project into a jar, and the file was configured to be included in the jar, it would also fail, as the path will no longer be valid either.
That being said, you need to determine if the file is to be an embedded-resource (or just "resource" - terms which sometimes I'll use interchangeably). If so, then you will want to build the file into the classpath, and access it via an URL. First thing you would need to do (in this particular) case is make sure that the file get built into the classpath. With the file in the project root, you must configure the build to include the file. But if you put the file in the src or in some directory below, then the default build should put it into the class path.
You can access classpath resource in a number of ways. You can make use of the Class class, which has getResourceXxx method, from which you use to obtain classpath resources.
For example, if you changed your project structure to ProjectRoot\src\resources\word.txt, you could use this:
InputStream is = Hangman1.class.getResourceAsStream("/resources/word.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
getResourceAsStream returns an InputStream, but obtains an URL under the hood. Alternatively, you could get an URL if that's what you need. getResource() will return an URL
For Maven users, where the directory structure is like src/main/resources, the contents of the resources folder is put at the root of the classpath. So if you have a file in there, then you would only use getResourceAsStream("/thefile.txt")
Relative paths can be used, but they can be tricky. The best solution is to know where your files are being saved, that is, print the folder:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
File myFile = new File("word.txt");
System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());
Scanner input = new Scanner(myFile);
String in = "";
in = input.nextLine();
}
}
This code should print the folder where it is looking for. Place the file there and you'll be good to go.
Your file should directly be under the project folder, and not inside any other sub-folder.
If the folder of your project is named for e.g. AProject, it should be in the same place as your src folder.
Aproject
src
word.txt
Try to create a file using the code, so you will get to know the path of the file where the system create
File test=new File("check.txt");
if (test.createNewFile()) {
System.out.println("File created: " + test.getName());
}
I was reading path from a properties file and didn't mention there was a space in the end.
Make sure you don't have one.
Make sure when you create a txt file you don't type in the name "name.txt", just type in "name". If you type "name.txt" Eclipse will see it as "name.txt.txt". This solved it for me. Also save the file in the src folder, not the folder were the .java resides, one folder up.
I have the same problem, but you know why? because I didn't put .txt in the end of my File and so it was File not a textFile, you shoud do just two things:
Put your Text File in the Root Directory (e.x if you have a project called HelloWorld, just right-click on the HelloWorld file in the package Directory and create File
Save as that File with any name that you want but with a .txt in the end of that
I guess your problem is solved, but I write it to other peoples know that.
Thanks.
i think it always boils to the classpath. having said that if you run from the same folder where your .class is then change Scanner input = new Scanner(new File("word.txt")); to Scanner input = new Scanner(new File("./word.txt")); that should work

How to create a Path and a File that does not Exist in Java

This is the problem I have: If part or all of the path does not already exist, the server should create additional directories as necessary in the hierarchy and then create a new file as above.
Files.createDirectories(path);
That's what I am currently using, but it does not create the end file. For example is the path="/hello/test.html" it will create a directory called "hello" and one called "test.html", I want the test.html to be a file. How can I do that?
This is what I did to solve this "problem" or misuse of the libraries.
Files.createDirectories(path.getParent());
Files.createFile(path);
The first line will get the parent directory, so lets say this is what I want to create "/a/b/c/hello.txt", the parent directory will be "/a/b/c/".
The second like will create the file within that directory.
Have you looked at the javadoc? createDirectories only creates... directories. If you're intent on using Files.createDirectories, parse off the file name, call createDirectories passing only the path portion, then create a new file passing the entire path. Otherwise this is a better approach.
Files.createDirectories(path.substring(0, path.lastIndexOf(File.separator)+1));
File yourFile = new File(path);
you can parse the 'path' variable to isolate the file and the directory using delimiter as '/', and do File file = new File(parsedPath); This would work only when you know that you ALWAYS pass the file name at the end of it.
If you know when you are a) creating a directory b) creating a directory and file, you can pass the boolean variable that would describe if file needs to be created or not.

File not found?

I have an assignment and we have a couple of classes given, one of them is a filereader class, which has a method to read files and it is called with a parameter (String) containing the file path, now i have a couple of .txt files and they're in the same folder as the .java files so i thought i could just pass along file.txt as filepath (like in php, relatively) but that always returns an file not found exception!
Seen the fact that the given class should be working correctly and that i verified that the classes are really in the same folder workspace/src as the .java files i must be doing something wrong with the filepath String, but what?
This is my code:
private static final String fileF = "File.txt";
private static final ArrayList<String[]> instructionsF =
CreatureReader.readInstructions(fileF);
Put this:
File here = new File(".");
System.out.println(here.getAbsolutePath());
somewhere in your code. It will print out the current directory of your program.
Then, simply put the file there, or change the filepath.
Two things to notice:
check if "File.txt" is really named like that, since it won't find "file.txt" -> case sensitivity matters!
your file won't be found if you use relative filenames (without entire directory) and it isn't on your classpath -> try to put it where your .class files are generated
So: if you've got a file named /home/javatest/File.txt, you have your source code in /home/javatest/ and your .class files in that same directory, your code should work fine.
If you class is in package and you have placed the files as siblings then your path must include the package path. As suggested in other answers, print out the path of the working directory to determine where Java is looking for the file relative from.

List assets in a subdirectory using AssetManager.list

My application has an assets directory in which I've dumped a bunch of text files I need to load at runtime.
I have a directory full of assets of a particular type (i.e., "assets/subdir") and I want to load all of the files in this directory, one at a time.
I have code like this:
AssetManager assetMgr = getAssets();
String[] assetsIWant = assetMgr.list("subdir");
for(String asset: assetsIWant) {
doAssetyThing(asset);
}
I've tried a zillion different versions of the parameter to assetMgr.list() and am not getting anywhere.
If I use "/", I get back a list containing the "assets" directory, and a few random other items (META_INF, for example). If I pass any other string (like "assets" or "assets/" or "/assets" or "/assets/" or "mysubdir" or "/mysubdir" or "assets/mysubdir" or ...) then I get back an empty array.
The documentation is unfortunately fairly incoherent.
Does anybody know what the correct formula for that list() parameter is?
Passing an empty string seems to work for me. I get a list of the files in my assets directory when I do the following:
AssetManager aMan = appContext.getAssets();
String[] filelist = aMan.list("");
I ve use the following code to list the file name in assets/myFolder/:
String[] fileNames =getAssets().list("myFolder");
for(String name:fileNames){
System.out.println(name);
}
note that the parameter in the method list does not contains "/".
When you need to have access to a folder down deeper in your hierarchy use
String[] fileNames =getAssets().list("myFolder"+File.separator+"mysubfolder");
instead of "/" inside the String, which would give you an empty String array as result.
I'm not sure why it works this way, but when I list "/", I get root level stuff, not things from my "assets" directory. I actually found no way to properly list my assets folder in the short time I spent trying.
The workaround I'm using is to create a "subdir" directory inside of my "assets" directory. I put all the assets that I want to access in this "subdir", and do:
String[] assetsIWant = assetMgr.list("subdir");
I don't know why "/" lists the "assets" directory itself as one of it's files, yet you don't have to specify the "assets" directory when giving list a path. Maybe someone else knows, but I hope this tip will help you out anyway.
There are a lot of similar questions around this topic, I suspect you are using the assets folder in a library project. if you try to access assets in a library project on android you will get empty contents and the exact symptoms you have described here
from the android "projects" documentation
source : http://developer.android.com/tools/projects/index.html
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in
a library project. Any asset resources used by an application must be
stored in the assets/ directory of the application project itself.
However, resource files saved in the res/ directory are supported.
if this is the case, the answer is that you should only include Raw assets in the assets folder of your application project NOT in any project marked as a library (you can check this by right clicking your project and selecting properties, select the android entry on the left and look at the is library checkbox)
Let's say you have this folder set up: Assets->SubDir1. You have things in Subdir1 like A.txt, B.txt, C.txt. So from the Project's directory, it would be Assets/SubDir1/A.txt.
Taking that into account, the way to access that file is similar to what you're doing, adding one thing. That one thing is in your doAssetyThing function. You have to put the SubDir1 directory in front of it, even if you did a filter of "SubDir1" in the .list("");
Example:
AssetManager am = getResources().getAssets();
String [] list = am.list("SubDir1");
for (String s:list) {
doAssetyThing("SubDir1/" + s);
//I use this next line as the start of copying a pdf from one location
//to another. I wanted to give you a real function in use.
InputStream inStream = am.open("SubDir1/" + s);
//more code
}
I hope this is clear and helps you out!
I use the following two methods to ensure given asset is present:
protected boolean hasAsset(String id) {
return hasAsset(id,"");
}
protected boolean hasAsset(String id, String dir) {
int idx = id.indexOf('/');
if (idx > 0 && idx < id.length() - 1) {
if (dir.length() > 0) {
dir = dir + "/" + id.substring(0,idx);
} else
dir = id.substring(0,idx);
return hasAsset(id.substring(idx + 1), dir);
}
try {
return Arrays.asList(context.getAssets().list(dir)).contains(id);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
For some reason, empty directories are not listed.
At least, in my experience, they are not listed.
That is, if you have some-dir/d1/f1 and some-dir/d2 in the assets area, and you list("some-dir"), d2 is not listed.
This solution seems working for me:
Keep a copy of each file you want to access from a sub directory in the assets directory too.
Use same convention. i.e. list("subdirectory")
It doesn't read the files in assets folder, but reads all "copied" files in its sub directory.
Weird!! But works!

Categories