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.
Related
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));
This is a chunk of data I'd like to access by a method.
I'm doing the following to read my file:
String fileName = "file.txt"
InputStream inputStream = new FileInputStream(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
My file.txt is in the same package, but I still get FileNotFoundException.
I didn't use a path url to point to the file because I thought since this it going to be an android application, hard-coding the path might not work when deployed... Please correct me if I am wrong. Thanks bunch!
This shows how to do that. https://stackoverflow.com/a/14377185/2801237
Also the 'package' your class is in has nothing to do with the 'path' where the file is being executed from. (two different concepts, 'package' = folder hierarchy of java source code files), 'path' = location on a filesystem of a specific file, your APK is being 'executed' in a particular place, and the location it writes a file is associated with that (I actually don't know where 'offhand' it writes by default, because I always get cache dir, or sd card root, etc.)
You may use:
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
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));
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);
I'm a beginner in Tomcat and Servlet.I come across a problem about reading File in servlet. I have search a lot information about this problem in stackoverflow,but I haven't solved it.I hope get some help.I write the code as following:
URL url=getServletContext().getResource("/WEB-INF/DataSpecification.owl");
File file=new File(url.toString());
FileInputStream input=new FileInputStream(file);
Reader in = new InputStreamReader(input,"UTF-8");
I get the following error:
java.io.FileNotFoundException: jndi:\localhost\MAGS\WEB-INF\DataSpecification.owl (File name or directory name wrong).
at java.io.FileInputStream.open(Native Method)
I put my file in WEB-INF directory.
I know that I can get an InputStream by using
getServletContext().getResourceAsStream()
But for some reasons,I nead to get a FileInputSream.
I hope to get your help!thank you!
This should work (no need to use FileInputStream):
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/DataSpecification.owl");
Reader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));