Hi I was reading files using FileReader and Buffereader only and it was working perfectly
fr = new FileReader("C:\\Users\\User\\Documents\\Bus_Station\\Bus station\\tripFile");
br = new BufferedReader(fr);
But I want to read put these files in somewhere so I put them in Referecnced Libraries so I can read from them on any machine without path but it is not working :/
(File file = new File("tripFile.txt");
file.createNewFile();
fr = new FileReader(file););
Any help ?
You should use resources.
URL fileURL = Resources.getResource("test.txt");
String text = Resources.toString(fileURL, Charsets.UTF_8);
How to add a resources folder
How do I add a resources folder to my Java project in Eclipse
adding resources in intellij for java project
How to create resource folder in Netbeans?
Related
I'm working with Eclipse and there, I have Java Project with the name "Test" which also contains text files. A class in this Project should be able to read in one of these files with a BufferedReader. This is my current code for that:
BufferedReader in = new BufferedReader(new FileReader("C:/Users/workspace/Test/testFile.txt"));
My file is always in the Project, but when I move the Project to another path, the file path changes, too, so I have to adjust the code with the new path.
I dont't want that, because it's impractical, so what can I do? How can I get the path of the Project?
You can add file to resources folder and read like
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Try something like this:
File currentDirFile = new File(".");
String fileDir = currentDirFile.getAbsolutePath();
I was having some issues getting the executable jar file for my project to work (outside of the eclipse IDE). I figured out the issue with java -jar fileName.jar
Here's the code:
fileIn = new Scanner(new File("src//resources//TestSave.txt"));
Through a little troubleshooting and research I understand that this wasn't working in the executable jar because it doesn't have a src folder, that's only created within the IDE. Here was my solution:
fileIn = new Scanner(new File("D://resources//TestSave.txt"));
The only problem with this is that if I want to put this program on my resume then who ever views it will have to place the folder into their D drive. I want it to be quick and easy so that they can just view my project with no hassle.
How can I access the resources folder within the executable jar file itself without having to reference/create any outside folders?
You have two options.
put the file in the same file as executable jar fileIn = new Scanner(new File("./TestSave.txt"));
Follow this post reading-a-resource-file-from-within-jar
Use getResourceAsStream (no File) within a JAR:
InputStream inputStream = classLoader.getResourceAsStream("TestSave.txt");
Scanner input = new Scanner(inputStream);
I'm working on a Java program in Netbeans that I'd like to be able to
run from an external jar file
And I cannot seem to read from a file that isn't located inside of
the project default directory or some subfolder located there, and
that only works inside Net beans.
What I'd like to be able to do is read the text file from the file
path
src/assets/files/textFile.txt
.
I've tried all of the suggestions here, but they don't seem to work for me. Here's the code I'm currently using:
File file = new File("assets/files/textFile.txt");
if (!file.exists()) {
throw new FileNotFoundException("File does not exist");
}
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
When I try to run this, the exception is thrown each time.
The term for such non-Files is resource.
String encoding = "UTF-8";
new BufferedReader(
new InputStreamReader(YourClass.getResourceAsStream("/assets/files/textFile.txt"),
encoding));
Also FileReader is an old utility class that uses the default system encoding. That might be different from where the text file was created, hence Unicode, such as encoded in UTF-8, would be ideal. For Windows users you might write a BOM character at the beginning to mark the file as UTF-8: "\ufeff".
The class YourClass should be from the jar. Here an absolute path is used "/...". A relative path would start with the package path.
Also Windows is not case-sensitive. Other systems and the zip format of the jar are case-sensitive. Check the jar with a zip utility for correct paths.
I am using a FileInputStream to read from a File object. My program is able to read the text file when run in the Eclipse IDE, but not when it is run as a JAR file.
I am exporting it as a JAR file and not a Runnable JAR file, and I know that the text file is already included in the JAR file because I extracted it to check.
This is what my file structure looks like:
I would suggest you to put your learnaboutfonts.txt inside src folder(not inside package) and read file in source code like the following:
Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:learnaboutfonts.txt");
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
And you can use br as you want to use. Happy Coding.
You should use something like:
InputStream input = getClass().getResourceAsStream("/learnaboutfonts.txt");
Note that the trick is using the leading "/" properly. If you don't put the leading /, then it searches for the file in the package of the class its invoked from.
How do you access your file? You maybe need a relative file path like "./mytext.txt").
Use this pattern:
InputStream is = new getClass().getResourceAsStream(
"/text.txt");
Test to include your text-file in your package of your class where you want to access the file.
I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored owl files in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource("/folderName").getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, i changed the path like:
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
ArrayList<File> filesFromCommonOntology = new ArrayList<File>();
while(str = br.readLine() != null)
{
File f = new File(line);
System.out.println(f);
filesFromCommonOntology.add(f);
}
for (int i = 0; i < filesFromCommonOntology.size(); i++)
{
file = filesFromCommonOntology.get(i);
String filePath = file.getPath();
Model timeModel = FileManager.get().loadModel(filePath,
ApplicationConstants.N3);
}
But when i run the project i get null pointer exception for line
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
How to resolve this problem?
You cannot enumerate files in a folder when the application is packaged into jar. It is even difficult to imagine how this could work at all: you open a folder as an inptu stream and read the file names (in that folder) that way?
The simplest way would be to add a text file containing entries in the folder, probably with some descriptions. MyClass.class.getResourceAsStream should open files (not folders) from the jar.
If you have very huge number of files, you can also try to reopen own jar as ZipFile and then you can search for the entries in you folder using ZipFile API. However this is more complex.