文件操作相关代码(java)

创建文件

public class Demo01 {
    public static void main(String[] args) throws IOException {
        File file1 = new File("d:/1.txt");
        // 1 创建文件
        file1.createNewFile();
        File file2 = new File("d:/demo01");
        file2.mkdir();
        File file3 = new File("d:/demo01/demo02");
        file3.mkdirs();
public class Demo02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null ;
        try {
            // 1 准备输入输出流
             fis = new FileInputStream(new File("d:/jdk-15.0.2_windows-x64_bin.exe"));
             fos = new FileOutputStream(new File("C:/jdk15.exe"));
            // 2 循环读取操作
            int len = 0 ;
            byte [] buff  = new byte[1024]; // 开辟缓冲区
            long start = System.currentTimeMillis();
            while ((len = fis.read(buff)) != -1 ) {
                fos.write(buff , 0 ,len );
            }
            long end = System.currentTimeMillis();
            System.out.println("时间"+ (end - start) );  // 1265
            // 3 关闭输入输出资源
            fis.close();
            fos.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

reader和 write

  public static void main(String[] args) throws Exception {
        // reader和 writer 只能操作文本内容
        Reader reader = new FileReader("d:/1.txt");
        FileWriter fileWriter = new FileWriter("d:/111.txt");
        char [] buff = new char[1024];
        int len = 0 ;
        while ( (len= reader.read(buff)) != -1 ) {
            fileWriter.write( buff , 0 , len ) ;
        }
        fileWriter.flush();

        // 关闭输入资源
        reader.close();
        fileWriter.close();

    }
}

public class Demo03 {
    public static void main(String[] args) throws IOException {
        method1();
    }
    public static void method1() throws IOException {
        // 创建输入流对象
        InputStream is = System.in;
        // 转换
        InputStreamReader isr = new InputStreamReader(is);
        // 创建输出流对象
        FileWriter fw = new FileWriter("d:/demo03.txt");

        // 读写数据
        char[] bys = new char[1024];
        int len;
        while ((len = isr.read(bys)) != -1) {
            fw.write(new String(bys, 0, len));
            fw.flush();
        }
        // 释放资源
        is.close();
        fw.close();
    }

}

输出流装换器

  // 输出流装换器
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(""));
        OutputStreamWriter osw  = new OutputStreamWriter(System.out); // 装换器 stream ->writer
        String line = null;
        while ((line = (br.readLine())) !=null);{
            osw.write(line+"\n");
        }

        osw.flush();

        br.close();
        osw.close();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.什么是流? 流是一个抽象的概念。当Java程序需要从数据源读取数据时,会开启一个到数据源的流。数据源可以是文件...
    yuan_hh阅读 849评论 1 0
  • 心得体会 如果有山的话,就有条越过它的路^^ 今日所学 1.文件的相关操作 1.如何创建文件 2.I/O流 3.文...
    宁晓鸯阅读 652评论 0 2
  • 创建文件 完整的路径 文件的copy pathString path="";File file=new Fi...
    信1张欣欣阅读 169评论 0 0
  • Java里面,将IO流体系按操作方式分类,可分为字节流和字符流。相关类结构图如下: 字节流相关的基类是InputS...
    zackyG阅读 2,409评论 0 1
  • 一、文件 File 类相关知识点整理 1、File 基本概念及操作 1)、概念: 文件和目录(文件夹)的路径名的抽...
    吾乃韩小呆阅读 305评论 0 0

友情链接更多精彩内容