Created
June 7, 2014 15:20
-
-
Save alibitek/3b6266bc5d24ddaeda04 to your computer and use it in GitHub Desktop.
Normalize white space in filepath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedInputStream; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| import java.nio.charset.Charset; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| public class TestWhiteSpace { | |
| public static void main(String[] args) throws IOException { | |
| Path path = Paths.get("/home/alex/Music/./Alexander Blu/Alexander_Blu_-_Electro_maniac.mp3"); | |
| System.out.println("Absolute path: " + path.toAbsolutePath()); | |
| System.out.println("URI: " + path.toUri()); | |
| System.out.println("Normalized Path: " + path.normalize()); | |
| System.out.println("Normalized URI: " + path.normalize().toUri()); | |
| try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(path.toFile()))) { | |
| byte[] data = new byte[(int) path.toFile().length()]; | |
| int bytesRead = br.read(data); | |
| System.out.println("Bytes read: " + bytesRead); | |
| String encodedData = new String(data, Charset.defaultCharset()); | |
| System.out.println("Encoded data: " + encodedData); | |
| } | |
| } | |
| } | |
| /* Output: | |
| Absolute path: /home/alex/Music/./Alexander Blu/Alexander_Blu_-_Electro_maniac.mp3 | |
| URI: file:///home/alex/Music/./Alexander%20Blu/Alexander_Blu_-_Electro_maniac.mp3 | |
| Normalized Path: /home/alex/Music/Alexander Blu/Alexander_Blu_-_Electro_maniac.mp3 | |
| Normalized URI: file:///home/alex/Music/Alexander%20Blu/Alexander_Blu_-_Electro_maniac.mp3 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment