Created
September 27, 2011 03:47
-
-
Save amrishodiq/1244294 to your computer and use it in GitHub Desktop.
Blackberry - Get file contents
This file contains 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
/** | |
* Method to get all bytes from a file. | |
* | |
* @param path | |
* @return | |
* @throws IOException | |
*/ | |
public static byte[] getFileContent(String path) throws IOException { | |
FileConnection file = null; | |
if (path.startsWith("file://")) | |
path = path.substring(7); | |
else if (!path.startsWith("/")) | |
path = "/" + path; | |
try { | |
file = (FileConnection) Connector.open("file://" + path, | |
Connector.READ); | |
int fileSize = (int) file.fileSize(); | |
if (fileSize > 0) { | |
byte[] data = new byte[fileSize]; | |
InputStream input = file.openInputStream(); | |
input.read(data); | |
Thread.yield(); | |
input.close(); | |
return data; | |
} else { | |
throw new NullPointerException("File " + path + " is empty."); | |
} | |
} catch (IOException e) { | |
throw e; | |
} finally { | |
if (file != null && file.isOpen()) | |
file.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment