Last Updated:

Writing data to Java files

Writing to a file is a stream of data that is managed by the output stream classes. At the top of the tree of these classes is the OutputStream, which contains child classes that control the output stream to files, consoles, sockets, and more. OutputStream inherits from the following classes:

  • FileOutputStream (hereinafter OS),
  • Objects
  • BufferedOS,
  • ByteArrayOS,
  • FilterOS,
  • DataOS.

However, as part of manipulating file streams, or more specifically for writing characters, it is more convenient to use the class stack from the abstract Writer class. It is specifically designed to transfer string and character data for writing to files and other objects. It includes the following classes: CharArrayWriter, FileWriter, BufferedWriter, FilterWriter.

Writing bytes to a file

You write bytes to a file by creating a byte stream object of the FileOutputStream class. The object constructor provides a single parameter that specifies the path to a String file.

 

You can also insert an object of type File as a path in the constructor. The second parameter is optional and is of type boolean. It has two values: true - the file continues to be written; false — the available data in the file is reset and the write starts at the beginning.

 

An example of writing a line to a file:

class Example{
public static void Main(){
String t = «Don’t worry! We w’got everything we need.»; \\ Line to write to file
FileOutputStreambyteFlow = new FileOutputStream(note.txt" false; \\ Create a stream object to the note file.txt
byte[] strToByte = t.getBytes(); \\ line feed to byteFlow.write
(strToByte); \\ Write bytes to the file
t.close();

}
}

The task of the program is to write to the file "note.txt", which is located in the project folder of the line "Don'tworry! Wew'goteverythingweneed." in byte format:

  1. The main string that you plan to write to a text file is stored in the variable "t".
  2. Open the file stream by creating an object of the FileOutputStream class and set the path to the file as the first parameter, the second parameter indicates that the file will be overwritten.
  3. Convert each character of the string to a numeric format and store it in an array of characters.
  4. Write numeric bytes to the file, which are automatically converted to characters and become the original string.
  5. Close the flow.

There is one question here: Why write text files in byte format? After all, the Java language has everything you need to write text files. A byte stream is required for graphics, media files. Text files have separate classes and a set of methods.

FileWriter class for writing text files

FileWriter inherits all functionality from the top in the hierarchy of the Writer class. The object constructor of the FileWriter class can obtain the following parameters:

  1. An object of type File.
  2. The string path to the file with its name.
  3. A handle to a file of type FileDescriptor.

The second parameter is optional. Its values are true or false, where the first option continues to write the file and the second completely overwrites it.

Example of writing a text file:

FileWritertxt_file = newFileWriter(C:\\docs\tf.txt, false);
txt_file.write(«Good morning!»);
txt_file.close();

In this example, we dispensed with formatting the string into bytes. The write(text) method simply writes a line to the beginning of the file because the constructor has an option to overwrite it. Remember to close the file stream by using the close() method. This will allow you to economically manage memory and not overload the application with unnecessary activities that may not be used in the future.

If writing to the file is carried out through an event, for example, by pressing a button, it is recommended to leave the stream open and close it when you exit the program.

Methods for Writing Files over a Byte Stream

The OutputStream class passes the following methods on its descendants:

  1. write(byte[] b) - write a byte array to a file;
  2. write(byte[] b, intgoto, intlength) — writes a byte array to the file from the first parameter, the second parameter shifts the carriage to the int stages, the third parameter indicates the number of bytes to be written.
  3. write(intch) — writes only one byte to the file in the numerical format specified in a single parameter.
  4. close() — close the thread and clear the memory.
  5. flush() — Clears the stream buffer of a single byte array or an array.

All of these methods return nothing. Therefore, their default return value is void.

Methods for manipulating the text stream

The Writer class provides several methods for child classes:

  1. write(char[] str) — write a string to the file symbolically.
  2. write(Stringstr, intgoto, intlength) — write a string with a carriage offset on goto characters forward with a limited length of the length string.
  3. Writerappend(CharSequencestr) - The string str is written to the end of the file and a Writer object is returned.
  4. close() — close the text stream.

Conclusion

 

All child classes OutputStream and Writer inherit the methods of their ancestors. You should use the exact type of thread that corresponds to the current operation. After all, for writing text files there is a simpler functionality than for a byte stream.

You should always close the thread so as not to overload the program, which will take a significant share of resources on weak computers. In Java, the functionality for manipulating files is quite low. However, this is not to say that this is not enough. On the contrary, the language has, although not flexible, but necessary.