Java Input Output
This tutorial explains how to read and write files via Java.
Table of Contents
- 1. Java I/O (input/output) for files
-
- 1.1. Overview
- 1.2. Reading a file in Java
- 1.3. Writing a file in Java
- 1.4. How to identify the current directory
- 2. Exercise:reading and writing files
- 3. Reading resources out of your Project/jar
- 4. About this website
-
- 4.1.
- 5. Links and literature
1. Java I/O (input/output) for files1.1. Overview
Java provides a standard-of-the-reading from and writing to files. Traditionally java.io
the package is used, but in modern Java applications the java.nio.file
API.
Java would read all input as a stream of bytes. The class is the superclass of any InputStream
classes representing an input stream of bytes.
1.2. Reading a file in Java
To read a text file you can use the Files.readAllBytes
method as demonstrated by the following listing.
Import java.io.IOException; import Java.nio.file.Files; import java.nio.file.Paths; //Somewhere in your code
To read a text file line by line into a of List
type String
structure you can use the following example.
1.3. Writing a file in Java
To write a file can use the following method:
1.4. How to identify the current directory
You can access files relative to the current execution directory of your Java program. To print the current directory in which your Java program is running and you can use the following statement.
System.out.println (System.getproperty (
2. Exercise:reading and writing files
Create a new Java project called com.vogella.java.files. Create the following FilesUtil.java
class.
Package Com.vogella.java.files;import Java.io.ioexception;import Java.nio.file.files;import java.nio.file.Paths; Import Java.nio.file.standardopenoption;import Java.util.list;public class Filesutil {public static String ReadTextFile (String fileName) throws IOException { string content = new String (Files.readallbytes (Paths.get ( FileName)); return content; } public static list<string> Readtextfilebylines (String fileName) throws IOException { list<string> Lines = Files.readalllines (Paths.get (FileName)); return lines; } public static void Writetotextfile (string fileName, String content) throws IOException { files.write (Paths.get ( FileName), Content.getbytes (), standardopenoption.create); }
To test these methods, the Create a text file called with the file.txt
some content in your project folder. Create the following Main
class and run it.
Package Com.vogella.java.files;import Java.io.ioexception;import Java.nio.file.path;import java.nio.file.Paths; public class Main {public static void Main (string[] args) throws IOException { String input = FILESUTIL.READTEXTF Ile ("file.txt"); SYSTEM.OUT.PRINTLN (input); Filesutil.writetotextfile ("Copy.txt", input); System.out.println (Filesutil.readtextfile ("Copy.txt")); Filesutil.readtextfilebylines ("file.txt"); Path PATH = Paths.get ("file.txt");} }
3. Reading resources out of your Project/jar
You can read resources from your project or your jar file via the .getClass().getResourceAsStream()
method chain from any object.
Reading and writing files in Java (input/output)-Tutorial