Java 读取文本文档中的内容

文本文档位于工程下。

鼠标右击工程,选择“new - File”,即可创建。

文本文档的格式:GBK

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 
 7 public class IOTest01 {
 8 
 9     public static void main(String[] args) {
10         File src = new File("src.txt");
11         InputStream is = null;
12         
13         try {
14             is = new FileInputStream(src);
15             int temp;
16             while ((temp = is.read()) != -1) {
17                 System.out.print((char)temp);
18             }
19         } catch (FileNotFoundException e) {
20             e.printStackTrace();
21         } catch (IOException e) {
22             e.printStackTrace();
23         } finally {
24             try {
25                 if (is != null) {
26                     is.close();                
27                 }
28             } catch (IOException e) {
29                 e.printStackTrace();
30             }
31             System.out.println("\nDone!");
32         }
33     }
34 }

猜你喜欢

转载自www.cnblogs.com/Satu/p/10002779.html
今日推荐