How-to articles, tricks, and solutions about IO

byte[] to file in Java

To write a byte[] to a file in Java, you can use the following code:

console.writeline and System.out.println

console.writeline is not a valid method in Java.

Easy way to write contents of a Java InputStream to an OutputStream

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.

Get an OutputStream into a String

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.

How can I read a large text file line by line using Java?

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:

How do I check if a file exists in Java?

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:

How do I create a Java string from the contents of a file?

To create a Java string from the contents of a file, you can use the following code:

How do I get the file extension of a file in Java?

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.

How do I read / convert an InputStream into a String in Java?

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:

How to append text to an existing file in Java?

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.

How to read all files in a folder from Java?

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:

How to read integer value from the standard input in Java

To read an integer value from the standard input (keyboard) in Java, you can use the Scanner class from the java.util package.

Scanner is skipping nextLine() after using next() or nextFoo()?

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

Unicode (UTF-8) reading and writing to files in Python

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".