How-to articles, tricks, and solutions about IO
To write a byte[] to a file in Java, you can use the following code:
One way to write the contents of a Java InputStream to an OutputStream is to use the read and write methods of the InputStream and OutputStream classes.
To get an OutputStream into a String, you can use a ByteArrayOutputStream as the OutputStream and then use the toString() method of the ByteArrayOutputStream to get the contents of the stream as a string.
To read a large text file line by line in Java, you can use a BufferedReader and pass it a FileReader object to read the file. Here's an example of how you can do this:
To check if a file exists in Java, you can use the exists method of the File class from the java.io package. This method returns true if the file exists, and false if it doesn't. Here's an example of how you can use this method:
To create a Java string from the contents of a file, you can use the following code:
To get the file extension of a file in Java, you can use the File class and its getName() method to get the file name, and then use the substring() method of the String class to extract the extension from the file name.
There are several ways to read an InputStream and convert it to a String in Java. One way is to use the BufferedReader and InputStreamReader classes to read the InputStream line by line, and use a StringBuilder to construct the final String:
To append text to the end of an existing file in Java, you can use the Files.write() method from the java.nio.file package. This method allows you to write a sequence of characters to a file in a single operation.
To read all the files in a folder from Java, you can use the File class from the java.io package to list all the files in a directory. Here's an example of how you can do this:
To read an integer value from the standard input (keyboard) in Java, you can use the Scanner class from the java.util package.
It is common for the Scanner class's next() and nextFoo() methods (where Foo is any primitive type such as Int, Double, etc.) to skip over newline characters in the input. This is because these methods are designed to read only the next token in the input
To read a file in Unicode (UTF-8) encoding in Python, you can use the built-in open() function, specifying the encoding as "utf-8".