When you created a new project (Java. Create new project in Eclipse), you want to know how to work with text files. Open SampleText.java and add after "package SampleText" two strings.


They will allow us to work with files. Let’s determine the quantity of strings in text file.


This code prints the quantity of lines in file (var nums) and the content of file (var sb). If file is not found, we’ll see the error message.


If we want to read each line of file to String array, we add the code like this:


We declared the string array and set value of it’s element to string values.

If we change try to call the "nums+1" element, we’ll get the error message like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6

       at SampleText.SampleText.main(SampleText.java:50)


Using array is a classic method. But quite for a long time instead  of arrays it’s better to use collections List<String>. You won't have to think about their sizes.