Results 1 to 7 of 7
Thread: Filelist
- 07-23-2013, 12:18 PM #1
Senior Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 135
- Rep Power
- 0
Filelist
Hi,
I have been playing around with files in a small java Application I am making.
It Works, but I am not completly satisfied with my code. I think there could be a easier way of doing it.
Heres my case:
I have a path to where my files are. I then wants to list my files recursively, split out the Complete path (path + subdirectory) and the filename.
Then I write the result to a MySQL table.
I have accomplished this. Heres the code:
Java Code:public void ListSource(String pathname) { File folder = new File(pathname); File[] files = folder.listFiles(); if (files == null) return; for (File file : files) { if ( file.isDirectory() ) { ListSource( file.getAbsolutePath() ); file.getAbsoluteFile(); String path = file.toString(); }else { file.getAbsoluteFile(); long modified = file.lastModified(); Date modifiedDate = new Date(modified); String filename = file.toString(); String outfile = filename; String [] splittet = outfile.split("\\\\"); String st = splittet[0]; String directory1 = splittet[1]; String directory2 = splittet[2]; String directory3 = splittet[3]; String fileName = splittet[4]; String directory = "\\" + directory1 + "\\" + directory2 + "\\" + directory3 + "\\"; SourceTable.insert(fileName, directory, pathname, modifiedDate); } } }
I could write som if conditions like this:
Java Code:if(splittet.length == 5)
But there must be a easier way of identify the filename and show the Complete path?
Any suggestions?
- 07-23-2013, 12:30 PM #2
Re: Filelist
Yep. Get the name via File.getName(). Get the absolutePath via File.getAbsolutePath(). Then use substring on absolutePath to cut off the filename at the end.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-23-2013, 01:44 PM #3
Senior Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 135
- Rep Power
- 0
Re: Filelist
Thanks for a good pointer in the right direction. :)
Here is my code now:
Java Code:String fileName = file.getName(); String directory = file.getAbsolutePath(); long modified = file.lastModified(); Date modifiedDate = new Date(modified);
When I run this, the string Directory contains the complete path including the filename.
Do you have an example?
- 07-23-2013, 01:49 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 10
Re: Filelist
Why ask for one? Google "java substring example" and get one of the hundred that already exists. Then you don't ask people in this forum to do what has been done countless times before, and you save yourself waiting time.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 07-23-2013, 02:23 PM #5
Re: Filelist
A pointer, you want to remove the filename from the path and you know how long the filename is.
API: String.substring(int,int)Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-23-2013, 02:35 PM #6
Senior Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 135
- Rep Power
- 0
Re: Filelist
I might be a little slow... But how do I now how long the filename is?
The substring(int,int) I understand, but how to find the int,int?
- 07-23-2013, 02:49 PM #7
Re: Filelist
What are the two ints for? What does the API say? I think you haven't read the API docs.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Bookmarks