当前位置:学者斋 >

计算机 >java语言 >

Java如何读取文本文件

Java如何读取文本文件

Java具有简单性、面向对象、分布式、健壮性、安全性、平台的独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。以下是小编为大家搜索整理的java如何读取文本文件,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!

Java如何读取文本文件

下面的代码是读取文本文件的'例子,程序会读取文件,并将它的内容显示出来。

1 import eredReader;

2 import ;

3 import Reader;

4 import NotFoundException;

5 import ception;

6

7 public class ReadTextFileExample

8 {

9 public static void main(String[] args)

10 {

11 File file = new File("");

12 StringBuffer contents = new StringBuffer();

13 BufferedReader reader = null;

14

15 try

16 {

17 reader = new BufferedReader(new FileReader(file));

18 String text = null;

19

20 // repeat until all lines is read

21 while ((text = Line()) != null)

22 {

23 nd(text)

24 nd(roperty(

25 "rator"));

26 }

  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/java/jvkj59.html