Karla News

How to Read from an Input File in Java

Integers

If you are coding in JAVA and you need to get input from a text file there are many ways you can choose to accomplish this. File reading objects such as Scanner, BufferedReader, and FileReader are commonly used in java. This article will focus on using the Scanner class in reading files in java.

Suppose you have a text file which contains a long list of integers that you need to read, do some calculations with, and out put the answer to the screen then you will need to know how to read the integers from a text file. The Scanner class is located in the java.util package. So make sure to add the following line in the beginning of your code so that you can use Scanner.

import java.util.Scanner;

You can import the entire util package by adding this line:

import java.util.*:

This will come in handy in case you are using another object in the package.

When reading from a file in your java code, you will have to make sure to throw the IOException in case there is a problem with your input file. If you do not do this, your java code will probably not compile. Make sure your main class declaration looks like this:

public static void main(String[] args) throws IOException{

}

You will also have to import the following for the exception.

import java.io.*;

Now, you are all ready to use the Scanner class. We now have to create a scanner object to use. If you are reading from a file, then you will have to declare the file inside of the Scanner parameters. In the following :

See also  Mouse Pad Comparison: Steel Series, RatPadz and Cyber Snipa

Scanner in = new Scanner(new File(“myFile.txt”));

We have created a Scanner object named ‘in’ which will scan the File myFile.txt. This location is relative, so it will look for a file myFile.txt within the directory of the .class file. If your input file is not in the same directory, then you will have to write the entire file name inside of the quotation marks (make sure to use the correct form for ‘s as you will have to use the escape character \ , i.e. C:documentsmyFile.txt would become C:\documents\myFile.txt)

Now you can read from the file using several different methods such as nextInt() or nextLine(). (Check the link at the bottom for more methods in the Scanner class). To retrieve an integer from your file, you will use the .nextInt() to retrieve the integer, making sure to store it somewhere for use.

Here is a short snippet of code that will take every integer from a file and put it into an ArrayList.

Scanner in = new Scanner(new File(“integers.txt”));

ArrayList intList = new ArrayList();

while(in.hastNextInt()){

intList.add(in.nextInt());

}

You can also read Strings, Doubles, and other types of objects, just make sure to use the correct format for the input or you will result with a run-time error.

Check out http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html to learn about the other methods of the Scanner class.

Reference: