How to read a file in Groovy into a string?
String fileContents = new File(‘/path/to/file’).text If you need to specify the character encoding, use the following instead: String fileContents = new File(‘/path/to/file’).getText(‘UTF-8’)
String fileContents = new File(‘/path/to/file’).text If you need to specify the character encoding, use the following instead: String fileContents = new File(‘/path/to/file’).getText(‘UTF-8’)
The shortest way would probably be to use the fileinput module. For example, the following adds line numbers to a file, in-place: import fileinput for line in fileinput.input(“test.txt”, inplace=True): print(‘{} {}’.format(fileinput.filelineno(), line), end=”) # for Python 3 # print “%d: %s” % (fileinput.filelineno(), line), # for Python 2 What happens here is: The original file … Read more
You may use MemoryStream.WriteTo or Stream.CopyTo (supported in framework version 4.5.2, 4.5.1, 4.5, 4) methods to write content of memory stream to another stream. memoryStream.WriteTo(fileStream); Update: fileStream.CopyTo(memoryStream); memoryStream.CopyTo(fileStream);
Try the next: ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream(“test.csv”); If the above doesn’t work, various projects have been added the following class: ClassLoaderUtil1 (code here).2 Here are some examples of how that class is used: src\main\java\com\company\test\YourCallingClass.java src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java src\main\resources\test.csv // java.net.URL URL url = ClassLoaderUtil.getResource(“test.csv”, YourCallingClass.class); Path path = Paths.get(url.toURI()); List<String> lines = Files.readAllLines(path, … Read more
Git keeps track of filepermission and exposes permission changes when creating patches using git diff -p. So all we need is: create a reverse patch include only the permission changes apply the patch to our working copy As a one-liner: git diff -p -R –no-ext-diff –no-color \ | grep -E “^(diff|(old|new) mode)” –color=never \ | … Read more
The preferred way to do this is to only put an opening brace { after the last using statement, like this: using (StreamReader outFile = new StreamReader(outputFile.OpenRead())) using (StreamReader expFile = new StreamReader(expectedFile.OpenRead())) { ///… }
$files = glob(‘path/to/temp/*’); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) { unlink($file); // delete file } } If you want to remove ‘hidden’ files like .htaccess, you have to use $files = glob(‘path/to/temp/{,.}*’, GLOB_BRACE);
>>> import os >>> os.stat(“file”).st_size == 0 True
How about: Dir[“/path/to/directory/*.rb”].each {|file| require file }
It sounds like you’re looking for enca. It can guess and even convert between encodings. Just look at the man page. Or, failing that, use file -i (Linux) or file -I (OS X). That will output MIME-type information for the file, which will also include the character-set encoding. I found a man-page for it, too 🙂