Package java.io - java.io Package in Java
java.io Package
java.io Package in Java
Package java.io provides for system input and output through data streams, serialization and the file system.
This tutorial is an overview of Java I/O and all the classes in the java.io package. And explains how to handle file management in java.
In this tutorial you can learn about File Management in java using java io Package. Also learn about java io package and what are java io package classes.
What is the File Management in Java ?
Java I/O Introduction
- Data in files on your system is called persistent data because it persists after the program runs.
- Files are created through streams in Java.
- A stream is a linear, sequential flow of bytes of input or output data.
- Streams are written to the file system to create files.
- Streams can also be transferred over the Internet.
- Three streams are created for us automatically :
System.out - standard output stream
System.in - standard input stream
System.err - standard error
What are the Basic Requirements to create files in Java ?
How to Create Text or Binary Files in Java ?
What's your format for storing or transmitting data? Will you be using text or binary data?
If you use binary data, such as integers or doubles, then use the InputStream and OutputStream classes.
If you are using text data, then the Reader and Writer classes are right.
How to Random Access file in Java ?
Do you want random access to records? Random access allows you to go anywhere within a file and be able to treat the file as if it were a collection of records.
The RandomAccessFile class permits random access. The data is stored in binary format. Using random access files improves performance and efficiency.
How to Create Object or non-object file in Java ?
Are you inputting or outputting the attributes of an object? If the data itself is an object, then use the ObjectInputStream and ObjectOutputStream classes.
Sources and Sinks for Data
What is the source of your data? What will be consuming your output data, that is, acting as a sink? You can input or output your data in a number of ways: sockets, files, strings, and arrays of characters. Any of these can be a source for an InputStream or Reader or a sink for an OutputStream or Writer.
How to Filtering Data in Java File Management ?
Do you need filtering for your data? There are a couple ways to filter data.
Buffering is one filtering method. Instead of going back to the operating system for each byte, you can use an object to provide a buffer.
Checksumming is another filtering method. As you are reading or writing a stream, you might want to compute a checksum on it. A checksum is a value you can use later on to make sure the stream was transmitted properly.
How to Storing Data Records in a File ?
A data record is a collection of more than one element, such as names or addresses. There are three ways to store data records :
- Use delimited records, such as a mail-merge style record, to store values. On output, data values are converted to strings separated by delimiters such as the tab character, and ending with a new-line character. To read the data back into Java code, the entire line is read and then broken up using the StringTokenizer class.
- Use fixed size records to store data records. Use the RandomAccessFile class to store one or more records. Use the seek() method to find a particular record. If you choose this option to store data records, you must ensure strings are set to a fixed maximum size.
- Use variable length records if you use an auxiliary file to store the lengths of each record. Use object streams to store data records. If object streams are used, no skipping around is permitted, and all objects are written to a file in a sequential manner.