Android Studio code writing a local file on OSX - java

Running in the Debug mode, with phone connected "not emulator", this Android Studio code tries to create/write a file on the local Mac machine running El Capitan but it gives error:
java.io.IOException: open failed: ENOENT (No such file or directory)
try {
Log.d("TAG", "trying to write to file");
String path = "/Users/myName/Desktop/sss.html";
File file = new File(path);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write in file
bw.write(response);
// close connection
bw.close();
Log.d("TAG", "finish file writing");
} catch (Exception e) {
Log.e("TAG", e.toString());
}
I don't know how to create a file on the local developing machine and would appreciate some how to. Thanks
edit
As stated in the comments. If the path is pointing to a file on the phone, How can I adjust it so that it points to a file on the developing machine?

You can't make or write to a file from a phone/emulator on a devoloping machine without some form of network.
These are 2 different machines and are completely independent.
You can always make a file on the emulator and send it to your developing machine using some kind of network.

Related

desktop.open in Java opens .bat file instead of a folder

File file = new File ("D:\\Folder\\Folder2\\");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
The following code supposed to open Folder2, but instead it opens D:\Folder\Folder2.bat file.
How to fix that?
Opening folder through Deskopt.open() would be delegated to Desktop.browseFileDirectory() (JDK 8/9)
But, as seen in JDK-8233994, that is not supported/implemented for Windows.
So the alternative with explorer.exe is indeed the recommended way:
Process p = new ProcessBuilder("explorer.exe", "/select,D:\\Folder\\Folder2").start();
The Desktop API describes the method open() clearly:
Launches the associated application to open the file. If the specified
file is a directory, the file manager of the current platform is
launched to open it.
Or see the reply of #vonC

Read only file-system error while downloading file from FTP in Android

Im trying to download file from FTP in android using Apache Commons library and Im always getting Error EROFS (Read only file-system). I tried to make directory on the server instead, using ftpClient.makeDirectory("xxx") and that works completely fine. I also tried to do this without using passive mode and I made sure that path to file is right but with the same result. Using total comander - everything works so its not the permission issue. Thanks for reply.
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(xxx);
ftpClient.connect(InetAddress.getByName("xxx",xxx);
ftpClient.login("xxx", "yyy");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
FileOutputStream buffIn = null;
buffIn = new FileOutputStream(new File("/file")); //here I get the crash
ftpClient.retrieveFile("file", buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
new File("/file"). That is not possible as that would be in the root of the filesystem. So your path is wrong. You should first decide where to save the file. You can choose from internal, external or removable memory. Should it have the name 'file'?

File upload from a web-based application to a linux server

It seems that my code won't work after learning that the machine that I'll be pointing the upload path to is a Linux box.
My use case is, a user logs in to the web app, chooses a file to upload, then click upload button. Is it possible to do this direct from the Java code to the Linux server using appropriate ssh or scp libraries if there is any?
EDIT: Here's my current code.
#Override
public void fileTransfer(File uploadedFile, String fileName, String pathTemp) {
File destFile = new File( pathTemp + File.separator + fileName);
try{
FileUtils.copyFile(uploadedFile, destFile);
String getTempFile = destFile.toString();
String tempPath = getTempFile.replace("\\", "\\\\");
File tempFile = new File(tempPath); // 1st file
String tempFileName = tempFile.getName();
String fileSave = getUploadPathSave().replace("\\", "\\\\");
tempFile.renameTo(new File(fileSave + tempFileName));
} catch (IOException ex) {
System.out.println("Could not copy file " + fileName);
ex.printStackTrace();
}
}
If your app is deployed at one place only (not mass distribution), the easiest way would be:
create samba share on linux machine
map samba share to logical drive on windows machine
do usual file copy with java functions.
attention: renameTo will not work between drives. You'll need to copy input stream to output stream or, better, use apache commons-io functions for that.
There are different possibilities:
If you can create a shared directory in linux and mount it under windows (see Samba. Then you can write to that directory like a local directory. File will go to the linux server.
Use a library like Jsch to upload the file from windows server to linux server.
There are certain things you can do:
1-> If you can program your linux server, then you can make a program that listens to user request on a port, and stores data in file. Then you can send you files to that port of server.
2-> The other way is you can use some sort of script to create ssh-connection to server and then you can just add file through ssh, but here your java program will not be useful.
I personally use my own program to share files between 2 machines in same network.
You can use it,if it will be useful for you: https://github.com/RishabhRD/xshare

How to download file from one Amazon EC2 instance to another EC2 instance using java apache FTPClient

I have setup a ftp server in one amazon EC2 instance and can download file from that server in my local machine using apache ftp library. Now I am trying to run the same ftpclient program in another Amazon EC2 instance, but it is not working.
Here is the sample code to download a file from ftpsever:
FTPClient ftp = new FTPClient();
String loc = "/home/ubuntu/test/";
String remote = "/home/ftp";
try
{
ftp.connect("ec2-xx-xx-xx-xxx.compute-1.amazonaws.com", 21);
ftp.login("username", "xxx");
System.out.println("connected..");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
OutputStream output;
output = new FileOutputStream(loc+"file_name");
ftp.retrieveFile(remote+"/filen_name", output);
output.close();
ftp.disconnect();
}
catch(Exception ex)
{
ex.printStackTrace();
}
The following code can list all the file names of the remote directory:
for(FTPFile f: ftp.listFiles(remote))
{
System.out.println(f.getName());
}
Both of the code work fine in my local machine but is not working in amazon machine. It connects but cannot list the file name or download file. And it doesn't show any error message.
Thanks in advance--
I just found a possible answer, because I have been having the same problem for a while.
It solved the problem to me.
You have to add this line to your code:
ftp.enterLocalPassiveMode();
after connect() and before login().
Here is the link:
http://bartling.blogspot.dk/2012/04/using-apache-commons-net-ftpclient-on.html

Eclipse FileInputStream with local development and remote file

I'm developing a java web app on my local PC that uses a file on the deployment server. I have to do something like this to get it to work both locally during development and on the server when deployed. Is this standard practice? Or is there a way in Eclipse using linked folders and files to specify the file the same as it is referenced on the server?
InputStream in = null;
try {
in = new FileInputStream("/EBWEB/www/homepages/path/to/file/STKCore.properties");
} catch (java.io.FileNotFoundException ex) {
in = new FileInputStream("W:/internal/www/homepages/path/to/file/STKCore.properties");
}
W: is how I have the remote server mapped using Samba in Windows XP on my PC.
You can put your properties file in your war and open it with a getResourceAsStream().

Categories