好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java inputstream和outputstream使用详解

计算机在进行I/O时都是以流的形式来进行,Java中所有流的相关操作的类,都继承自以下四个抽象类。

  输入流 输出流
字节流 InputStream OutputStream
字符流 Reader Writer

InPutStream的实现

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

 

public class TestFileInPutStream {

     public static void main(String[] args) {

         try {

             File file = new File( "D:/test/testIO.java" );

             // 如果文件存在,读取文件中的内容,并在控制台输出

             if (file.exists()) {

                 InputStream in = new FileInputStream(file);

                 int a = 0 ;

                 while ((a = in.read()) != - 1 ) {

                     System.out.print(( char ) a);

                 }

                 in.close();

 

             } else {

                 // 如果文件不存在返回文件不存在

                 System.out.println( "文件不存在" );

 

             }

 

         } catch (FileNotFoundException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         } catch (IOException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         }

 

     }

}

在D盘已经存在testIO文件如下:

将文件中的内容输出到控制台,结果如下:

OutPutStream的实现

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

public class TestOutPutStream {

     private static InputStream in;

     private static OutputStream out;

     public static void main(String[] args) {

        

         try {

             in = new FileInputStream( "D:/test/testIO.java" );

             if (in == null ){

                 //原文件不存在

                 System.out.println( "原文件不存在" );

             } else {

                 //原文件存在,判断目标文件是否存在

                 File file = new File( "D:/test/testIOO.txt" );

                 if (!file.exists()){

                     //目标文件不存在,创建目标文件

                     file.getParentFile().mkdirs();

                     file.createNewFile();

                 }

                 //将原文件内容读取到目标文件

                 out = new FileOutputStream(file);

                 int a = 0 ;

                 while ((a = in.read()) != - 1 ){

                     out.write(a);

                 }

                

             }

         } catch (FileNotFoundException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         } catch (IOException e) {

             // TODO Auto-generated catch block

             e.printStackTrace();

         } finally {

             //流关闭

             try {

                 if (in != null ){

                     in.close();

                 }

                 if (out != null ){

                     out.close();

                 }

                

             } catch (IOException e) {

                 // TODO Auto-generated catch block

                 e.printStackTrace();

             }

         }

     }

}

D盘中原文件存在,在D盘中创建了目标文件

注意:在判断原文件是否存在时,直接判断字节流文件对象是否存在

到此这篇关于Java inputstream和outputstream使用详解的文章就介绍到这了,更多相关Java inputstream和outputstream内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_38363025/article/details/79626884

查看更多关于Java inputstream和outputstream使用详解的详细内容...

  阅读:20次