- Java get short name
- Field Summary
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Field Detail
- MIN_VALUE
- MAX_VALUE
- TYPE
- SIZE
- BYTES
- Constructor Detail
- Short
- Short
- Method Detail
- toString
- parseShort
- parseShort
- valueOf
- valueOf
- valueOf
- decode
- byteValue
- shortValue
- Java Get Short Class Name of a Class using Apache Commons Lang
- How to add Apache Commons Lang 3 library to your Java project
- How to get Short Class Name in Java using ClassUtils class
- How to get short-filenames in Windows using Java?
- How to get short-filenames in Windows using Java?
- Self Answer
- Main Concept
- Solution
- How are Short File Names generated in Windows?
- Get short path with '~' on windows when using java
- Java call for Windows API GetShortPathName
Java get short name
The Short class wraps a value of primitive type short in an object. An object of type Short contains a single field whose type is short . In addition, this class provides several methods for converting a short to a String and a String to a short , as well as other constants and methods useful when dealing with a short .
Field Summary
Constructor Summary
Constructs a newly allocated Short object that represents the short value indicated by the String parameter.
Method Summary
Returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified short value.
Returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.
Methods inherited from class java.lang.Object
Field Detail
MIN_VALUE
public static final short MIN_VALUE
MAX_VALUE
public static final short MAX_VALUE
TYPE
SIZE
public static final int SIZE
BYTES
public static final int BYTES
Constructor Detail
Short
Short
public Short(String s) throws NumberFormatException
Constructs a newly allocated Short object that represents the short value indicated by the String parameter. The string is converted to a short value in exactly the manner used by the parseShort method for radix 10.
Method Detail
toString
parseShort
public static short parseShort(String s, int radix) throws NumberFormatException
- The first argument is null or is a string of length zero.
- The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX .
- Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ ( ‘\u002D’ ) or plus sign ‘+’ ( ‘\u002B’ ) provided that the string is longer than length 1.
- The value represented by the string is not a value of type short .
parseShort
public static short parseShort(String s) throws NumberFormatException
Parses the string argument as a signed decimal short . The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ ( ‘\u002D’ ) to indicate a negative value or an ASCII plus sign ‘+’ ( ‘\u002B’ ) to indicate a positive value. The resulting short value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseShort(java.lang.String, int) method.
valueOf
public static Short valueOf(String s, int radix) throws NumberFormatException
Returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed short in the radix specified by the second argument, exactly as if the argument were given to the parseShort(java.lang.String, int) method. The result is a Short object that represents the short value specified by the string. In other words, this method returns a Short object equal to the value of:
valueOf
public static Short valueOf(String s) throws NumberFormatException
Returns a Short object holding the value given by the specified String . The argument is interpreted as representing a signed decimal short , exactly as if the argument were given to the parseShort(java.lang.String) method. The result is a Short object that represents the short value specified by the string. In other words, this method returns a Short object equal to the value of:
valueOf
Returns a Short instance representing the specified short value. If a new Short instance is not required, this method should generally be used in preference to the constructor Short(short) , as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
decode
public static Short decode(String nm) throws NumberFormatException
Decodes a String into a Short . Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString: Signopt DecimalNumeral Signopt 0x HexDigits Signopt 0X HexDigits Signopt # HexDigits Signopt 0 OctalDigits Sign: — +
DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The Java™ Language Specification , except that underscores are not accepted between digits. The sequence of characters following an optional sign and/or radix specifier (» 0x «, » 0X «, » # «, or leading zero) is parsed as by the Short.parseShort method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String .
byteValue
shortValue
Java Get Short Class Name of a Class using Apache Commons Lang
This Java tutorial shows you how to get the short class name of a Java class which returns the class name without package name using the ClassUtils class of Apache Commons Lang library.
How to add Apache Commons Lang 3 library to your Java project
To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.
implementation 'org.apache.commons:commons-lang3:3.12.0'
To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.
org.apache.commons commons-lang3 3.12.0
To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/
How to get Short Class Name in Java using ClassUtils class
The Apache Commons Lang library provides the method ClassUtils.getShortClassName() to get the short class name minus the package name for a given class.
The code example below to get the short class name from a given Class object.
import org.apache.commons.lang3.ClassUtils; import java.util.Date; public class GetShortClassNameOfClass public static void main(String. args) String result1 = ClassUtils.getShortClassName(ClassUtils.class); String result2 = ClassUtils.getShortClassName(Date.class); System.out.println(result1); System.out.println(result2); > >
The code example below to get the short class name from a full class name in String.
import org.apache.commons.lang3.ClassUtils; public class GetShortClassNameOfClassName public static void main(String. args) String result1 = ClassUtils.getShortClassName("org.apache.commons.lang3.ClassUtils"); String result2 = ClassUtils.getShortClassName("java.util.Date"); System.out.println(result1); System.out.println(result2); > >
The code example below to get the short class name from a given object value.
import org.apache.commons.lang3.ClassUtils; import java.util.Date; public class GetShortClassNameOfObject public static void main(String. args) Double doubleValue = new Double(10.20); Date date = new Date(); String result1 = ClassUtils.getShortClassName(doubleValue, ""); String result2 = ClassUtils.getShortClassName(date, ""); System.out.println(result1); System.out.println(result2); > >
How to get short-filenames in Windows using Java?
Solution 1: Self Answer There are related questions with related answers. Main Concept Main concept lies in calling CMD from Java(tm) by means of the runtime class: Solution Tested on Java SE 7 running on Windows 7 system (Code has been reduced for brevity).
How to get short-filenames in Windows using Java?
Self Answer
There are related questions with related answers. I post this solution, however, because it uses Java(tm) code without the need for external libraries. Additional solutions for different versions of Java and/or Microsoft(R) Windows(tm) are welcome.
Main Concept
Main concept lies in calling CMD from Java(tm) by means of the runtime class:
cmd /c for %I in («[long file name]») do @echo %~fsI
Solution
Tested on Java SE 7 running on Windows 7 system (Code has been reduced for brevity).
public static String getMSDOSName(String fileName) throws IOException, InterruptedException < String path = getAbsolutePath(fileName); // changed "+ fileName.toUpperCase() +" to "path" Process process = Runtime.getRuntime().exec( "cmd /c for %I in (\"" + path + "\") do @echo %~fsI"); process.waitFor(); byte[] data = new byte[65536]; int size = process.getInputStream().read(data); if (size public static String getAbsolutePath(String fileName) throws IOException
I found a slight problem Osmund's solution. It doesn't work properly for this file name for some reason:
N:\directoryY\tmp\temp\asdfasdf sdf dsfasdf [dfadss]\[asdfas] asdfasd asd asdfasdf ~fdfsdfdfdsfdfdfdfdfd~ TTTm7-9 [RR 1234a5678 A.888 OKOK]a
I'm not really sure why exactly. But if you run the command a slightly different way (using ProcessBuilder), it works. Here is the new code (I am using BufferedReader to read the output, it is much cleaner).
public static String getMSDOSName(String path) throws IOException, InterruptedException < Process process = new ProcessBuilder().command("cmd", "/c", "for %I in (\"" + path + "\") do @echo %~fsI").start(); process.waitFor(); try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) < return br.readLine(); >>
This is the output of the original solution vs my solution. The original solution fails to shorten the last path element:
N:\DIRECT~1\tmp\temp\ASDFAS~1\[asdfas] asdfasd asd asdfasdf ~fdfsdfdfdsfdfdfdfdfd~ TTTm7-9 [RR 1234a5678 A.888 OKOK]a N:\DIRECT~1\tmp\temp\ASDFAS~1\_ASDFA~1.888
How to get short-filenames in Windows using Java?, Additional solutions for different versions of Java and/or Microsoft(R) Windows(tm) are welcome. Main Concept. Main concept lies in calling CMD from Java(tm) by means of the runtime class: cmd /c for %I in ("[long file name]") do @echo %~fsI. Solution. Tested on Java SE 7 running on Windows 7 system (Code has been reduced for brevity). Code samplethrows IOException
How are Short File Names generated in Windows?
Microsoft does not document this, but Wikipedia does:
- Example: TEXTFILE.TXT
- Example: TextFile.Txt becomes TEXTFILE.TXT .
- Example: TextFile1.Mine.txt becomes TEXTFI~1.TXT (or TEXTFI~2.TXT , should TEXTFI~1.TXT already exist). ver +1.2.text becomes VER_12~1.TEX .
- Example: TextFile.Mine.txt becomes TE021F~1.TXT .
As Joey mentioned, the undocumented hash of the filename has been reverse engineered.
That's because the very primitive scheme of using a counter and a prefix only works up to a certain number of files. With increasing numbers of files Windows switches to a shorter prefix and a hash. Someone actually reverse-engineered the hash along with a bit of explanation:
- All periods other than the one separating the filename from the extension are dropped - a.testing.file.bat turns into atestingfile.bat.
- Certain special characters like + are turned into underscores, and others are dropped. The file name is upper-cased. 1+2+3 Hello World.exe turns into 1_2_3HELLOWORLD.EXE.
- The file extension is truncated to 3 characters, and (if longer than 8 characters) the file name is truncated to 6 characters followed by ~1. SomeStuff.aspx turns into SOMEST~1.ASP.
- If these would cause a collision, ~2 is used instead, followed by ~3 and ~4.
- Instead of going to ~5, the file name is truncated down to 2 characters, with the replaced replaced by a hexadecimal checksum of the long filename - SomeStuff.aspx turns into SOBC84~1.ASP, where BC84 is the result of the (previously-)undocumented checksum function.
What is the best way to generate a unique and short file, Well, you could use the 3-argument version: File.createTempFile(String prefix, String suffix, File directory) which will let you put it where you'd like. Unless you tell it to, Java won't treat it differently than any other file. The only drawback is that the filename is guaranteed to be at least 8 characters long (minimum of 3 …
Get short path with '~' on windows when using java
You can't (and you don't) use ~ for the home folder (that is a shell expansion). You can use System.getProperty("user.home") - which will return the home folder.
How are Short File Names generated in Windows?, I know that Windows uses the following short filename convention: Cut the name to 6 characters (without extension) Append the tilde ( ~) Append an unsigned integer number which indicates the match index (starting with 1) Append the original file extension. Thus, the file name C:\abcdefghijklmn.txt should be accessible …
Java call for Windows API GetShortPathName
Thanks for hint. Following is my improved function. It uses Unicode version of the GetShortPathName
import com.sun.jna.Native; import com.sun.jna.platform.win32.Kernel32; public static String GetShortPathName(String path)
Get short path with '~' on windows when using java, Get short path with '~' on windows when using java. Ask Question Asked 4 years, 9 months ago. Active 4 years, 8 months ago. Viewed 708 times 1 1. I have created a java application which downloads files from an online source to my local computer which runs windows 7. The code downloads the …