Skip to content

Instantly share code, notes, and snippets.

@anta40
Forked from amrishodiq/SaveBytesToFile.java
Last active August 29, 2015 14:06
Show Gist options
  • Save anta40/0676c5a26007d431740e to your computer and use it in GitHub Desktop.
Save anta40/0676c5a26007d431740e to your computer and use it in GitHub Desktop.
public static synchronized String saveToFile(String basePath, byte[] data)
throws IOException {
try {
if (basePath.startsWith("file://")) {
// do nothing
} else if (!basePath.startsWith("/"))
basePath = "file:///" + basePath;
else if (basePath.startsWith("//"))
basePath = "file:/" + basePath;
else
basePath = "file://" + basePath;
FileConnection fconn = (FileConnection) Connector.open(basePath,
Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();
fconn.setWritable(true);
OutputStream outputStream = fconn.openOutputStream();
outputStream.write(data);
outputStream.close();
fconn.close();
return basePath;
} catch (IOException e) {
throw e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment