Difference between revisions of "Read a Text File"
(→Using the try structure) |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{topTenTips}} | {{topTenTips}} | ||
− | <question lang="java" className="Demo" copyFile="haggis.txt" >You | + | <question lang="java" className="Demo" copyFile="haggis.txt" >You can open a text file and read each line. <prog> |
import java.io.BufferedReader; | import java.io.BufferedReader; | ||
import java.io.FileReader; | import java.io.FileReader; | ||
Line 19: | Line 19: | ||
<code>FileReader</code>. | <code>FileReader</code>. | ||
*The while condition <code>(s=fh.readLine())!=null</code> does two | *The while condition <code>(s=fh.readLine())!=null</code> does two | ||
− | things, it assigns the string s and it checks for null. | + | things, it assigns the string s and it checks for null. |
− | <p> | + | <p> |
The text file | The text file | ||
[haggis.txt] | [haggis.txt] | ||
Line 26: | Line 26: | ||
some lines of text. | some lines of text. | ||
</p> | </p> | ||
+ | </question> | ||
+ | |||
+ | ==Using the try structure== | ||
+ | <question lang="java" className="Demo" copyFile="haggis.txt" > | ||
+ | You can use the try feature in Java 7. This ensures that the file will be closed and disposed of even if the read fails. | ||
+ | *If the file is missing then the exception will be raised and caught. | ||
+ | <prog> | ||
+ | import java.io.BufferedReader; | ||
+ | import java.io.FileReader; | ||
+ | public class Demo{ | ||
+ | public static void main(String[] argv){ | ||
+ | try(BufferedReader br = | ||
+ | new BufferedReader(new FileReader("hggis.txt"))) | ||
+ | { | ||
+ | for(String line = br.readLine();line!=null;line=br.readLine()) | ||
+ | System.out.println(line); | ||
+ | } | ||
+ | catch(Exception ex){ | ||
+ | System.err.println("Exception handled: "+ex); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </prog> | ||
+ | </question> | ||
+ | |||
+ | |||
+ | ==Using the try structure== | ||
+ | <question lang="java" className="Demo" copyFile="haggis.txt" > | ||
+ | You can use the streams from Java 8. | ||
+ | *In the '''forEach''' statement there is a lambda expression - s is a string in this | ||
+ | <prog><![CDATA[ | ||
+ | import java.nio.file.*; | ||
+ | import java.util.stream.Stream; | ||
+ | public class Demo{ | ||
+ | public static void main(String[] argv)throws Exception{ | ||
+ | try (Stream<String> st = Files.lines(Paths.get("haggis.txt"))) { | ||
+ | st.forEach(s -> System.out.println(s)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ]]></prog> | ||
</question> | </question> |
Latest revision as of 10:31, 27 September 2015
You can open a text file and read each line.
- The
BufferedReader
represents a text file. - You can create
BufferedReader
from an
FileReader
.
- The while condition
(s=fh.readLine())!=null
does two
things, it assigns the string s and it checks for null.
The text file [haggis.txt] includes some lines of text.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Using the try structure
You can use the try feature in Java 7. This ensures that the file will be closed and disposed of even if the read fails.
- If the file is missing then the exception will be raised and caught.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Using the try structure
You can use the streams from Java 8.
- In the forEach statement there is a lambda expression - s is a string in this
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]