background preloader

Welcome - Learn Java - Free Interactive Java Tutorial

Java For Complete Beginners - Reading Text Files Manipulating text files is a skill that will serve you well in your programming career. In this section, you'll learn how to open and how to write to a text file. But by text file, we just mean a file with text in it - simple as that! You can create a text file in programmes like Notepad on a Windows computer, TextEdit on a Mac, Gedit in a Linux/Gnome environment. The first thing we'll do is to open up a text file and read its contents. Read a Text File Start a new project for this. import java.io.IOException; Your coding window will then look like this: To deal with anything going wrong with our file handling, add the following to the main method (the text in bold): public static void main(String[ ] args) throws IOException { We're telling Java that the main method will throw up an IOException error, and that it has to be dealt with. To open the text file, let's create a new class. import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader; public String[ ] int i;

Related: