How-to articles, tricks, and solutions about FILE-IO

Correct way to write line to file?

The correct way to write a line to a file in Python is to use the write() method on the file object, followed by the line you want to write.

File to byte[] in Java

To convert a file to a byte[] in Java, you can use the readAllBytes method of the Files class from the java.nio.file package. This method reads all the bytes from a file and returns them in a byte[]. Here's an example of how you can use this method:

Find all files in a directory with extension .txt in Python

Here is a code snippet that uses the os and glob modules to find all files in a directory with the extension '.txt':

How can I delete a file or folder in Python?

To delete a file or folder in Python, you can use the os module and call the os.remove() function to delete a file, or the shutil.rmtree() function to delete a folder and all its contents.

How can I open multiple files using "with open" in Python?

You can open multiple files using "with open" in Python by using multiple with open statements, one for each file you wish to open.

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 save a String to a text file using Java?

To save a String to a text file in Java, you can use the write method of the FileWriter class. Here's an example of how you can do this:

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 create a temporary directory/folder in Java?

To create a temporary directory/folder in Java, you can use the createTempDirectory() method of the Files class in the java.nio.file package.

How to read text file from classpath in Java?

To read a text file from the classpath in Java, you can use the getResourceAsStream method of the ClassLoader class to get an InputStream for the file, and then use a BufferedReader to read the contents of the file.

Java FileOutputStream Create File if not exists

To create a file using FileOutputStream in Java if it does not exist, you can use the following code:

Java: How to read a text file

To read a text file in Java, you can use the BufferedReader class from the java.io package.

java.io.FileNotFoundException: the system cannot find the file specified

The java.io.FileNotFoundException: the system cannot find the file specified error is usually thrown when a program tries to access a file that does not exist, or that it does not have permission to access.

open() in Python does not create a file if it doesn't exist

Here is a code snippet that demonstrates how to use the open() function in Python to create a new file if it does not already exist:

Print string to text file

In Python, you can use the built-in open() function to create a text file and write a string to it.

Read/Write String from/to a File in Android

To read and write a string from and to a file in Android, you can use the FileInputStream and FileOutputStream classes along with the InputStreamReader and OutputStreamWriter classes.

Reading a plain text file in Java

This code opens the file "filename.txt" and reads it line by line. Each line is printed to the console until the end of the file is reached.

Reading binary file and looping over each byte

Here is an example code snippet that demonstrates how to read a binary file and loop over each byte in Python:

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

This error occurs when trying to decode a string using the 'charmap' codec, which is typically used for Windows-1252 character encoding.

What is simplest way to read a file into String?

To read a file into a String in Java, you can use the readAllBytes() method of the Files class and the new String() constructor:

Writing a list to a file with Python, with newlines

Here is a code snippet that demonstrates how to write a list to a file with newlines in Python: