I would like to know how to get a YAML file in dropbox and store it as a YamlConfiguration Object in Java. This is for a Bukkit plugin, so the Plugin object is part of the API. The code I have right now is only local, here it is:
private File cfile;
private FileConfiguration config;
private Plugin p;
//setup
public void setup(Plugin p){
this.p = p;
cfile = new File(p.getDataFolder(), "punishments.yml");
config = YamlConfiguration.loadConfiguration(cfile);
config.save(cfile);
}
How would I get this file from dropbox, and how would I reupload it with updated information? https://www.dropbox.com/s/hv8yhz0grci8xpl/punishments.yml
Thanks
Not sure I understand the Bukkit API, but if you're looking to download a file, I wrote this for you:
public static boolean downloadFile(String urlStr, String fileStr)
{
boolean success = true;
InputStream urlStream = null;
BufferedInputStream iStream = null;
FileOutputStream fOutput = null;
try
{
URL url = new URL(urlStr);
urlStream = url.openStream();
iStream = new BufferedInputStream(urlStream);
fOutput = new FileOutputStream(new File(fileStr));
byte[] buffer = new byte[512];
while(iStream.read(buffer) != -1)
{
fOutput.write(buffer);
}
} catch (IOException ioe)
{
success = false;
ioe.printStackTrace();
}
finally
{
if(fOutput != null)
try
{
fOutput.close();
} catch (IOException e)
{
e.printStackTrace();
}
if(iStream != null)
try
{
iStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
if(urlStream != null)
try
{
urlStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
return success;
}
You can run it via downloadFile("www.urlhere.com/data.dat", "FileToSaveAs.dat"), and it will return true if it succeded and false if it failed. Be aware that it will overwrite the existing file if it exists, and if the connection breaks halfway through, you'll be left with a broken file.
As for updating the file, you have 3 choices:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.