栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java io流专题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java io流专题

 来自哔哩哔哩韩顺平讲Java

package com.file;

import org.junit.Test;

import java.io.File;


public class FileInformation {
    public static void main(String[] args) {

    }
    //获取文件的信息
    @Test
    public void info(){
        //先创建文件对象
        File file = new File("d:\news1.txt");
        //调用相应的方法得到对应信息
        System.out.println("文件名称="+file.getName());

        System.out.println("文件绝对路径="+file.getAbsolutePath());

        System.out.println("文件父级目录="+file.getParent());

        //utf8字符集编码格式:一个英文字母对应一个字节数,一个汉字对应三个字节数
        System.out.println("文件大小(字节)="+file.length());

        System.out.println("文件是否存在="+file.exists());//T

        System.out.println("是不是一个文件="+file.isFile());//T

        System.out.println("是不是一个目录="+file.isDirectory());//F

    }
}

 

package com.file;

import org.junit.Test;

import java.io.File;
import java.io.IOException;


public class FileCreate {
    public static void main(String[] args) {

    }

    //方式1:new File(String pathname)
    @Test
    public void create01() {
        String filePath = "d:\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //方式2: new File(File parent,String child)//根据父目录文件+子路径构建
    //d:\news2.txt     parent是d:\
    @Test
    public void create02(){
        File parentFile = new File("d:\");
        String fileName="news2.txt";
        //这里的file对象,在Java程序中,只是一个对象
        File file = new File(parentFile, fileName);
        try {
            //只有执行了createNewFile方法,才会在磁盘创建该文件
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3  new File(String parent,String child)//根据父目录+子路径构建
    @Test
    public void create03(){
        //两种
        String parentPath="d:\";
        //String parentPath="d:/";
        String fileName="news3.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.file;

import org.junit.Test;

import java.io.File;
import java.io.Reader;


public class Directory {
    public static void main(String[] args) {

    }

    //判断 d:\news1.txt 是否存在,如果存在就删除
    @Test
    public void m1(){
        String filePath="d:\news1.txt";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(filePath+"删除成功");
            }else{
                System.out.println(filePath+"删除失败");
            }
        }else{
            System.out.println(filePath+"该文件不存在");
        }
    }

    //判断 d:\demo02是否存在,存在就删除,否则提示不存在
    //这里需要体会到,在Java编程中,目录也被当作文件
    @Test
    public void m2(){
      String filePath="d:\demo02";
        File file =new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }else{
            System.out.println("目录不存在");
        }
    }

    //判断 d:\demo\a\b\c 目录是否存在,如果存在就提示已经存在,否则就创建
    @Test
    public void m3(){
        String directoryPath="d:\demo\a\b\c";
        File file =new File(directoryPath);
        if(file.exists()){
            System.out.println(directoryPath+"目录已经存在");
        }else{
            System.out.println("目录不存在");
            //创建多级目录用mkdirs  d:demoabc
            //创建一级目录用mkdir  d:demo
            if (file.mkdirs()) {
                System.out.println(directoryPath+"创建成功");
            }else {
                System.out.println(directoryPath+"创建失败");
            }
        }
    }


}

 

package com.inputStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class FileInputStreamCode {
    public static void main(String[] args) {

    }

    
    @Test
    public void readFile01(){
        String filePath="d:\hello.txt";

        int readData=0;
        FileInputStream fileInputStream =null;
        try {
            //创建FileInputStream 对象,用于读取文件
           fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止
            //如何返回-1,表示读取完毕
           while ((readData= fileInputStream.read())!=-1){
               System.out.print((char)readData);//转成char显示
           }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    
    @Test
    public void readFile02(){
        String filePath="d:\hello.txt";
        byte[] buf=new byte[8];//一次读取8个字节
        int readLen=0;
        FileInputStream fileInputStream =null;
        try {
            //创建FileInputStream 对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止
            //如何返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while ((readLen=fileInputStream.read(buf))!=-1){
                System.out.println(new String(buf,0,readLen));//显示
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 

package com.outPutStream;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;


public class FileOutputStream01 {
    public static void main(String[] args) {

    }

    
    @Test
    public void writeFile() {

        String filePath = "d:\b.txt";
        //创建 FileOutputStream对象
        FileOutputStream fileOutputStream = null;
        try {
            //得到FileOutputStream对象 对象
            //1.new FileOutputStream(filePath)创建方式,当写入内容时,会覆盖原来的内容
            //2.new FileOutputStream(filePath,true)创建方式,当写入内容时,是追加到文件后面
            fileOutputStream = new FileOutputStream(filePath,true);
            //写入一个字节
            //fileOutputStream.write('H');
            // 写入字符串
            String str="hhhhhello,world";
            //str.getBytes() 可以把 字符串->字节数组
            //write(byte[] b,int off,int len) 将 len字节从位于偏移量  off的指定字节数组写入此文件输出流
            fileOutputStream.write(str.getBytes(),0,str.length());
            //fileOutputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

package com.outPutStream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileCopy {
    public static void main(String[] args) {
        //完成 文件拷贝,将 d:\a.jpg拷贝c:\
        //思路分析:
        //1.创建文件的输入流,将文件读入到程序
        //2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
        String srcFilePath="d:\a.jpg";
        String destFilePath="d:\a\a.jpg";
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;

        try {
            fileInputStream=new FileInputStream(srcFilePath);
            fileOutputStream=new FileOutputStream(destFilePath);
            //定义一个字节数组,提高读取效率
            byte[] buf=new byte[1024];
            int readLen=0;
            while ((readLen=fileInputStream.read(buf))!=-1){
                 //读取到后,就写入到文件 通过 fileOutputStream
                //即,是一边读,一边写
                fileOutputStream.write(buf,0,readLen);//一定要使用这个方法
            }
            System.out.println("拷贝ok");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭输入流和输出流
                if (fileInputStream!=null) {
                    fileInputStream.close();
                }
                if(fileOutputStream!=null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号