I can't figure out what an InputStream is in the context of the clojure-xml documentation. The clojure-xml documentation lists the inputs to clojure-xml/parse as a "File, InputStream, or String naming a URL".
I tried:
(defn open-file
"Attempts to open a file and complains if the file is not present."
[file-name]
(let [file-data (try
(slurp file-name)
(catch Exception e))]
file-data))
(clojure-xml/parse (utl/open-file "test.xml"))
and receive this error:
FileNotFoundException /home/cnorton/projects/clojure/xml-lib/<
(No such file or directory) java.io.FileInputStream.open
(FileInputStream.java:-2)
but this works:
(clojure-xml/parse "test.xml")
Why wouldn't an InputStream be considered the result of opening a file? Therefore, what is an InputStream in this context?
InputStream refers to a java.io.InputStream. The easiest way to get one in Clojure is by using clojure.java.io/input-stream.
clojure.xml/parse should accept anything that clojure.java.io/input-stream would. Examples:
(require '[clojure.xml :as xml])
(require '[clojure.java.io :as io])
(xml/parse "/Users/bsmith/.m2/settings.xml")
(xml/parse (io/input-stream "/Users/bsmith/.m2/settings.xml"))
The difference between "/Users/bsmith/.m2/settings.xml" and a "File name":
"/Users/bsmith/.m2/settings.xml" is a String which (happens) to specify an (absolute) path to a file on my file system. In the Java world, however, the class java.io.File is the idiomatic way to represent a file path. clojure.java.io does't care, it will accept a String naming a file or a java.io.File naming a file.
The error message you got in your initial post came because you first loaded the XML into a String using slurp. (Not a good idea since XML carries its own encoding with it, which slurp is not able to interpret.) In any event, you then passed the String on and clojure.java.io ultimately tried to interpret the actual content of the XML as a file path, which clearly can't work.
Related
I am familiar with Ruby and am trying to write a program in Crystal.
I have a file called special_file.txt that I want to read in my Crystal program, how do I do that?
Crystal is inspired by Ruby syntax and so you can often read and perform File operations in a similar manner. For example, Crystal has a File classclass which is an instance of the IO class containing a read method.
To read a file's contents on your filesystem you can instantiate a File object and invoke the gets_to_end method coming from the IO super class:
file = File.new("path/to/file")
content = file.gets_to_end
file.close
The gets_to_end method reads an entire IO objects data to a String variable.
You can also use a block invocation to achieve a similar result:
# Implicit close with `open`
content = File.open("path/to/file") do |file|
file.gets_to_end
end
Finally, the most idiomatic way to read the contents of an entire file would be the one line:
# Shortcut:
content = File.read("path/to/file")
I would like to save a vibe.d stream such as HTTPClientResponse.bodyReader (of type InterfaceProxy!InputStream), but also other potential vibe.d streams to a file, how do I best do that in a memory efficient way without copying all data to RAM?
In general for downloading files using a HTTP client you can use the vibe.inet.urltransfer package which offers a download convenience function which performs a HTTP request, handles redirects and stores the final output to a file.
download(url, file);
However if you want to take a raw input stream (for example when not handling redirects) you can use vibe.core.file : openFile to open/create a file as file stream and then write to that.
To then write to the file stream you've got two options:
Either you directly call file.write(otherStream)
Otherwise you can use vibe.core.stream : pipe
Directly calling write on the FileStream object is what is being used inside the vibe.d urltransfer module and is also recommended for files as it will read directly from the stream into the write buffer instead of using an additional temporary buffer which pipe would use.
Sample:
// createTrunc creates a file if it doesn't exist and clears it if it does exist
// You might want to use readWrite or append instead.
auto fil = openFile(filename, FileMode.createTrunc);
scope(exit) fil.close();
fil.write(inputStream);
I want to read TGA file to BufferedImage. How I can do it without libraries?
Now there is function:
(defn load-image [filename]
(ImageIO/read (File. filename)))
This function read jpeg file successfully, but return nil instead of BufferedImage for TGA file.
The easiest thing would be still to use a library, for example TwelveMonkeys. In your project.clj (if you are using Leiningen), add:
{:dependencies [... [com.twelvemonkeys.imageio/imageio-tga "3.4.1"]]}
Then, in the code:
(ImageIO/scanForPlugins)
(defn load-image [filename]
(ImageIO/read (File. filename)))
This will work for valid TGA files.
I currently have some code reading files which are not compressed, it uses the following approach to read a file in C++
FILE* id = fopen("myfile.dat", "r");
after obtaining id, different parts of the code access the file using fread, fseek, etc.
I would like to adapt my code so as to open a gzip version of the file, e.g. "myfile.dat.gz" without needing to change too much.
Ideally I would implement a wrapper to fopen, call it fopen2, which can read both myfile.dat and myfile.dat.gz, i.e. it should return a pointer to a FILE object, so that the remaining of the code does not need to be changed.
Any suggestions?
Thank you.
PS: it would be fine to decompress the whole file in memory, if this approach provides a solution
zlib provides analogs of fopen(), fread(), etc. called gzopen(), gzread(), etc. for reading and writing gzip files. If the file is not gzip-compressed, it will be read just as the f functions would. So you would only need to change the function names and link in zlib.
all. I am trying to write a C++ program that will iterate through a user-specified directory (e.g. /home/alpernick/Pictures). Primarily, this is to ensure that there are no duplicates (checked via md5sum).
But one feature I truly want to include is to ensure that the extension of a filename matches the file's type.
For example, if the file's name is "sunrise.png" I want to ensure that it actually is indeed a PNG and not a mislabeled JPEG (for example).
I am approaching this with four functions, as follows.
string extension(string fileName) // returns the extension of fileName (including .tar.gz handling, so it isn't blindly just returning the last 3 characters)
string fileType(string fileName) // This one is the key -- it returns the actual file type, so if the file named fileName is a PNG, fileType() will return PNG, regardless of the return value of extension()
string basename(string fileName) // Rerturns the basename of the file, I.e. everything before the extension (so, for sunset.jpg, it would return sunset; for fluffytarball,tar.gz, it would return fluffytarball)
string renameFile(string incorrectFileName, string fileNameBeforeExtension, string actualFileType) // Returns a string whose value is the basename concatenated with the correct file extension.
string file = sunset.jpg;
/* Setting file to be hard-coded for illustrative purposes only */
if(extension(file) != fileType(file)
{
char fixedName [] = renameFile(file, basename(file), fileType(file));
puts(fixedName);
}
I have zero issues with the string processing. I'm stuck, however, on fileType(). I want this program to not only run on my primary machine (Kubuntu 14.04), but also to be capable of being run on a Windows machine as well. So, it seems I need some library or set of libraries that would be common to both (or at the least compiled for both).
Any help/advice?
There are more exceptions than rules for guessing the actual type of a file based on its contents.
This is exacerbated by the fact that a file can be valid and useful interpreted as two completely different file types.
For a good program trying to guess on insufficient data, try file on Unixoids.
You could try looking at file source code: https://github.com/file/file .
But as wikipedia states
file's position-sensitive tests are normally implemented by matching various locations within the file against a textual database of magic numbers (see the Usage section). This differs from other simpler methods such as file extensions and schemes like MIME.
In most implementations, the file command uses a database to drive the probing of the lead bytes. That database is implemented in a file called magic, whose location is usually in /etc/magic, /usr/share/file/magic or a similar location.
So it does not seem trivial.