answersLogoWhite

0


Best Answer

The class I find useful is the FileReader class in java.io. It allows you to read an external file in your program.

For instance,

Consider a file input.txt:

Hello

How are you?

1 2 3 4 5

You can access it by:

FileReader read = new FileReader("input.txt"); // pass the file name as a String

//now the read is the file

//can scan from the file using Scanner

Scanner scan = new Scanner(read);

System.out.println( scan.nextLine()); // prints Hello

System.out.println( scan.nextLine()); // prints How are you?

while(scan.hasNextInt())

System.out.print(scan.nextInt() + " "); // prints 1 2 3 4 5

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

For java 5.0, there is a really easy way to read text from a file, and there is an option to read whole objects from a file. = To read a Text external file = The simplest way is to use the Scanner Class: Scanner scan = new Scanner ( new File("[yourfilename]")); // and basically use all the scanner class's methods to access the file. // read a line String line = scan.nextLine(); // read a token (a section of non white space) String word = scan.next(); // read and integer or double int input1 = scan.nextInt(); double input2 = scan.nextDouble(); = To read objects from files will be added later =

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you read from an external file in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How read xml file in java?

Java provides several XML Parsers like DOM, SAX or JDOM and API javax.xml.parsers by using those we can easily read xml files in Java. DOM is quickest and easiest way to read XMlL file in Java.


Program in Java to create file and copy content of an existing file to it?

You need to use File class to create file in java and than Reader class implementation such as BufferedReder to read content.


How do you read and write video file in java?

you can use inputstream for reading the file java.io.fileinputstream and write the file using outputstream..


When java uses compiler?

What i know is java we will use compiler when it want to get class file(file with .class extension) from java file(file with .java extension).


When l try to use craftbukkit for minecraft it says that java is not recognized as an internal or external command?

If you don't have Java installed, download and install it. If you do have Java installed, try changing 'java' in the batch file to "C:\Program Files\Java\jre6\bin\java.exe" (with quotes) or wherever you have Java installed.


How do you open class files on windows 7?

right click on the class file ,go to properties and change open with to notepad, and then it can be opened.. :) While that may work, it is almost impossible to read as it is. What I suggest is downloading a java decompiler, then open it with that. It will convert it to what you would see in a .java file, which you are able to read if you know java programming.


What is the java file in java?

The java file has the actual JAVA code present in it,..i.e the entire coding is done in that file.. you can view it using any text editor.. After compiling it you get a class file of every class present in the java file When you open a class file in a text editor, you won't see anything that makes sence. You won't find the original code that you wrote in the java file. That is because a class file is a product of the compilation of a java file.


What type of file is created by Java?

Java created a file called class, and is identified by having the .class at the end. This file includes a Java bytecode, which then can be used on the Java Virtual Machine.


How do you read contents of pdf file using java?

Not sure if Java is capable of that but you can download a PDF text reader at this site: http://www.ctdeveloping.com/ctdeveloping/products/pdftextreader_info.asp


How do you read a pdf file using java?

Not sure if Java is capable to do that but you can download a PDF text reader using this http: http://www.ctdeveloping.com/ctdeveloping/products/pdftextreader_info.asp


How do you save a notepad file into a java file?

just add an extension .java when you save your notepad text file


What is a java lang unsupported class version error?

This is an error you receive when your Java Virtual Machine or JVM attempts to read a file but the version numbers are not supported. This can happen in cases where your version of JVM is higher than the version used for that file.