Undo a file readline() operation so file-pointer is back in original state

You have to remember the position by calling file.tell() before the readline and then calling file.seek() to rewind. Something like: fp = open(‘myfile’) last_pos = fp.tell() line = fp.readline() while line != ”: if line == ‘SPECIAL’: fp.seek(last_pos) other_function(fp) break last_pos = fp.tell() line = fp.readline() I can’t recall if it is safe to call … Read more

Uploading and Downloading large files in ASP.NET Core 3.1?

If you have files that large, never use byte[] or MemoryStream in your code. Only operate on streams if you download/upload files. You have a couple of options: If you control both client and server, consider using something like tus. There are both client- and server-implementations for .NET. This would probably the easiest and most … Read more

PHP create random tmp file and get its full path

tmpfile returns a stream-able file pointer. To get the corresponding path, ask the stream for its meta data: $file = tmpfile(); $path = stream_get_meta_data($file)[‘uri’]; // eg: /tmp/phpFx0513a The benefit of the tmpfile approach? PHP automatically removes the $path when $file goes out of scope. With tempnam, you must manually remove the created file.

Speeding up file I/O: mmap() vs. read()

Reads back to what? What is the final destination of this data? Since it sounds like you are completely IO bound, mmap and read should make no difference. The interesting part is in how you get the data to your receiver. Assuming you’re putting this data to a pipe, I recommend you just dump the … Read more

Wait for file to be freed by process

A function like this will do it: public static bool IsFileReady(string filename) { // If the file can be opened for exclusive access it means that the file // is no longer locked by another process. try { using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None)) return inputStream.Length > 0; } catch (Exception) { return … Read more

Java io ugly try-finally block

This is the correct idom (and it works fine): InputStream in = null; OutputStream out = null; try { in = new FileInputStream(inputFileName); out = new FileOutputStream(outputFileName); copy(in, out); finally { close(in); close(out); } public static void close(Closeable c) { if (c == null) return; try { c.close(); } catch (IOException e) { //log the … Read more