file-processing

How to prevent file from being overridden when reading and processing it with Java?

北战南征 提交于 2019-11-30 18:18:57
问题 I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it? That is, some way to make it read-only, keep it "open" or something... This would be done in Windows environment. br, Touko 回答1: you want a FileLock: FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel(); // Try acquiring the lock without blocking. This method

Randomly Pick Lines From a File Without Slurping It With Unix

…衆ロ難τιáo~ 提交于 2019-11-28 03:03:59
I have a 10^7 lines file, in which I want to choose 1/100 of lines randomly from the file. This is the AWK code I have, but it slurps all the file content before hand. My PC memory cannot handle such slurps. Is there other approach to do it? awk 'BEGIN{srand()} !/^$/{ a[c++]=$0} END { for ( i=1;i<=c ;i++ ) { num=int(rand() * c) if ( a[num] ) { print a[num] delete a[num] d++ } if ( d == c/100 ) break } }' file cadrian if you have that many lines, are you sure you want exactly 1% or a statistical estimate would be enough? In that second case, just randomize at 1% at each line... awk 'BEGIN

Python - reading files from directory file not found in subdirectory (which is there)

北慕城南 提交于 2019-11-26 21:57:43
问题 I am convinced it is something simply syntactic - I however can not figure out why my code: import os from collections import Counter d = {} for filename in os.listdir('testfilefolder'): f = open(filename,'r') d = (f.read()).lower() freqs = Counter(d) print(freqs) will not work - it apparently can see in to the 'testfilefolder' folder and tell me that the the file is there i.e. an error message 'file2.txt' is not found. So it can find it to tell me that it is not found... I however get this