java io读取数据

1.字节流读取数据 

java io读取数据_第1张图片

2.字节流读取数据: read()

java io读取数据_第2张图片

package wwx;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Test {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;


        try {
            fileInputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {

            int read = fileInputStream.read();
            System.out.println((char)read);
            int read1 = fileInputStream.read();
            System.out.println((char)read1);
            int read2 = fileInputStream.read();
            System.out.println((char)read2);

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }

}


 java io读取数据_第3张图片

 优化:

package wwx;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Test {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;


        try {
            fileInputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {

            int read ;

//            System.out.println((char)read);
//            int read1 = fileInputStream.read();
//            System.out.println((char)read1);
//            int read2 = fileInputStream.read();
//            System.out.println((char)read2);

//            while (read != -1) {
//                System.out.println((char) read);
//                read = fileInputStream.read();
//            }
            //优化
            while ((read = fileInputStream.read()) != -1) {
                System.out.print((char) read);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}


3.字节流复制文件:

java io读取数据_第4张图片

package wwx;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Test {

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("E:\\Test\\wwx.txt");

        FileOutputStream fileOutputStream = new FileOutputStream("E:\\cTest\\wwx.txt");
       
        int by;
        while ((by = fileInputStream.read()) != -1) {
            fileOutputStream.write(by);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }


}


 4.byte数组转为字符串:new String(byte)方法

package wwx;
public class Test {
    public static void main(String[] args) {

        byte[] bytesArray={97,98,99};

        String s = new String(bytesArray);//用构造方法创建的,值为正常值

        System.out.println("byte数组转为字符串形式:"+s);


       String str1="3,4,5,67";

       byte[] bytes1=str1.getBytes();//该方法返回值为内存地址

       System.out.println("字符串转为byte数组"+bytes1);

    }

}


 java io读取数据_第5张图片

5.字节流以字节数组形式读取数据:

用idea读取该记事本的数据 

该记事本f之后换行,换行为一个\n,占两个字节 

java io读取数据_第6张图片java io读取数据_第7张图片

package wwx;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Test {
    public static void main(String[] args) throws IOException {

        FileInputStream input = new FileInputStream("E:\\wwx\\xx\\wx.txt");

        byte[] bytes = new byte[5];
        System.out.println("第1次读取数据");
        int bytesLen = input.read(bytes);
        System.out.println("byte数组长度:"+bytesLen);
        System.out.println(new String(bytes));

        System.out.println("第2次读取数据");
        bytesLen = input.read(bytes);
        System.out.println("byte数组长度:"+bytesLen);
        System.out.println(new String(bytes));


        System.out.println("第3次读取数据");
        bytesLen = input.read(bytes);
        System.out.println("byte数组长度:"+bytesLen);
        System.out.println(new String(bytes));

        input.close();

    }

}

java io读取数据_第8张图片

6.要求一次性读取wx.txt 中13个字节数据 :

package wwx;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
/*
要求一次性读取wx.txt 中13个字节数据
 */
public class Test {
    public static void main(String[] args) throws IOException {

        FileInputStream inputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");

        byte[] bytes = new byte[13];

        int len = inputStream.read(bytes);

        System.out.println("byte数组字节长度:"+len);

        System.out.println(new String(bytes));

        inputStream.close();

    }
}

java io读取数据_第9张图片

7.字节流以1024字节读取数据:

 

package wwx;
import java.io.*;
/*
要求一次性读取wx.txt 中13个字节数据
 */
public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\wwx\\xx\\wx.txt");

        byte[] bytes = new byte[1024];

        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            System.out.println("byte数组字节长度:" + len);
            System.out.println(new String(bytes,0,len));
        }
        inputStream.close();
    }
}

java io读取数据_第10张图片

你可能感兴趣的:(java基础,java,开发语言)