How to write to a JSON file in the correct format

Require the JSON library, and use to_json. require ‘json’ tempHash = { “key_a” => “val_a”, “key_b” => “val_b” } File.open(“public/temp.json”,”w”) do |f| f.write(tempHash.to_json) end Your temp.json file now looks like: {“key_a”:”val_a”,”key_b”:”val_b”}

Parse large JSON file in Nodejs

To process a file line-by-line, you simply need to decouple the reading of the file and the code that acts upon that input. You can accomplish this by buffering your input until you hit a newline. Assuming we have one JSON object per line (basically, format B): var stream = fs.createReadStream(filePath, {flags: ‘r’, encoding: ‘utf-8’}); … Read more

How to create a file in Android?

I used the following code to create a temporary file for writing bytes. And its working fine. File file = new File(Environment.getExternalStorageDirectory() + “https://stackoverflow.com/” + File.separator + “test.txt”); file.createNewFile(); byte[] data1={1,1,0,0}; //write the bytes in file if(file.exists()) { OutputStream fo = new FileOutputStream(file); fo.write(data1); fo.close(); System.out.println(“file created: “+file); } //deleting the file file.delete(); System.out.println(“file deleted”);

Read and overwrite a file in Python

If you don’t want to close and reopen the file, to avoid race conditions, you could truncate it: f = open(filename, ‘r+’) text = f.read() text = re.sub(‘foobar’, ‘bar’, text) f.seek(0) f.write(text) f.truncate() f.close() The functionality will likely also be cleaner and safer using open as a context manager, which will close the file handler, … Read more

Open file dialog box in JavaScript

$(“#logo”).css(‘opacity’,’0′); $(“#select_logo”).click(function(e){ e.preventDefault(); $(“#logo”).trigger(‘click’); }); <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <a href=”#” id=”select_logo”>Select Logo</a> <input type=”file” id=”logo”> for IE add this: $(“#logo”).css(‘filter’,’alpha(opacity = 0′);

Java FileReader encoding issue

Yes, you need to specify the encoding of the file you want to read. Yes, this means that you have to know the encoding of the file you want to read. No, there is no general way to guess the encoding of any given “plain text” file. The one-arguments constructors of FileReader always use the … Read more

Python module os.chmod(file, 664) does not change the permission to rw-rw-r– but -w–wx—-

Found this on a different forum If you’re wondering why that leading zero is important, it’s because permissions are set as an octal integer, and Python automagically treats any integer with a leading zero as octal. So os.chmod(“file”, 484) (in decimal) would give the same result. What you are doing is passing 664 which in … Read more