Read/Write String from/to a File in Android

Hope this might be useful to you. Write File: private void writeToFile(String data,Context context) { try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(“config.txt”, Context.MODE_PRIVATE)); outputStreamWriter.write(data); outputStreamWriter.close(); } catch (IOException e) { Log.e(“Exception”, “File write failed: ” + e.toString()); } } Read File: private String readFromFile(Context context) { String ret = “”; try { InputStream inputStream = … Read more

C fopen vs open

First, there is no particularly good reason to use fdopen if fopen is an option and open is the other possible choice. You shouldn’t have used open to open the file in the first place if you want a FILE *. So including fdopen in that list is incorrect and confusing because it isn’t very … Read more

Read binary file as string in Ruby

First, you should open the file as a binary file. Then you can read the entire file in, in one command. file = File.open(“path-to-file.tar.gz”, “rb”) contents = file.read That will get you the entire file in a string. After that, you probably want to file.close. If you don’t do that, file won’t be closed until … Read more

How to write a large buffer into a binary file in C++, fast?

This did the job (in the year 2012): #include <stdio.h> const unsigned long long size = 8ULL*1024ULL*1024ULL; unsigned long long a[size]; int main() { FILE* pFile; pFile = fopen(“file.binary”, “wb”); for (unsigned long long j = 0; j < 1024; ++j){ //Some calculations to fill a[] fwrite(a, 1, size*sizeof(unsigned long long), pFile); } fclose(pFile); return … Read more

What is simplest way to read a file into String? [duplicate]

Yes, you can do this in one line (though for robust IOException handling you wouldn’t want to). String content = new Scanner(new File(“filename”)).useDelimiter(“\\Z”).next(); System.out.println(content); This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is … Read more

How do I check if file exists in jQuery or pure JavaScript?

With jQuery: $.ajax({ url:’http://www.example.com/somefile.ext’, type:’HEAD’, error: function() { //file not exists }, success: function() { //file exists } }); EDIT: Here is the code for checking 404 status, without using jQuery function UrlExists(url) { var http = new XMLHttpRequest(); http.open(‘HEAD’, url, false); http.send(); return http.status!=404; } Small changes and it could check for status HTTP … Read more

Python recursive folder read

Make sure you understand the three return values of os.walk: for root, subdirs, files in os.walk(rootdir): has the following meaning: root: Current path which is “walked through” subdirs: Files in root of type directory files: Files in root (not in subdirs) of type other than directory And please use os.path.join instead of concatenating with a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)