public static void main(String[] args) { }
public static void main(String[] args) { try () { } }
FileInputStream
or FileReader
in the block statement. In the constructor of the created input stream, specify the name of the text file previously created. In this article, we called it file.txt. public static void main(String[] args) { try (FileInputStream in = new FileInputStream("file.txt")) { } }
FileOutputStream
or FileWriter
in the block statement. In the constructor of the created output stream, specify another arbitrary text file name. In this article, we will name it file-copy.txt. public static void main(String[] args) { try ( FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file-copy.txt") ) { } }
FileInputStream
, you need to create FileOutputStream
. The same goes for the FileReader
and FileWriter
.int
variable. This variable will be used for temporary storage of read byte or character. public static void main(String[] args) { try ( FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file-copy.txt") ) { int data; } }
int
variable until it reads the number -1
. In other words, it reads data from the file until it reaches the end of the file. public static void main(String[] args) { try ( FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file-copy.txt") ) { int data; while ((data = in.read()) != -1) { } } }
public static void main(String[] args) { try ( FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file-copy.txt") ) { int data; while ((data = in.read()) != -1) { out.write(data); } } }
IOException
and, in case of an error, print the stack trace to the console. public static void main(String[] args) { try ( FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file-copy.txt") ) { int data; while ((data = in.read()) != -1) { out.write(data); } } catch (IOException e) { e.printStackTrace(); } }
file.txt
are copied to the file-copy.txt
which, if it doesn't exist yet, is created.Buffered streams optimize input and output data. Buffer streams are more efficient than byte and character streams as they read and write from a buffer that is handled by Java platform rather than the underlying operating system. Most common usage of buffered streams is reading a textual file, line by line.
public static void main(String[] args) { }
public static void main(String[] args) { try () { } }
BufferedReader
in the block statement. In the constructor of the created input stream, create a FileReader
and specify the name of the text file previously created inside its constructor. In this article, we called it file.txt. public static void main(String[] args) { try (BufferedReader in = new BufferedReader(new FileReader("file.txt"))) { } }
BufferedWriter
in the block statement. In the constructor of the created output stream, create a FileWriter
and specify another arbitrary text file name in its constructor. In this article, we will name it file-copy.txt. public static void main(String[] args) { try ( BufferedReader in = new BufferedReader(new FileReader("file.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("file-copy.txt")) ) { } }
String
variable. This variable will be used for temporary storage of read lines. public static void main(String[] args) { try ( BufferedReader in = new BufferedReader(new FileReader("file.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("file-copy.txt")) ) { String line; } }
String
variable until it reads null
. In other words, it reads data from the file until it reaches the end of the file. public static void main(String[] args) { try ( BufferedReader in = new BufferedReader(new FileReader("file.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("file-copy.txt")) ) { String line; while ((line = in.readLine()) != null) { } } }
BufferedReader
reads lines, but omits the line separator. public static void main(String[] args) { try ( BufferedReader in = new BufferedReader(new FileReader("file.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("file-copy.txt")) ) { String line; while ((line = in.readLine()) != null) { out.write(line + System.lineSeparator()); } } }
IOException
and, in case of an error, print the stack trace to the console. public static void main(String[] args) { try ( BufferedReader in = new BufferedReader(new FileReader("file.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("file-copy.txt")) ) { String line; while ((line = in.readLine()) != null) { out.write(line + System.lineSeparator()); } } catch (IOException e) { e.printStackTrace(); } }
file.txt
are copied to the file-copy.txt
which, if it doesn't exist yet, is created.Data streams handle binary input and output of primitive data types and Strings. Data streams make it very easy to read and write boolean
, char
, byte
, short
, int
, long
, float
, double
and String
values. Most common usage of data streams is in network applications (e.g. instant messengers).
public static void main(String[] args) { }
public static void main(String[] args) { try () { } }
DataOutputStream
in the block statement. In the constructor of the created input stream, create FileOutputStream
and specify an arbitrary file name inside its constructor. In this article, we will name it file.txt. public static void main(String[] args) { try (DataOutputStream out = new DataOutputStream(new FileOutputStream("file.txt"))) { } }
DataInputStream
in the block statement. In the constructor of the created input stream, create a FileInputStream
and specify file name from the previous step inside its constructor. In this article, we named it file.txt. public static void main(String[] args) { try ( DataOutputStream out = new DataOutputStream(new FileOutputStream("file.txt")); DataInputStream in = new DataInputStream(new FileInputStream("file.txt")) ) { } }
boolean
, int
and other primitive data to the output stream. public static void main(String[] args) { try ( DataOutputStream out = new DataOutputStream(new FileOutputStream("file.txt")); DataInputStream in = new DataInputStream(new FileInputStream("file.txt")) ) { out.writeUTF("wikiHow is a great place to learn"); out.writeBoolean(true); out.writeInt(10430); out.writeDouble(20.9d); out.writeFloat(23.10f); } }
public static void main(String[] args) { try ( DataOutputStream out = new DataOutputStream(new FileOutputStream("file.txt")); DataInputStream in = new DataInputStream(new FileInputStream("file.txt")) ) { out.writeUTF("wikiHow is a great place to learn"); out.writeBoolean(true); out.writeInt(10430); out.writeDouble(20.9d); out.writeFloat(23.10f); System.out.println(in.readUTF()); System.out.println(in.readBoolean()); System.out.println(in.readInt()); System.out.println(in.readDouble()); System.out.println(in.readFloat()); } }
IOException
and, in case of an error, print the stack trace to the console. public static void main(String[] args) { try ( DataOutputStream out = new DataOutputStream(new FileOutputStream("file.txt")); DataInputStream in = new DataInputStream(new FileInputStream("file.txt")) ) { out.writeUTF("wikiHow is a great place to learn"); out.writeBoolean(true); out.writeInt(10430); out.writeDouble(20.9d); out.writeFloat(23.10f); System.out.println(in.readUTF()); System.out.println(in.readBoolean()); System.out.println(in.readInt()); System.out.println(in.readDouble()); System.out.println(in.readFloat()); } catch (IOException e) { e.printStackTrace(); } }
file.txt
and then read and printed to the console. wikiHow is a great place to learn true 10430 20.9 23.1
Object streams handle binary input and output of Java objects. Like data streams, object streams make it very easy to read and write primitive data types and Strings, but also any object that implements Serializable
interface. Most common usage of object streams is in network applications where both server and client are written in Java.
Serializable
interface. Name the class however you'd like, but in this article we will name it WikiHowian. public class WikiHowian implements Serializable { }
WikiHowian
class represents a person, we will create two String variables for first and last name and an int
variable for age. public class WikiHowian implements Serializable { private String firstName, lastName; private int age; }
firstName
, secondName
and age
parameters, corresponding to the private
variable types. public class WikiHowian implements Serializable { private String firstName, lastName; private int age; public WikiHowian(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } }
toString
method. Overriding toString
will, later on, allow us to print the object to the console easily. public class WikiHowian implements Serializable { private String firstName, lastName; private int age; public WikiHowian(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } @Override public String toString() { return firstName + " " + lastName + ", " + age; } }
public class WikiHowian implements Serializable { private String firstName, lastName; private int age; public WikiHowian(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } @Override public String toString() { return firstName + " " + lastName + ", " + age; } public static void main(String[] args) { } }
public class WikiHowian implements Serializable { private String firstName, lastName; private int age; public WikiHowian(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } @Override public String toString() { return firstName + " " + lastName + ", " + age; } public static void main(String[] args) { try () { } } }
ObjectOutputStream
in the block statement. In the constructor of the created input stream, create FileOutputStream
and specify an arbitrary file name inside its constructor. In this article, we will name it file.txt. public class WikiHowian implements Serializable { private String firstName, lastName; private int age; public WikiHowian(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } @Override public String toString() { return firstName + " " + lastName + ", " + age; } public static void main(String[] args) { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.txt"))) { } } }
ObjectInputStream
in the block statement. In the constructor of the created input stream, create a FileInputStream
and specify file name from the previous step inside its constructor. In this article, we named it file.txt.