Reading a CSV file using an absolute path in java giving exception - java

I was trying to create a program to read a CSV file from the downloads folder on any windows computer and could not get the Java BufferedReader to find the file.
I read that java can handle absolute paths so I did:
File f = new File("%systemdrive%\\users\\%username%\\Downloads\\quotes.csv");
BufferedReader br = new BufferedReader(new FileReader(f));
This threw an IOException with the message :
%systemdrive%\users\%username%\Downloads\quotes.csv (The system cannot find the path specified)
I made sure that this file existed by entering the same path into File Explorer and easily enough, the file showed up.
I was wondering if something like this is possible and if there is some way to find and read this file.
Thank you for any help!

The %systemdrive% and %username% appear to be environment variables expanded by the File Explorer.
You might find this other entry in SO ( How to find out operating system drive using java? ) interesting to get the value for %systemdrive. Similarly, you can apply the same call to System.getenv to get the username.
FWIW, here there's a list of environment variables in Windows. Note the %HOMEPATH% environment variable, which points to the home directory of the current user.
With all these premises, you might consider the following code to fix your issue:
String userhome = System.getenv ("HOMEPATH");
File f = new File(userhome + "\\Downloads\\quotes.csv");
BufferedReader br = new BufferedReader(new FileReader(f));

You could try something like this:
String userHome = System.getProperty("user.home");
String path = userHome + "\\Downloads\\quotes.csv";
File f = new File(path);
BufferedReader br = new BufferedReader(new FileReader(f));

Related

Java Gmail API - Reading credentials from json file using Inputstream vs BufferedReader

Im trying to run my java application on Ubuntu server. I enter absolute path to read that file but it produces and error.
The code which is supposed to read that file is below.
private static final String CREDENTIALS_FILE_PATH = "/home/dockeradmin/credentials.json";
InputStream in = application.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
And the error I get:
java.io.FileNotFoundException: Resource not found: /home/dockeradmin/credentials.json
When I tried to read file with same path using BufferedReader everything worked perfectly.
Like this:
BufferedReader br = new BufferedReader(new FileReader(new File("/home/dockeradmin/credentials.json")));
So my question is, what is the difference between these two and how could I solve my current problem?
The reason of the error is that getResourceAsStream is used to locate file on classpath and it can't be used for locating file on file system.
This line caused trouble.
InputStream in = application.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
I fixed this by replacing that, with that
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);

Reading a text file from expected path in java

I want to read a file from directory.File is in root directory. If i use path as E:\Java\Netbeans_practice\project_141\Description.txt then it works fine.But when i wanted to use path as the file name or within a defined folder as Info\Description.txt , it gives error (java.io.FileNotFoundException: Description.txt (The system cannot find the file specified)). Actually i don't want to use the path name before project directory (ex: E:\Java\Netbeans_practice\project_141).I have searched a lot but unable to solve.Please help me. Here is my portion of code :
Scanner in = new Scanner(new FileReader("Description.txt");
while(in.hasNextLine()){
out.print("* "+in.nextLine()+"<br>");
}
When you deploy your web app, only the contents inside the "WebContent" will be deployed. You can verify this by going to (assuming you are using tomcat in your eclipse):
projectworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<contextName>
So you may wanan copy your "Description.txt" file into "/WEB-INF" (for security sake) directory. Then you should be able to access it:
File file = new File(getServletContext().getRealPath("/WEB-INF/Description.txt"));
Update:
String path="/WEB-INF/Description.txt";
InputStream inputStream = this.getServletConfig().getServletContext().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

How to load a File within a Jar that is not relative to the Class its loaded from

I am trying to load a file that is within a jar file. I try to get the file to load in a BufferedReader. For example:
BufferedReader br = new BufferedReader(new FileReader(fileName));
where fileName is my string from the root of the Jar file: something like this "resources/text.txt"
I am having a hard time finding out how to make this happen. Obviously FileReader will not work since it reads from the file system.
Anyone that can help me out?
Use the classloader to get the resource as a stream.
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getClassLoader().getResourceAsStream("/resources/text.txt"), "utf-8");
Note that you need to specific the correct character encoding for the content.
If you are trying to access a file within the same jar as your running program you should use
final InputStream inputStream = ClassName.class.getResourceAsStream(fileName);

Read file from windows directory

I tried reading xml from windows path D:/xml/xmlfile.xml
On my webpage there is a browse button. I select file and click submit. I comes to my controller and there is code to read this file.
fileToRead variable has a value = file name. Not full directory path.
InputStream ips = this.getClass().getClassLoader().getResourceAsStream(fileToRead);
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
This did not read file. Another technique also did not work and I got File not found exception
InputStream ips=new FileInputStream(file);
You can look into this site. There is working code for your problem.
http://www.codejava.net/java-ee/servlet/eclipse-file-upload-servlet-with-apache-common-file-upload
File should be on server if you want to just give file name or relative path. You have to upload file to server then read. Else you have to give full path to read file from windows directory.

Getting error saying file won't open in Java...any idea why this is happening?

I think I am really close, but I am unable to open a file I have called LocalNews.txt. Error says can't find file specified.
String y = "LocalNews.txt";
FileInputStream fstream = new FileInputStream(y);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Name of file is LocalNews.txt in library called News....anyone know why the file will not open?
The file is in the same Java Project that I am working on.
Error: LocalNews.txt (The system cannot find the file specified)
Project is named Bst, package is src in subPackage newsFinder, and library that the text files are stored in is called News.
Found out it was looking in
C:\EclipseIndigoWorkspace1\Bst\bin\LocalNews.txt
But I want it to look in (I believe)
C:\EclipseIndigoWorkspace1\Bst\News\LocalNews.txt
But if I make the above url a string, I get an error.
String y = "LocalNews.txt";
instead use
String y = "path from root/LocalNews.txt"; //I mean the complete path of the file
Your program can probably not find the file because it is looking in another folder.
Try using a absolute path like
String y = "c:\\temp\\LocalNews.txt";
By 'library called News' I assume you mean a jar file like News.jar which is on the classpath and contains the LocalNews.txt file you need. If this is the case, then you can get an InputStream for it by calling:
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("LocalNews.txt");
Use
System.out.println(System.getProperty("user.dir") );
to find out what your current directory is. Then you'll know for sure whether your file is in the current directory or not. If it is not, then you have to specify the path so that it looks in the right directory.
Also, try this -
File file = new File (y);
System.out.println(file.getCanonicalPath());
This will tell you the exact path of your file on the system, provided your file is in the current directory. If it does not, then you know your file is not in the current directory.

Categories