【转载】Java IO基础总结

 

      Java中使用IO(输入输出)来读取和写入,读写设备上的数据、硬盘文件、内存、键盘......,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是输出流。

      根据处理的数据类型可分为字节流和字符流

  1.字节流可以处理所有数据类型的数据,在java中以Stream结尾

  2.字符流处理文本数据,在java中以Reader和Writer结尾。

       我们来看个IO流的详解图:

  

 IO流的本质是对字节和字符的处理,那么我们平时也是用来处理文件的,就从文件处理开始接触这方面的知识。

1.文件操作(创建文件和文件夹,查看文件)

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
//创建一个文件路径
File file = new File("D:\testData.txt");
if(file.exists()){
//得到文件路径
System.out.println(file.getAbsolutePath());
//得到文件大小
System.out.println("文件大小:"+file.length());
}
//创建文件和创建文件夹
File file1 = new File("d:\iotest.txt");
if(!file1.exists())
{
    try {
        file1.createNewFile();
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}else{
    System.out.println("文件已存在");
}
//创建文件夹
File file2 = new File("d:\testIO");
if(file2.isDirectory())
{
    System.out.println("文件夹存在");
}else{
    file2.mkdir();
}
 
//列出一个文件夹下的所有文件
File f = new File("d:\testIO");
if(f.isDirectory())
{
    File lists[] = f.listFiles();
    for(int i=0;i<lists.length;i++)
    {
        System.out.println(lists[i].getName());
    }
}

常用字节流FileInputStream和FileOutputStream:

FileInputStream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\testData.txt");
            byte bytes[]=new byte[1024];
            int n=0;
            while((n=fis.read(bytes))!= -1){
                String str = new String(bytes,0,n);
                System.out.print(str);
            }
        catch (Exception e) {
            e.printStackTrace();
        finally{
            try {
                fis.close();
            catch (IOException e) {
                e.printStackTrace();
            }
        }

 

查看输出:

FileOutputStream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\testData.txt");
            String str = "报效国家,舍生忘死";
            byte bytes[] = str.getBytes();
            fos.write(bytes);
        catch (Exception e) {
            e.printStackTrace();    
        finally {
            try {
                fos.close();
            catch (Exception e2) {
                e2.printStackTrace();
            }
        }

查看一下:

如果是续写文件,则可以加上参数:

字符流FileReader和FileWriter:

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
//字符流
        //文件写出  输入流
        FileReader freader = null;
        //写入到文件  输出流
        FileWriter fwriter = null;
        try {  
            //创建输入对象
            freader = new FileReader("d:\testData.txt");
            //创建输出对象
            File f1 = new File("e:\testData.txt");
            if(!f1.exists()){
                f1.createNewFile();
            }
            fwriter = new FileWriter(f1);
             
            //读入到内存
            char chars[] = new char[1024];
            int n=0;
            while((n=freader.read(chars))!= -1)
            {
                fwriter.write(chars);
                //System.out.println(chars);
            }
             
             
             
        catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }finally{
            try{
            freader.close();
            fwriter.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
         
         
         
        //缓冲字符流  bufferedReader  bufferedWriter
        BufferedReader bfreader = null;
        try {
            FileReader freader = new FileReader("d:\testData.txt");
            bfreader = new BufferedReader(freader);
            //循环读取
            String s ="";
            while((s=bfreader.readLine())!= null)
            {
                System.out.println(s);
            }
        catch (Exception e) {
            // TODO: handle exception
        }

  

原文地址:https://www.cnblogs.com/appium/p/10120539.html