首页 > 其他分享 >day13 I/O流——字节输入输出流、字符输入输出流 & File常用类 & (字节)复制大文件

day13 I/O流——字节输入输出流、字符输入输出流 & File常用类 & (字节)复制大文件

时间:2022-10-19 20:56:54浏览次数:57  
标签:字节 File 输入输出 len file new out

day13

I/O流

定义:数据在两设备传输称为流,流是一组有顺序的,有起点和终点的字节集合

I 是input的缩写,表示输入流

O是output缩写,表示输出流

字节流(视频等)

输入InputStream FileInputStream DataInputStream

输出OutputStream FileOutputStream DataOutputStream

public static void main(String[] args){
    try{
        inputData();//调用字节输入流
    }catch(IOException e){
        e.printStackTrace();
    }
}
//===========字节流==========
//-----------字节输入流-------

public static void inputData() throws IOException{
    //1.创建file对象,加载文件物理路径
	File file = new File("D://siTu/测试.txt");
    //2.创建字节输入流
    InputStream in = new FileInputStream(file);
    //3.返回字节输入流的刻度字节长度
    int len = in.available();
    /**全英文文档
    for(int i = 0; i < size; i++){
        char c = (char)in.read();//in.read()返回ascii码的十进制数
        //read()返回的是ascii码的十进制数,每个数字对应一个字符
    }
    */
    
    /**含中文文档
    	先创建一个byte数组用于接收读取的内容
    	for循环把读取的数据全都强转后放在byte数组中
    	输出一个byte数组转成的字符串,从而处理中文
    */
    byte[] b = new byte[len];
    for(int i = 0; i < b.length; i++){
        b[i] = (byte)in.read();
    }
    System.out.println(new String(b));
}
//-----------字节输出流—-----------
public static void outputStream{   
    //新建文件对象,并且获取物理地址
    File file = new File(D://siTu/测试.txt);
    //输入需要添加的字符
    String str = "元气";
    //创建字节输出流FileOutputStream(File file, boolean append)是否在file文件后拼接,true拼接
    OutputStream out = new FileOutputStream(file,true);
    //OutputStream out = new FileOutputStream(file);
	
    out.write(out.getByte());//把字符串转化成字节数组后 再写入文件
    out.flush();
    out.close();
    
}

字符流(文档)

输入 Reader FileReader

​ BufferedReader

输出 Writer FileWriter

​ BufferedReader

字符输入流读文件

//===========字符流==========
//-----------字符输入流-------

public static void OutputDate(){   
    File file = new File("D://siTu/测试.txt");
    InputStream inp = new InputStream(file);
    InputSteramReader reader = new InputStreamReader(inp,"UTF-8");
    BufferedReader br = new BufferedReader(reader);
/**    
    String str = null;
    while((str = br.readLine()) != null){
        System.out.println(str);
    }
*/
    byte[] bt = new byte[1024];
/*
    int len = in.read(bt);//每次读取指定数组长的字节数,并将读到的字节传入大数组中,返回读到的字节长度
    while(len != -1){
        out.write(bt);
        len = in.read(bt);
    }
*/
	int len = -1;
    while((len = in.read(bt)) != -1){
        out.write(bt);
    }
    out.close();
    in.close();
}

字符输出流

//-----------字符输出流-------

public static void outputreader(){
    File file = new File("D://siTu/测试1.txt");
    String str = "zg";
    OutputStream out = null;
    OutputStreamWriter writer = null;
    BufferedWriter bw = null;
        
    try{
        out = new OutputStream(file,true);
        writer = new OutputStreamWriter(out,"UTF-8");
        bw = new BufferedWriter(writer);
        
        bw.write("1111");
        bw.newLine();
        bw.write("aaaa");
        
        bw.flush();
    }catch(IOException e){
        e.printStrackTrace();
    }finally{
        (try catch)if(bw != null) bw.close();
        (try catch)if(writer != null) writer.close();
        (try catch)if(out != null) out.close();        
    }
}

File类常用的方法

File file = new File("D://siTu/ceshi.txt");

1.文件是否存在

boolean has = file.exists();

2.是否是 一个文件

boolean b = file.isFile();

3.判断是否是一个文件夹(目录)

boolean b1 = file.isDirectory();

4.获取当前目录下所有文件名

String[] fileNames = file.list();

5.获取当前目录下所有文件对象

File[] files = file.listFiles();

6.删除文件或者文件夹(boolean)

file.delete();

7.当文件,文件夹存在时,执行删除

file.deleteOnExit();

8.创建文件夹boolean

file.mkdir();

9.获取文件名

String name = file.getName();

10.创建新的文件

try {
    boolean tar2 = file.createNewFile();
    System.out.println(tar2);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

11.判断一个文件是否可读boolean

file.canRead();

12.判断一个文件是否可写boolean

file.canWrite();

复制大文件

public static void copy(){
        InputStream in = new FileInputStream(new File("D://siTu/测试2.txt"));
    OutputStream out = new FileOutputStream(new File("D://siTu/测试22.txt"));

/**    int len = in.available();
    syso(len);
    for(int i = 0; i < len ; i++){
        out.write(in.read);
    }
    out.close();
*/
 /**   
    byte[] bt = new byte[1024];
    int len = in.read(bt);
    while(len != -1){
        out.write(bt);
        len = in.read(bt);
    }
    out.close();
    in.close();
*/
    
	byte[] bt = new byte[1024];
    int len = -1
    while((len = in.read(bt) != -1){
        out.write(bt);
    }
    out.close();
    in.close();
}

标签:字节,File,输入输出,len,file,new,out
From: https://www.cnblogs.com/xiaoto9426/p/16807726.html

相关文章