I was looking for a way to download a file from a website. I've seen this question (How to download and save a file from Internet using Java?) already, but I was wondering if you could clear two things up for me. First, let's say the link to the file is "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip". I would insert the link in the URL constructor:
String url = "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip";
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
but what needs to be inserted in the FileOutputStream constructor? And the second thing is, where would fos.getChannel().transferFrom() save the file to? Or do I need to do another step to save the file to my hard drive?
Anyway, thanks for reading.
In answer to your first question, you put the filename of where you want to save the file. See the docs here.. And I think that answers your second question, since the string is where you want to save the file.
Just remember if you use a relative path, the file will save where the application is executed, and you also need to make sure you have write access to that directory.
Related
This is my first hands on using Java Spring boot in a project, as I have mostly used C# and I have a requirement of reading a file from a blob URL path and appending some string data(like a key) to the same file in the stream before my API downloads the file.
Here are the ways that I have tried to do it:
FileOutputStream/InputStream: This throws a FileNotfoundException as it is not able to resolve the blob path.
URLConnection: This got me somewhere and I was able to download the file successfully but when I tried to write/append some value to the file before I download, I failed.
the code I have been doing.
//EXTERNAL_FILE_PATH is the azure storage path ending with for e.g. *.txt
URL urlPath = new URL(EXTERNAL_FILE_PATH);
URLConnection connection = urlPath.openConnection();
connection.setDoOutput(true); //I am doing this as I need to append some data and the docs mention to set this flag to true.
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("I have added this");
out.close();
//this is where the issues exists as the error throws saying it cannot read data as the output is set to true and it can only write and no read operation is allowed. So, I get a 405, Method not allowed...
inputStream = connection.getInputStream();
I am not sure if the framework allows me to modify some file in the URL path and read it simultaneously and download the same.
Please help me in understanding if they is a better way possible here.
From logical point of view you are not appending data to the file from URL. You need to create new file, write some data and after that append content from file from URL. Algorithm could look like below:
Create new File on the disk, maybe in TMP folder.
Write some data to the file.
Download file from the URL and append it to file on the disk.
Some good articles from which you can start:
Download a File From an URL in Java
How to download and save a file from Internet using Java?
How to append text to an existing file in Java
How to write data with FileOutputStream without losing old data?
I would read txt content from web, I know how to read a local file but I want to put my txt file in a website and read it in my application. I used this code :
BufferedReader reader = new BufferedReader(new InputStreamReader((new URL("MY TXT URL")).openStream()));
String line = reader.readLine();
int k =0;
String[] SaveLine = new String[20];
for( k = 0; line!=null; k++)
{
System.out.println(reader.readLine());
line=reader.readLine();
}
There is a problem, with this code I read the entire webpage, so the HTML is included. How can I just read the file content ? There is a specified website where I could put my txt files for read them?
Thanks in advance!
If this "MY TXT URL" is indeed your TXT file's URL
(for example http://www.test.com/file10.txt), then your
code will read the TXT file itself, and not any web page (as you say it does).
So you're mixing something here.
Whatever URL you give to it, that web resource (file) it is going to read.
This is a very confusing problem. Try this. Make the URL include the location and name of the textfile; here is an example: www.yourwebsiteurl.com/yourtextfile.txt. Using that method, ONLY the textfile, not including HTML, will be read.
If the endpoint of your URL is a text file, if shouldn't be displaying any html (unless that's what's contained in your text file.)
allegedly uploadedit works though I haven't used it myself.
Thanks to all, I tested my code in a txt uploaded on DropBox and Dropbox probably has code system that I don't know, infact my code returns html/script and not the content of txt. So probably the correct ask is if anybody know a website where can I put my txt file just to reading it? (the url must don't change for ever)
EDIT: PROBLEM SOLVED
I put my file in a website to just read the file and don't read it in a html box (such as a lot of cloud website)
I try to write a text to a file and read this text later. When I use FileWriter I become a NullPointerException?
Is that a permission problem or ...? I also try the PrintWriter but I see the same Exception
.
This my code:
FileWriter fw = new FileWriter(new File("file.file"));
fw.write("XYZ");
best regards
londi
I guess your problem is that you use a relative file path, but that the origin of the relative path is not the one you think.
First of all, try to use an absolute path, that would be, on linux-like machines something like /home/me/myCode/myfile.txt or on windows something like c:/some/path/myfile.txt
Another thing you can do, in order to know what happens is print the origin.
File origin = new File(".");
System.out.println(origin.getAbsolutePath());
Once you know where the origin is, you can see what you need in order to get to your file.
Hope it will help.
Sounds like a permission issue. On iOS your application lives within a security sandbox, so you cannot just randomly read and write files anywhere you want. You could either use File.createTempFile to create a temp file somewhere hidden you your sandbox where nothing else can see it, or use the native api to determine where to dump your files. The following example will give you a file reference to the Documents Directory folder:
NSArray nsa = NSFileManager.defaultManager().URLsForDirectory$inDomains$(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask);
NSURL nsu = (NSURL)nsa.getFirst();
String snsu = nsu.getAbsoluteString() + "MyNewDocument.pdf";
File newFile = new File(new URI(snsu));
I'm writing a swing application, but I'm sure I'll think of more to add to it later, so I would like a way to download the file from dropbox if its new. I've tried a lot of different things, but all they give me are the page's HTML. Anyone know how to do this? I sure don't.
In my opinion, the Dropbox API is far too complicated for what you need.
It's actually extremely simple to download a file from dropbox.
The first step is to put the file that you want to download somewhere inside your dropbox's Public Folder.
Next you want to right click that file and choose "copy public link." You can do this from the web interface or even right there in your computer-sync-folder-thing. This will give you a unique download url for the file.
Next, use this code:
String url="https://dl.dropboxusercontent.com/u/73386806/Prune%20Juice/Prune%20Juice.exe";
String filename="PruneJuice.exe";
try{
URL download=new URL(url);
ReadableByteChannel rbc=Channels.newChannel(download.openStream());
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.getChannel().transferFrom(rbc, 0, 1 << 24);
fileOut.flush();
fileOut.close();
rbc.close();
}catch(Exception e){ e.printStackTrace(); }
Of course, change the value of the url string to your own download url, and the value of filename to whatever you want to save the file as.
Now, if this fails, you may need to change the url from https:// to http://, but either way it should still work. Dropbox is cool like that.
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
When trying to delete one of those files, this is what I use, but it's returning false.
String tag = v.getTag().toString();
File file = new File(System.getProperty("user.dir")+"/"+tag);
String s = new Boolean (file.exists()).toString();
Toast.makeText(getApplicationContext(), s, 1500).show();
file.delete();
How can I overcome this problem?
Use getFileStreamPath(FILENAME) to find your file. From the docs:
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.
Your current working directory.
To help diagnose the problem, use file.getAbsolutePath() to see the full path.
It could also be a permissions problem, if you're trying to delete from another application. If so, you may need to change to MODE_WORLD_WRITEABLE (insecure), or restructure your code so the create and delete are called by the same app.
EDIT: That was mostly incorrect. I didn't realize that openFileOutput didn't use the current working directory.
Use same contents as 'FILENAME' variable in your first snippet in the second snippet while trying to delete.
String RootDir = Environment.getExternalStorageDirectory()
+ File.separator + "Video";
File RootFile = new File(RootDir);
RootFile.mkdir();
FileOutputStream f = new FileOutputStream(new File(RootFile, "Sample.mp4"));
i used this code to save the video files to non-default location. Hope this will be useful to you.By default it is storing in sd card
For each application the Android system creates a "data/data/package of the application" directory.
Files are saved in the "files" folder under this directory
to change the default directory the above code will be used
the default working directory can be displayed using fileobject.getAbsolutePath()