How can I create a directory in BlackBerry internal memory - java

I am trying to create a directory to store my application's files in the BlackBerry's internal memory. Here's the code:
String uri = "file:///store/testapp/";
FileConnection dir;
try {
dir = (FileConnection)Connector.open(uri, Connector.READ_WRITE);
if (!dir.exists()){
dir.mkdir();
}
dir.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
When I run the above I get an IOException with the message "File System Error (12)". Looking this up in the list of BlackBerry constant values this corresponds to "The operation requested is invalid.". Why can't I create the testapp directory?

You can create your own directories only in: "file:///store/home/user/"

You should only create directories in "file:///store/home/user/" or "file:///store/home/samples/" only;
For creating a directory:
public void createDirectory()
{
FileConnection file=null;
try
{
String Path="file:///store/home/user/Abc/"; // or path="file:///store/home/samples/Abc/"
file = (FileConnection)Connector.open(Path);
if(!file.exists())
file.mkdir();
file.close();
}
catch (IOException e)
{
try
{
if(file!=null)
{
file.close();
}
System.out.println("==============Exception: "+e.getMessage());
}
catch (IOException e1)
{
}
}
}
There is different in "file:///store/home/user/Abc/" and "file:///store/home/user/Abc"
If you put like "file:///store/home/user/Abc" then it take the "Abc" as file;
If you put like "file:///store/home/user/Abc/" then it take the "Abc" as directory;

Related

copying directories using .jar program in java

i want to copy directories from the the place where my .jar files exist?
here are what i tried.. but i always get /home/user/
how can i copy files from where my .jar program exist?
private void copy_dir() {
//Path sourceParentFolder = Paths.get(System.getProperty("user.dir") + "/Project/");
// Path sourceParentFolder = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString());
Path destinationParentFolder = Paths.get(System.getProperty("user.home"));
try {
Stream<Path> allFilesPathStream = Files.walk(sourceParentFolder);
Consumer<? super Path> action = new Consumer<Path>() {
#Override
public void accept(Path t) {
try {
String destinationPath = t.toString().replaceAll(sourceParentFolder.toString(), destinationParentFolder.toString());
Files.copy(t, Paths.get(destinationPath));
} catch (FileAlreadyExistsException e) {
//TODO do acc to business needs
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
allFilesPathStream.forEach(action);
} catch (FileAlreadyExistsException e) {
//file already exists and unable to copy
} catch (IOException e) {
//permission issue
e.printStackTrace();
}
}
Try
Path destinationParentFolder = Paths.get(System.getProperty("user.dir"));
"user.dir" gets the absolute path from where your application was initialized.
"user.home" gets the user's home directory.

get the sharedlink of an existing file in Dropbox using the dropBox-sdk-java

I want to retrieve the shared file url of an existing file on Dropbox. I am using the dropbox-java-sdk, and I have managed to create a shared link for a file I just uploaded. The only way I managed to get the shared link of an existing file is by listing all links and get the one I want depending on the path, like so
public void getShareLink(String path) throws DbxApiException, DbxException{
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sharedLinkMetadata = client.sharing().listSharedLinks();
for (SharedLinkMetadata slm: sharedLinkMetadata.getLinks()){
if(slm.getPathLower().equalsIgnoreCase(path)){
System.out.println(slm.getUrl());
return;
}
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
}
Isn’t there a way to directly get the url for the file I want? I just think it is a waste to iterate all items just to get one of them.
Get a ListSharedLinksBuilder from listSharedLinksBuilder and set ListSharedLinksBuilder.withDirectOnly to request only links for the exact path specified:
public String getShareLink(String path) {
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sh = client.sharing().listSharedLinksBuilder()
.withPath(path)
.withDirectOnly(true)
.start();
for (SharedLinkMetadata slm : sh.getLinks()) {
return slm.getUrl();
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
return null;
} catch (DbxException ex) {
System.out.println(ex);
return null;
}
return null;
}

I'm trying to Rename a File using Java But It is Not Working for Some Reason

File oldfile = new File("C:\\NewText Document.txt");
File newfile = new File("C:\\Hello Buddy.txt");
if (oldfile.renameTo(newfile))
{
System.out.println("Rename succesful");
}
else
{
System.out.println("Rename failed");
}
I'm planning on developing it into a file normalizer, but I just want to get this done first.
I've tried using the absolute path, makes no difference. Constantly returning "Rename Failed".
Use move method of Files class. Worked for me ;)
Java doc
If you are using Java 7 then try this:
final File oldfile = new File("C:\\NewText Document.txt");
final File newfile = new File("C:\\Hello Buddy.txt");
final Path source = oldfile.toPath();
final Path dest=newfile.toPath();
try {
Files.move(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
FileChooser();
File oldfile = new File(fileName);
File newfile = new File(fileName.substring(0, 21) + "hello world.txt");
if (!oldfile.exists())
{
try
{
oldfile.createNewFile();
}
catch (IOException ex)
{
System.out.println(ex);
}
}
else
{
if (oldfile.renameTo(newfile))
{
System.out.println("Rename succesful");
}
else
{
System.out.println("Rename failed");
}
}
This is my new code, it works using a file chooser,but currently it only works if i choose the file from my desktop, hence the hardcoded substring.

Error in FileUtils.copyUrlToFile

I am executing the below code to pull the .gz file from a URL to a local directory.For small files it goes through fine but for large files it downloads only part of it but does not fail. I get to know the error only when I try to UNZIP it. Can someone throw any light on this on what could be the reason.
public boolean downloadFilemethod(String filePath, String url, String
decompressFilePath) {
if (StringUtils.isNotBlank(filePath) && StringUtils.isNotBlank(url)) {
try {
FileUtils.copyURLToFile(new URL(url), new File(SRC_BASE_DIR + filePath),
TIMEOUT_IN_MILLIS, TIMEOUT_IN_MILLIS);
ingestmethod(filePath,decompressFilePath);
downloadSuccess = true;
}
catch (MalformedURLException e)
{ LOG.warn("some message"); }
catch (IOException e)
{ LOG.warn("some message);
}
}
return downloadSuccess
}

open pdf file located in a ressource folder

I'm trying to open a pdf located in a ressource folder with my application.
It does work on the emulator but nothing happens when I try on the exported application.
I'm guessing I'm not using the rigth path but do not see where I'm wrong. The getRessource method works very well with my images.
Here is a code snippet :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported()) {
try {
URL monUrl = this.getClass().getResource(pdf);
File myFile = new File(monUrl.toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm referring to the pdf variable this way : "name_of_the_file.pdf"
Edit: I've pasted the whole method
Ok, solved it. The file being located in a Jar, the only way to get it was through a inputsteam/outstream and creating a temp file.
Here is my final code, which works great :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported())
{
InputStream jarPdf = getClass().getClassLoader().getResourceAsStream(pdf);
try {
File pdfTemp = new File("52502HPA3_ELECTRA_PLUS_Fra.pdf");
// Extraction du PDF qui se situe dans l'archive
FileOutputStream fos = new FileOutputStream(pdfTemp);
while (jarPdf.available() > 0) {
fos.write(jarPdf.read());
} // while (pdfInJar.available() > 0)
fos.close();
// Ouverture du PDF
Desktop.getDesktop().open(pdfTemp);
} // try
catch (IOException e) {
System.out.println("erreur : " + e);
} // catch (IOException e)
}
}
You mentioned that it is running on Emulator but not on the application. There is high probability that the platform on which the application is running does not support Desktop.
Desktop.isDesktopSupported()
might be returning false. Hence no stack trace or anything.
On Mac, you can do:
Runtime runtime = Runtime.getRuntime();
try {
String[] args = {"open", "/path/to/pdfFile"};
Process process = runtime.exec(args);
} catch (Exception e) {
Logger.getLogger(NoJavaController.class.getName()).log(Level.SEVERE, "", e);
}

Categories

Resources