This question already has answers here:
How can I download and save a file from the Internet using Java?
(23 answers)
Closed 4 years ago.
I want to add a URL into my java program: http://www.markit.com/news/InterestRates_JPY_20160426.zip; so basically when you open this link a zip file is downloaded. How do I do that?
And then, I want to unzip the downloaded file in the java program as well.
How do I do these in java?
You can use zip4j to unzip your file.
To download a file in Java you can use this code.
try
{
String url = "download url";
String path = "C:/Users/...."; // Path to where the files is going to be downloaded.
ReadableByteChannel in = Channels.newChannel( new URL(url).openStream() );
FileOutputStream fileOutputStream = new FileOutputStream(path);
FileChannel out = fileOutputStream.getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
}
catch (Exception e)
{
e.printStackTrace();
}
Related
This question already has answers here:
How to handle ~ in file paths
(5 answers)
Closed 3 years ago.
File f = new File("~/NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
cat ~/NetBeansProjects/ChatApp/src/chatapp/Server.java, prints the content of the file.
But the above program doesn't print "File exist".
The ~ is resolved by the shell, whereas Java do not resolve it. Try something like this:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
The "home" wildcard (~) cannot be resolved in the JVM. You need to load that property via the Java API:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
This question already has answers here:
Can't read and write a TIFF image file using Java ImageIO standard library
(5 answers)
Closed 5 years ago.
I'm trying to read an image from a relative path:
String fp = "../resources/img/wc/text/039.tiff";
The following code succeeds:
File fi = new File(getClass().getResource(fp).getPath());
System.out.println("fi: " + fi);
if (fi.exists() && !fi.isDirectory()) {
System.out.println("file exists"); // <-- console prints this
}
try {
img = ImageIO.read(getClass().getResource(fp));
System.out.println("file read"); // <-- console prints this
} catch (IOException e) {
e.printStackTrace();
}
... but the following code just after it:
System.out.println(img.getType());
... fails, reporting:
Exception in thread "main" java.lang.NullPointerException
at com.ddc.fmwscanner.java.LoadImageApp.ddNextImage(LoadImageApp.java:60)
at com.ddc.fmwscanner.java.LoadImageApp.<init>(LoadImageApp.java:85)
at com.ddc.fmwscanner.main.FmwScanner.main(FmwScanner.java:15)
I know the image is valid, because I can open it using non-Java methods. However, those methods will not open the image from a .jar, so I need to use a pure Java method.
Any insight is appreciated.
This ended up being a problem with loading .tiff files in pure Java. Installing TwelveMonkeys ImageIO plugin did the trick. Thanks again, especially to #IlarioPierbattista, who directed me to the solution!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java: Check if file is already open
I am making a swing utility in which i am creating and using a excel sheet by Apache poi.
If the file is already opened while i m trying to access it it throws an exception like some other process is using this excel file. So all i want is to check if that excel file is already opened and if yes then close it.
Did you do some Google search on this topic: Here is some I came up with
Java: Check if file is already open
check if a file is already open before trying to delete it
checking that an excel file is already opened by another application
I am copying the answer from the first question on stackoverflow here:
File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Get an exclusive lock on the whole file
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
// Ok. You get the lock
} catch (OverlappingFileLockException e) {
// File is open by someone else
} finally {
lock.release();
}
Hope this will be of help.
Have you tried this:
File file = new File(pathToFile);
if (file.canRead()) {
<do whatever you are doing with the file>
}
This question already has answers here:
Android How to use MediaScannerConnection scanFile
(8 answers)
Closed 8 years ago.
Im working on Android. My android application successfully creates a text file in SD card memory; the created text file can be seen in the DDMS explorer but cannot be seen in the windows explorer. Here is my code:
private void initFile(String filename, char[] data, int length){
File File = new File(Environment.getExternalStorageDirectory() + File.separator + "Download" + File.separator + filename);
try {
File.delete();
File.createNewFile();
FileOutputStream fOut = new FileOutputStream(mFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
if(mFile.exists())
{
myOutWriter.write(data,0,length);
myOutWriter.flush();
myOutWriter.close();
fOut.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Note: But if I restart the android the created text file can be seen already in the windows explorer.
You have any idea why is it so? Why the file cannot be seen in the windows explorer when it is created in android when it can be seen in the DDMS?
Im using real android tablet.
You have to pull the file from DDMS File Explorer to Windows File Explorer. You have to select the file on the DDMS File Explorer and then click the button pull a file from the device present at the right side of the DDMS File Explorer Window and save it in any of the folder on your wish. After that you can see it in Windows FileExplorer
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java
I want to write my results to the desktop of the user rather than to the same directory as file class that I am running.
I am using Mac OS.. How about in Window?1
Thanks
The user's home directory is:
System.getProperty("user.home")
In general +"/Desktop" would do, but is not portable.
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
...
} finally {
out.close();
}
This would write the file "mytext.txt" to the home directory.