Resetting Standard output Stream

You can get hold of the file descriptor for standard out through FileDescriptor.out. To reset standard out to print to console, you do System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); Another way is to simply hold on to the original object, as follows: PrintStream stdout = System.out; System.setOut(new PrintStream(logFile)); // … System.setOut(stdout); // reset to standard output

Create a new line in Java’s FileWriter

If you want to get new line characters used in current OS like \r\n for Windows, you can get them by System.getProperty(“line.separator”); since Java7 System.lineSeparator() or as mentioned by Stewart generate them via String.format(“%n”); You can also use PrintStream and its println method which will add OS dependent line separator at the end of your … Read more

At what point does wrapping a FileOutputStream with a BufferedOutputStream make sense, in terms of performance?

BufferedOutputStream helps when the writes are smaller than the buffer size e.g. 8 KB. For larger writes it doesn’t help nor does it make it much worse. If ALL your writes are larger than the buffer size or you always flush() after every write, I would not use a buffer. However if a good portion … Read more

simple Haskell loop

You could define a recursive function that prints “a string” n times (n being the parameter of the function), like this: printStringNTimes 0 = return () printStringNTimes n = do putStrLn “a string” printStringNTimes (n-1) main = printStringNTimes 10 A somewhat more general approach would be to define a function that repeats any IO action … Read more

When to flush a BufferedWriter

The BufferedWriter will already flush when it fills its buffer. From the docs of BufferedWriter.write: Ordinarily this method stores characters from the given array into this stream’s buffer, flushing the buffer to the underlying stream as needed. (Emphasis mine.) The point of BufferedWriter is basically to consolidate lots of little writes into far fewer big … Read more

How to update/modify an XML file in python?

Using ElementTree: import xml.etree.ElementTree # Open original file et = xml.etree.ElementTree.parse(‘file.xml’) # Append new tag: <a x=’1′ y=’abc’>body text</a> new_tag = xml.etree.ElementTree.SubElement(et.getroot(), ‘a’) new_tag.text=”body text” new_tag.attrib[‘x’] = ‘1’ # must be str; cannot be an int new_tag.attrib[‘y’] = ‘abc’ # Write back to file #et.write(‘file.xml’) et.write(‘file_new.xml’) note: output written to file_new.xml for you to experiment, … Read more

Haskell pre-monadic I/O

Before the IO monad was introduced, main was a function of type [Response] -> [Request]. A Request would represent an I/O action like writing to a channel or a file, or reading input, or reading environment variables etc.. A Response would be the result of such an action. For example if you performed a ReadChan … Read more

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