Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an "I/O" topic (input/output). The related classes are in <code>package java.io</code>.</p> <p>If you're reading a simple text file, <a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html" rel="nofollow noreferrer"><code>java.util.Scanner</code></a> can be very useful. There are many examples in the documentation, and also elsewhere on StackOverflow.</p> <h3>See also</h3> <ul> <li><a href="http://java.sun.com/docs/books/tutorial/essential/io/" rel="nofollow noreferrer">Java Lessons/Basic I/O</a></li> </ul> <hr> <h3>Simple example</h3> <p>The following code takes a filename from the command line, treating it as a text file, and simply print its content to standard output.</p> <pre><code>import java.util.*; import java.io.*; public class FileReadSample { public static void main(String[] args) throws FileNotFoundException { String filename = args[0]; // or use e.g. "myFile.txt" Scanner sc = new Scanner(new File(filename)); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } } </code></pre> <p>You can compile this, and then run it as, say, <code>java FileReadSample myFile.txt</code>.</p> <p>For beginners, <code>Scanner</code> is recommended, since it doesn't require complicated handling of <a href="http://java.sun.com/javase/6/docs/api/java/io/IOException.html" rel="nofollow noreferrer"><code>IOException</code></a>.</p> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#hasNextLine%28%29" rel="nofollow noreferrer"><code>Scanner.hasNextLine()</code></a> <ul> <li>Returns <code>true</code> if there is another line in the input of this scanner.</li> </ul></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#nextLine%28%29" rel="nofollow noreferrer"><code>Scanner.nextLine()</code></a> <ul> <li>Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.</li> </ul></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#ioException%28%29" rel="nofollow noreferrer"><code>Scanner.ioException()</code></a> <ul> <li>Returns the <code>IOException</code> last thrown by this <code>Scanner</code>'s underlying <code>Readable</code>. This method returns <code>null</code> if no such exception exists.</li> </ul></li> </ul> <h3>See also</h3> <ul> <li><a href="http://java.sun.com/docs/books/tutorial/essential/io/scanfor.html" rel="nofollow noreferrer">Java Lessons/Basic I/O/Scanning and formatting</a></li> <li><a href="http://java.sun.com/docs/books/tutorial/essential/environment/cmdLineArgs.html" rel="nofollow noreferrer">Java Tutorials/Command Line Arguments</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload