java io

package com.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class BufferedReaderDemo01 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("./src/main/java/com/io/FISDemo1.java"),
                                StandardCharsets.UTF_8
                )
        );
        String line = null;
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
        br.close();
    }
}

package com.io;

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

public class CreateNewFileDemo {

    public static void main(String[] args) throws IOException {
        //在当前目录下新建一个文件:test.txt
        File file = new File("./test.txt");
        //boolean exists()判断当前File表示的位置是否已经实际存在该文件目录
        if(file.exists()){
            System.out.println("该文件已存在!");
        }else{
            file.createNewFile();//将File表示的文件创建出来
            System.out.println("文件已创建");
        }
    }
}

package com.io;

import java.io.File;

/**
 * 删除一个目录
 */
public class DeleteDirDemo {
    public static void main(String[] args) {
        File dir = new File("demo");

        if(dir.exists()){
            dir.delete();
            System.out.println("目录已删除");
        }else{
            System.out.println("目录不存在");
        }
    }
}

package com.io;

import java.io.File;

public class DeleteFileDemo {
    public static void main(String[] args) {
        File file = new File("test.txt");
        if(file.exists()){
            file.delete();
            System.out.println("文件已删除");
        }else {
            System.out.println("文件不存在");
        }
    }
}

package com.io;

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

public class FileCopyDemo01 {

    public static void main(String[] args) throws IOException {
        FileInputStream fis =
                new FileInputStream("a.jpg");
        FileOutputStream fos =
                new FileOutputStream("b.jpg");

        int n = -1;
        while((n=fis.read())!=-1){
            fos.write(n);
        }

        fis.close();
        fos.close();
    }
}

package com.io;

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

public class FileCopyDemo2 {

    public static void main(String[] args) throws IOException {
        //实现思路
        //1读取文件内容
        FileInputStream fis = new FileInputStream("a.jpg");

        FileOutputStream fos = new FileOutputStream("b.jpg");

        //读取数据(复制)
        //2.1定义数组用于临时存储读取到数据
        byte[] data = new byte[1024];//这里数组的大小是自定义
        //2.2循环读取数据
        int length = -1;
        while((length=fis.read(data))!=-1){
            fos.write(data,0,length);//读了多少写多少  ,0表示起始位置,length表示写入的字节数
        }
        //3.关闭流对象
        fis.close();
        fos.close();
    }

}

package com.io;

import java.io.*;

public class FileCopyDemo3 {
    public static void main(String[] args) throws IOException {
            //
        BufferedInputStream bis =
                new BufferedInputStream(
                        new FileInputStream("./a.jpg"));
        BufferedOutputStream bos =
                new BufferedOutputStream(
                        new FileOutputStream("./c.jpg")
                );
        //2.读写数据(拷贝
        byte[] data = new byte[16];
        int len;
        while ((len = bis.read(data))!= -1){
            bos.write(data,0,len);
        }

        bos.flush();//这里表示刷新缓冲区内容到文件
        //3.关闭流对象
        bis.close();
        bos.close();
    }
}

package com.io;

import java.io.File;

public class FileDemo {
    public static void main(String[] args) {
        File file = new File("./demo.txt");
        //获取名字
        String name = file.getName();
        System.out.println(name);
        //获取文件大小(单位是字节)
        long len = file.length();
        System.out.println(len+"字节");
        //是否可读可写
        boolean cr = file.canRead();
        boolean cw = file.canWrite();
        System.out.println("是否可读"+cr);
        System.out.println("是否可写"+cw);
        //是否隐藏
        boolean ih = file.isHidden();
        System.out.println("是否隐藏:"+ih);
    }
}

package com.io;

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

public class FISDemo1 {

    public static void main(String[] args) throws IOException {
        //构建文件输入流对象
        FileInputStream fis = new FileInputStream("jsd/2406/fos.dat");
        int n = 0;
        while((n=fis.read())!=-1){
            System.out.println(n);
        }
        //3.关闭流对象(水龙头有打开是不是也会有关闭,否则浪费资源
        fis.close();//流对象关闭后会释放内存
    }

}

package com.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FOSDemo01 {

    public static void main(String[] args) throws IOException {
        //  构建一个文件输出流对象
        //创建文件对象
        File file = new File("jsd/2406/fos.dat");
        //获取文件对象的目录结构
        File parent = file.getParentFile();// jsd/2406/
        System.out.println(parent);
        //假如目录不存在则创建目录mkdirs()
        if(!parent.exists()){//parent.exists()加入返回ture表示存在
            parent.mkdirs();//创建多层目录

        }

        FileOutputStream fos2 = new FileOutputStream(file);
        //向文件写入数据
        fos2.write(3);
        fos2.write(4);
        fos2.write(5);

        System.out.println("数据写入ok");
        //释放资源(关闭流对象)
        fos2.close();
    }
}

package com.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class InputStreamReaderDemo01 {

    public static void main(String[] args) throws IOException {
        InputStreamReader isr
                = new InputStreamReader(
                        new FileInputStream("./osw.txt"),
                StandardCharsets.UTF_8
        );
        int data = 0;
        while( (data = isr.read())!=-1){
            System.out.println((char)data);
        }
        isr.close();
    }
}

package com.io;

import java.io.File;

/**
 * 访问一个目录中的所有子项
 */
public class ListFileDemo1 {
    public static void main(String[] args) {
        //获取当前目录中的所有子项
        File dir = new File(".");

        /**
         * boolean isFile()
         * 判断当前File表示的是否为一个文件
         * boolean isDirectory()
         * 判断当前File表示的是否一个目录
         */
        if(dir.isDirectory()){
            File[] subs = dir.listFiles();
            System.out.println("当前目录包含"+subs.length+"个子项");
            for(int i = 0;i< subs.length;i++){
                File sub = subs[i];
                System.out.println(sub.getName());
            }
        }

    }
}

package com.io;

import java.io.File;

public class ListFilesDemo2 {

    public static void main(String[] args) {
        File dir = new File(".");
        if(dir.isDirectory()){
            File[] subs = dir.listFiles(f->f.getName().endsWith(".txt"));

            for(File sub:subs){
                System.out.println(sub.getName());
            }
        }
    }
}

package com.io;

import java.io.Serializable;

public class Message implements Serializable {
    private static final long serialVersionUID = 7613180213416277986L;

    private Integer id;

    private transient String title;

    private String content;

    public Message(){}

    public Message(Integer id,String title,String content){
        this.id= id;
        this.title=title;
        this.content=content;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    //alt+insert
    @Override
    public String toString() {
        return "Message{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

package com.io;

import java.io.File;

public class MkDirDemo {
    public static void main(String[] args) {
        File dir = new File("demo");

        if(dir.exists()){
            System.out.println("该目录已存在");
        }else{
            dir.mkdirs();//     创建目录时会将路径上所有不存在的目录一同创建
            System.out.println("目录已创建");

        }
    }
}

package com.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo02 {
    public static void main(String[] args) throws IOException,ClassNotFoundException {
        ObjectInputStream ois =
                new ObjectInputStream(
                        new FileInputStream("message.txt")
                );
        Message msg = (Message)ois.readObject();
        System.out.println(msg);
        ois.close();
    }
}

package com.io;

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

public class ObjectOutputStreamDemo01  {
    public static void main(String[] args) throws IOException {
        Message m1 = new Message(1,"传奇老师病了","做了一个小手术");
        ObjectOutputStream oos =
                new ObjectOutputStream(
                        new FileOutputStream("message.txt")
                );
        oos.writeObject(m1);
        System.out.println("序列化OK");
        oos.close();
    }
}

package com.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

public class OutputStreamWriteDemo01 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw =
                new OutputStreamWriter(
                        new FileOutputStream("./osw.txt"),
                        StandardCharsets.UTF_8
                );

        //2.写数据
        osw.write("你好");
        osw.write("子干老师 ");
        System.out.println("数据写出完毕");
        osw.close();
    }
}

package com.io;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintWriteDemo01 {

    public static void main(String[] args) throws FileNotFoundException {

        PrintWriter pw = new PrintWriter("./pw.txt");

        pw.println("hello");
        pw.println("world");
        pw.println("Java");
        pw.close();
    }
}

package com.io;

import java.io.*;

public class PrintWriteDemo2 {
    public static void main(String[] args)throws FileNotFoundException {
        PrintWriter printWriter = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream("./pw.txt",true)
                        )
                ),true
        );
        printWriter.println("hello");
        printWriter.println("Java");

        printWriter.close();
    }
}

package com.io;

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

public class ReadStringDemo01 {

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

        FileInputStream fis = new FileInputStream("./f1.txt");


        //available获取流中有效字节数
        byte[] data = new byte[fis.available()];
        int len = fis.read(data);

        String str = new String(data,0,len);
        System.out.println(str);
        fis.close();
    }
}
package com.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class SimpleNoteDemo1 {

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(
                new FileOutputStream("mynote.txt",true),
                true
        );

        while(true){
            System.out.println("请输入要存储的数据");
            String line = sc.nextLine();
            if("exit".equalsIgnoreCase(line)){
                break;
            }
            pw.println(line);
        }
        sc.close();
        pw.close();
    }

}

package com.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class WriteStringDemo01 {

    public static void main(String[] args) throws IOException {
        String str = "我爱Java";
        //构建文件输出流对象
        FileOutputStream fos =  new FileOutputStream("./f1.txt");
        //写字符数据
        byte[] data = str.getBytes(StandardCharsets.UTF_8);
        fos.write(data);
        str="Hello Tedu";
        fos.write(str.getBytes(StandardCharsets.UTF_8));
        //释放资源
        fos.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值