I have a Groovy script where I am downloading a file from a url to my local machine like so..
URL url = new URL("http://i.imgur.com/pszAeGh.png")
HttpURLConnection urlConn = url.openConnection()
File pic = new File('/Users/me/Downloads', 'myimage.jpg')
pic.append(urlConn.getInputStream())
If the 'myimage.jpg' doesn't exist in the Downloads folder, I want the script to create it. How can I specify that? Right now the 3rd line gives
java.io.FileNotFoundException: /Users/me/Downloads/myimage.jpg (No such file or directory)
Some other posts for Java have suggested using the createNewFile method but Groovy is failing even when it tries to create the file.
You should just be able to do:
new File('/Users/me/Downloads', 'myImage.gif').withOutputStream { os ->
os << new URL("http://i.imgur.com/pszAeGh.png").openStream()
}
Assuming /Users/me/Downloads exists
Related
If file is dragged and dropped from file explorer it has FileAttributes.ReadOnly flag for StorageFile.Attributes parameter. In that case using StorageFile api to write to file will give error. How to write to file in this case??
In this case PathIO api can be used to write to file (unless file is a system file). Convert data to write into bytes array and then add following code to write to file:
await PathIO.WriteBytesAsync(file.Path, bytes);
This will write to these files without any error. You don't need any additional permission like broadFileSystemAccess for this.
From the screenshot below: I did a command-C copy of a scala source file - seen in the screenshot as BpmSpecs . Then went to the containing folder eventapi , right clicked and selected paste.
Well .. Intellij did a rather boneheaded thing here: it created a new directory called .. wait for it .. LocalEngineCycles.scala .. and proceeded to copy the original file into that directory under the same name as the original file.
So .. how to copy a scala source file into the same directory - and having intelllij actually help us by allowing us to supply the new filename ?
Re: possible duplicate: it is not. This problem relates to a File. I clone classes successfully all the time: IJ asks me to change the ClassName and I do it. For the file it gets confused and instead creates a new directory and places a copy of the original file inside this new directory.
is it possible to load module from file with extension other than .lua?
require("grid.txt") results in:
module 'grid.txt' not found:
no field package.preload['grid.txt']
no file './grid/txt.lua'
no file '/usr/local/share/lua/5.1/grid/txt.lua'
no file '/usr/local/share/lua/5.1/grid/txt/init.lua'
no file '/usr/local/lib/lua/5.1/grid/txt.lua'
no file '/usr/local/lib/lua/5.1/grid/txt/init.lua'
no file './grid/txt.so'
no file '/usr/local/lib/lua/5.1/grid/txt.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './grid.so'
no file '/usr/local/lib/lua/5.1/grid.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
I suspect that it's somehow possible to load the script into package.preaload['grid.txt'] (whatever that is) before calling require?
It depends on what you mean by load.
If you want to execute the code in a file named grid.txt in the current directory, then just do dofile"grid.txt". If grid.txt is in a different directory, give a path to it.
If you want to use the path search that require performs, then add a template for .txt in package.path, with the correct path and then do require"grid". Note the absence of suffix: require loads modules identified by names, not by paths.
If you want require("grid.txt") to work should someone try that then yes, you'll need to manually loadfile and run the script and put whatever it returns (or whatever require is documented to return when the module doesn't return anything) into package.loaded["grid.txt"].
Alternatively, you could write your own loader just for entries like this which you set into package.preload["grid.txt"] which finds and loads/runs the file or, more generically, you could write yourself a loader function, insert it into package.loaders, and then let it do its job whenever it sees a "*.txt" module come its way.
I have about 200 folders with X images in each of them.
I have a master script in the root folder that does some stuff to the images.
Each folder has some variables specific to it and its contents.
I want my master script, when it parses folder Y, load some sort of a config file from within folder Y to get those variables, then when folder Z is to be parsed, load the config file from that one.
I know of #include "config.jsx" that I use at the moment to load it but its at the beginning of the script, I need something dynamic and doesn't need to be a jsx at all.
I store all my parameters in xml format and read that in using the XML objects in extendscript. As long as your parameters file is always named something like 'config.xml' it is easily located.
var file = new File( /c/folder/file.xml );
file.open("r");
var str = file.read();
var xml = new XML(str);
I'm trying to create a 'valid' tar.gz archive by using the apache commons compress librarys. The created archive will be read by an embedded device and has to be in the same format with the same file permissions i think.
If i'm using Linux to create my file, everything works fine, but if i'm using Windows. The file is rejected.
As you can see, the archive only contains to special files with unix permissions. these are correctly set and if i use a "working" tar file and run it through gzip, the created tar.gz also works fine.
The only difference i figured out is, that the non-working tar file is slightly larger (61 instead of 56 kb) and 7zip shows under "Host OS" FAT instead of Unix.
Any ideas, how i can create a "real" tar archive from Windows?
Thanks in advance!
My current sourcecode is:
public static void compress(File configTar, File rcConf, File databaseTxt)
throws ArchiveException, IOException {
OutputStream tarFileStream = new GZIPOutputStream(new FileOutputStream(configTar));
InputStream rcConfStream = new FileInputStream(rcConf);
InputStream databaseTxtStream = new FileInputStream(databaseTxt);
ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.TAR, tarFileStream);
TarArchiveEntry databaseTxtEntry = new TarArchiveEntry(databaseTxt);
TarArchiveEntry rcConfEntry = new TarArchiveEntry(rcConf);
databaseTxtEntry.setName("database.txt");
databaseTxtEntry.setGroupName("root");
databaseTxtEntry.setUserName("root");
databaseTxtEntry.setMode(convertModeFromString("rwxr-xr-x"));
archiveOutputStream.putArchiveEntry(databaseTxtEntry);
IOUtils.copy(databaseTxtStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
rcConfEntry.setName("rc.conf");
rcConfEntry.setGroupName("root");
rcConfEntry.setUserName("root");
rcConfEntry.setMode(convertModeFromString("rw-rw-rw-"));
archiveOutputStream.putArchiveEntry(rcConfEntry);
IOUtils.copy(rcConfStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
archiveOutputStream.finish();
rcConfStream.close();
databaseTxtStream.close();
tarFileStream.close();
}
I've done some research and noticed a difference between the TAR headers. Can anybody tell, what i am doing wrong?
Working example of file 1:
http://i.stack.imgur.com/S8Rbi.jpg
NON-Working example of file 1:
http://i.stack.imgur.com/bdc9T.jpg
Working example of file 2:
http://i.stack.imgur.com/rYhr9.jpg
NON-Working example of file 2:
http://i.stack.imgur.com/4wHw3.jpg
You need to use different constructor. I you check JavaCode TarArchiveEntry
new TarArchiveEntry(file) will end up into new TarArchiveEntry(file, file.getPath())
so, if you use new TarArchiveEntry(file, file.getName()) that will make it flat