java io读取数据

1.字节流读取数据 

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

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();
            }
        }




    }

}


 

 优化:

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.字节流复制文件:

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);

    }

}


 

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

用idea读取该记事本的数据 

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

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();

    }

}

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();

    }
}

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值