aspose file tools
The moose likes Java in General and the fly likes How to convert String to File object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to convert String to File object" Watch "How to convert String to File object" New topic
Author

How to convert String to File object

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

I have a code that lists the contents of a directory.



I after listing the filenames, I want them to be converted to Files or File Object. And save it to the database. Is it possible?

I have tried this one and append it to the code above but it didn't work:



Stack trace:


java.lang.NullPointerException
at org.apache.jsp.HttpUploadDB_jsp._jspService(HttpUploadDB_jsp.java:148)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595)
java.lang.NullPointerException


Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
What is this, all in a JSP page? I recommend that you move this code into a class so you can test it better. In particular, what line is that error message coming from?

To get a File object rather than a String filename, you should probably include the parent directory:

Or to make this easier, just use listFiles() rather than list() in the first place.


"I'm not back." - Bill Harding, Twister
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Thanks so much for your reply. Yes, I will move my codes in servlets within the week but as of now I placed it in JSPs for testing purposes.

I followed your advice and modified the code:



But I am not able to iterate it in here.

filename = files[i];

Is says, "Cannot convert from File to String".

Thanks again.

Darren
[ September 10, 2007: Message edited by: Darren Alexandria ]
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

filename = files[i];

Is filename a String? If yes, then it definitely won't work. You can't assigned a File object into a String object. If you want to get the file name, you can do this:



I want them to be converted to Files or File Object. And save it to the database.

You shouldn't do that. If you want to save the files into the database, you should save the content of the file and not the File object.
[ September 10, 2007: Message edited by: Freddy Wong ]

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
mohsen fakhari
Greenhorn

Joined: May 04, 2003
Posts: 25
as jim said it is hard to say what causes this NullPointerException before you move the code
into a separate class.Or at least you can insert some try/catch statements.
And as I understood all what you need is the file name(the last part of complete path) and
its size,right?
Isn't it better to define children like this:

File[] children = dir.listFiles();

And then you can print children[i].getName() in your loop and use chldren[i].length() when
you need.
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Wow, thanks to your replies. I was really enlightened. All the while I thought that I can get the contents of the file using File Object. Actually, what I am trying to do is this: The client uploads files to the server's file system. After that, those will will be retrieved and then be uploaded to the database.
Is that sensible? or Can I do it in from Client and save it straight to the database? I have made some code with that process using HttpClient and FileUpload but it always ends up with "File Not Found".

Please give me advice.
Thanks.

Darren
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to convert String to File object
 
Similar Threads
File Not Found Exception when uploading
how to read files in a directory on a local network
Image Uploading in database through JSP
IllegalStateException: getOutputStream() has already been called for this response
NullPointerException