Fastest way to tell if two files have the same contents in Unix/Linux?
I believe cmp will stop at the first byte difference: cmp –silent $old $new || echo “files are different”
I believe cmp will stop at the first byte difference: cmp –silent $old $new || echo “files are different”
Guaranteed to be (statistically) unique: string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + “.csv”; (To quote from the wiki article on the probabilty of a collision: …one’s annual risk of being hit by a meteorite is estimated to be one chance in 17 billion [19], that means the probability is about 0.00000000006 (6 × 10−11), equivalent … Read more
As pointed out by michaelb958, you cannot replace in place with data of a different length because this will put the rest of the sections out of place. I disagree with the other posters suggesting you read from one file and write to another. Instead, I would read the file into memory, fix the data … Read more
Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream: try (InputStream in = getClass().getResourceAsStream(“/file.txt”); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { // Use resource } As long as the file.txt resource is available on the classpath then this approach will … Read more
Just use the PHP magic constant __FILE__ to get the current filename. But it seems you want the part without .php. So… basename(__FILE__, ‘.php’); A more generic file extension remover would look like this… function chopExtension($filename) { return pathinfo($filename, PATHINFO_FILENAME); } var_dump(chopExtension(‘bob.php’)); // string(3) “bob” var_dump(chopExtension(‘bob.i.have.dots.zip’)); // string(15) “bob.i.have.dots” Using standard string library functions is … Read more
If you, like me, would rather use some library code where they probably have thought of all special cases, such as what happens if you pass in null or dots in the path but not in the filename, you can use the following: import org.apache.commons.io.FilenameUtils; String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
A very minor improvement of the code by Awesomeness01 (no need for anchor tag) with addition as suggested by trueimage (support for IE): // Function to download data to a file function download(data, filename, type) { var file = new Blob([data], {type: type}); if (window.navigator.msSaveOrOpenBlob) // IE10+ window.navigator.msSaveOrOpenBlob(file, filename); else { // Others var a … Read more
Here’s how you read a file, and then write to it (overwriting any existing data), without closing and reopening: with open(filename, “r+”) as f: data = f.read() f.seek(0) f.write(output) f.truncate()
Java 8 provides a nice stream to process all files in a tree. Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .forEach(System.out::println); This provides a natural way to traverse files. Since it’s a stream you can do all nice stream operations on the result such as limit, grouping, mapping, exit early etc. UPDATE: I might point out there is also Files.find … Read more
Let’s make a Go 1-compatible list of all the ways to read and write files in Go. Because file API has changed recently and most other answers don’t work with Go 1. They also miss bufio which is important IMHO. In the following examples I copy a file by reading from it and writing to … Read more