So I've written a simple text editor in java, and it retrieves the file via showOpenDialog() and converts the filename into a string, so it can be displayed as the title:
String title = fc.getSelectedFile().toString();
But lets say I have the path "home/user/file.txt". How would I strip off the path and make it so the filename displays as "file.txt" only?
File getName() returns what you want i.e just the last name in the pathname's name sequence.
getSelectedFile() returns a File object; the easiest thing to do would be to just call getName() on the File object. If the path comes from someplace else, you could actually construct a File from it and then call getName().
String filename = title.substring(title.lastIndexOf("/"))
edit: brain's answer is better and more concise though :-)
Related
i have a path
C:\Users\abc xyz\Desktop\test.docx
I want to change in into
C:\Users\sara waheed\Desktop\~$sara.docx
for that first i got the last index of backslash now i want t append '~$' after the last backslash
String str=path.toString();
int index = str.lastIndexOf('\\');
Note
I do not know the value of the path in advance
how can i achieve that
In general, you should use java.nio.file API when manipulating paths, so that it can avoid platform dependent assumptions.
String str = path.getFileName().toString();
// The string concatenation way:
if(str.endsWith(".docx")) { // should we check the beginning of fname?
// maybe it already has the prefix?
path = path.resolveSibling("~$" + str);
}
System.out.println(path);
Since you're representing files, I suggest using the Path api. You can modify any part of the path without messing with the rest.
Path file = Paths.get("C:\\Users\\abc xyz\\Desktop\\test.docx");
Path lock = file.resolveSibling("~$" + file.getFileName());
To interact with these paths, go ahead and look at the Files class.
Files.touch(lock);
Or alternatively using the classic way via path.toFile()
By the way, these are all in the java.nio.file package.
I'm developing a project for college which consist reading a CSV file and converting that to a PDF file. That part is fine, I have already done that.
In the end I need to show the name of the PDF file without the full path of where it was created. In other words, I just want the to show the name.
I search a lot to see if there is a simple method that show the name like Java has to show only the name of the File like
file.getName();
Whenever you use iText to create a PDF file, your code sets the target which usually is an OutputStream. If you use a FileOutputStream there, you know the file it writes to.
Thus, all you have to do to to show the name of the PDF File is to inspect your own code and check which target it sets.
Use getBaseName in Apache Commons IO.
getBaseName
public static String getBaseName(String filename)
Gets the base name, minus the full path and extension, from a full
filename.
This method will handle a file in either Unix or Windows format. The
text after the last forward or backslash and before the last dot is
returned.
a/b/c.txt --> c
a.txt --> a
a/b/c --> c
a/b/c/ --> ""
The output will be the same irrespective of the machine that the code
is running on.
Parameters:
filename - the filename to query, null returns null
Returns:
the name of the file without the path, or an empty string if none exists. Null bytes inside string will be removed
Source: https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FilenameUtils.html#getBaseName(java.lang.String)
If you also need the extension, use getExtension. Which would probably always be .pdf, but you know, it's perfectly valid to have a PDF file without the .pdf filename extension. No sane person would do that but it is better to be prepared for insane users.
I try to create file in storage and for file name I use webview.getTitle() string.
When I save file with name for example "Google.jpg" or "Foreca.jpg" it works well, but...
not all webpage titles is so clear.
For example:
"android - java.io.ioexception: open failed: einval (Invalid argument)
when saving a image to external storage - Stack Overflow:"
There is a lot of wrong characters, if I need this title put in file name.
Is there easy way to replace all this :;?!/<>- characters to ""?
Yes, you can use replaceAll():
String fileName = webview.getTitle();
filename = fileName.replaceAll("(\\p{Punct})","") // fixed \p
Check out the Java documentation for more.
In java.net.url there is a getFile() method and a getPath() method.
In my testing, they both return the same result: the full path and file after the domain name trailing slash.
For instance, http://www.google.com/x/y/z.html returns x/y/z.html for both methods.
Could someone elaborate on the Javadocs?
The URL.getFile() javadocs say this:
Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.
They will be the same unless there is a query string, e.g. a ?somename=value&somethingelse=value2 in the URL.
URL.getFile():
Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.
File, in the general case, is longer
new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getFile();
// returns: "/x/y/z.html?v=1"
, than Path:
new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getPath();
// returns: "/x/y/z.html"
I need to pass the filename of a file-input to a parent page... is this even possible?
The input type ="file" has a value, but can I use it like this?
I have used the value of the file-input to check the extension of the file, and it works!
So I guess there is a value, but can this be done and is it reliable?
If not, how would you have done it?
Thanks
You can only access the file name (without the full path) with javascript in all the browsers except IE.
var filename = document.forms.myForm.elements.myFileInput.value;
will give "file.gif" if the file c:\docs\bah\file.gif was selected