JAVA的I/O(输入输出流)

本文详细介绍了Java中的I/O流,包括File类的使用,如创建、删除文件和遍历文件夹。重点讲解了字节流(字节输出流、字节输入流、字节输入缓冲流)和字符流(字符输出流、字符输入流),以及如何处理文件复制、加密和乱码问题。同时提到了字符缓冲流在提高效率中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

File类:

File类中的java.io包是唯一代表磁盘文件本身对象。

创建File对象:

File file = new File("D:\aa\hello.txt");

获取文件的名字和路径:

public static void main(String[] args) {
        File file = new File("\\aa\\hello.txt");
        //获取绝对路径
        String absolutePath = file.getAbsolutePath();
        System.out.println("absolutePath = " + absolutePath);

        //获取创建时传入的路径
        String path = file.getPath();
        System.out.println("path = " + path);

        //获取file对象的文件名/目录(重要)
        String name1 = file.getName();
        System.out.println("name1 = " + name1);

        //获取file大大小,当file大了之后,这里和电脑显示的会有误差
        long length = file.length();
        System.out.println("length = " + length);
    }

判断文件:

public static void main(String[] args) {
        File file = new File("\\aa\\hello.txt");

        //1,判断该文件是否存在
        boolean exists = file.exists();
        System.out.println("exists = " + exists);

        //2.判断file文件是不是文件夹
        boolean directory = file.isDirectory();
        System.out.println("directory = " + directory);

        //3.判断该file文件是不是文件
        boolean file1 = file.isFile();
        System.out.println("file1 = " + file1);
    }

创建文件和删除文件:

public static void main(String[] args) throws IOException {
        File file = new File("\\aa\\bb\\cc");

        //1.创建文件,如果不存在就创建出来,返回true,否则就不管,返回false
        boolean newFile = file.createNewFile();
        System.out.println("newFile = " + newFile);

        //2.删除文件/文件夹,如果要删除文件夹只能删除空文件夹,删除文件夹先删除里面的文件,再删除文件夹。
        boolean delete = file.delete();
        System.out.println("delete = " + delete);

        //3.创建单级文件夹
        boolean mkdir = file.mkdir();
        System.out.println("mkdir = " + mkdir);
        //创建多级文件夹
        boolean mkdirs = file.mkdirs();
        System.out.println("mkdirs = " + mkdirs);

    }

对文件夹进行遍历:

public static void main(String[] args) {
        File file = new File("D:\\aa");

        //得到文件夹中所有内容的名字
        String[] list = file.list();
        for (String s : list) {
            System.out.println("s = " + s);
        }

        System.out.println("------------------");


        //得到文件夹中所有内容的file对象
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println("file1 = " + file1);
        }
    }

IO流顾名思义输入输出流,输入(I)就是往磁盘中写数据,输出(O)就是读取磁盘数据

字节流:

字节输出流:

字节输出流(往内存向磁盘写数据)
public static void main(String[] args) throws IOException {

        //1.创建字节输出流
        FileOutputStream out = new FileOutputStream("D:\\aa\\hello.txt");

        //2.写入数据
        //2.1写入单个数据
        out.write(89);
        //2.2写入整个字节数组
        byte[] arr = {1,2,3};
        //out.write(arr);

        //2.3写入部分数组
        out.write(arr,0,1);

        //3.刷新流对象
        out.flush();
        //4.关流
        out.close();
    }

字节输入流:

读取数据

public static void main(String[] args) throws IOException {
        //创建字节输入流
        FileInputStream in = new FileInputStream("D:\\aa\\hello.txt");

        //读取信息
        //一次读一个字节
        /*int read = in.read();
        System.out.println("read = " + read);*/

        //一次读取多个字节
        byte[] arr = new byte[5];//创建一个数组,充当拖车
        int read = in.read(arr);//读取数组
        System.out.println(Arrays.toString(arr));
        System.out.println("read = " + read);

        //关流
        in.close();
    }

使用循环读取数据:

//使用循环读取数据
public class demo3 {
    public static void main(String[] args) throws Exception{
        FileInputStream in = new FileInputStream("D:\\aa\\hello.txt");

        //读取信息
        int read;//定义变量,代表一次读取几个字节
        byte[] arr = new byte[5];
        //使用while循环,完成读取完毕
        while ((read = in.read(arr)) != -1){
            String s = new String(arr, 0, read);
            System.out.println(s);
        }



        //关流
        in.close();

    }
}

对文件进行复制:

//文件的复制
public class demo4 {
    public static void main(String[] args) throws IOException {
        //读取,输入流
        FileInputStream in = new FileInputStream("D:\\aa\\b.txt");

        //写入,输出流
        FileOutputStream out = new FileOutputStream("D:\\aa\\a.txt");

        //循环读取数据并写入
        /*int xrc;
        byte[] arr = new byte[5];
        while ((xrc=in.read(arr))!=-1){
            out.write(arr,0,xrc);
            out.flush();
        }
        //关流,之前可以自动刷新流对象,可以省略out》flush();
        in.close();
        out.close();*/

        //一个字节一个字节的复制,
        int b;
        while ((b = in.read()) != -1){
            //b就是读取出来的值
            out.write(b+1);

        }

        in.close();
        out.close();
    }
}

对文件进行加密:

//对文件进行加密
//+1是加密,解密就再复制一次-1就好了(还有文件)
public class demo5 {
    public static void main(String[] args) throws IOException {

        FileInputStream in = new FileInputStream("D:\\aa\\aaa.jpg");
        FileOutputStream out = new FileOutputStream("D:\\aa\\22356209-6.jpg");

        int b;
        while ((b = in.read()) != -1){
            out.write(b-1);
        }
        in.close();
        out.close();
    }
}

字节输入缓冲流:

public class demo6 {
    public static void main(String[] args) throws IOException {
        //字节输入缓冲流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\aa\\x.txt"));
        //字节输出缓冲流
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\aa\\r.txt"));

        //缓冲流主要是提高效率

        int a;
        while ((a = in.read())!=-1){
            out.write(a+1);//加密是+1,解密是在循环一次-1
        }
        in.close();
        out.close();

    }
}

字符流:

字符流只能复制文本文档,其他都用字节流

字符输出流:

乱码的解决:

public class demo1 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "中国";

        //字符串-字节数组utf-8
        byte[] arrUTF = s.getBytes("UTF-8");
        System.out.println(Arrays.toString(arrUTF));

        //GBK
        byte[] arrGBK = s.getBytes("GBK");
        System.out.println(Arrays.toString(arrGBK));

        //乱码的出现,用不同的编码会出现乱码
        //字节数组还原字符串
        String s1 = new String(arrUTF, "UTF-8");
        System.out.println("s1 = " + s1);

    }
}

字符输出流;写文件

//字符输出流(写文件)
public class demo2 {
    public static void main(String[] args) throws IOException {
        //创建字符输出流,文件不存在就会自动创建,存在则会清空
        //开启续写功能就在后面添加一个参数true
        FileWriter in = new FileWriter("D:\\aa\\a.txt",true);

        //写数据
        //写入单个数据
        in.write("99");
        //写入一个数组
        char[] arr = {'q','x','e'};//写入的含头不含尾
        in.write(arr,0,1);//如果想全部写进去就不要在后面写参数,后面是根据索引写那些数据

        //写入字符串
        in.write("w中国人不偏中国人",0,4);//如果想全部写进去就不要在后面写参数,后面是根据索引写那些数据

        //关流
        in.close();

    }

}

字符输入流:读文件

//字符输入流(读取)
public class demo3 {
    public static void main(String[] args) throws IOException {
        //1.创建字符输入流
        FileReader out = new FileReader("D:\\aa\\a.txt");

        //2.读取
        //一次读取一个字符
        /*int b;//装字符
        while ((b=out.read())!=-1){
            System.out.println((char)b);
        }*/

        //一次读取多个字符
        char[] arr = new char[5];
        int len;//代表读取的个数
        while ((len=out.read(arr))!=-1){
            String s = new String(arr,0,len);
            System.out.println("s = " + s);
        }

        //3.关流
        out.close();
    }
}

字符流复制文本:

//使用字符流完成文件复制
//字符流只能复制文本文档,其他都用字节流
public class demo4 {
    public static void main(String[] args) throws IOException {
        //创建字符输出输入流
        FileReader fileReader = new FileReader("D:\\aa\\a.txt");
        FileWriter fileWriter = new FileWriter("D:\\aa\\c.txt",true);


        //边读取边写入
        int b;
        while ((b = fileReader.read()) != -1){
            fileWriter.write(b);
        }

        fileReader.close();
        fileWriter.close();
    }
}

字符缓冲流:

//重点
//字符缓冲流
public class demo5 {
    public static void main(String[] args) throws IOException {
        //字符缓冲输入流
    //    BufferedReader bufferedReader = new BufferedReader
      //      (new FileReader("D:\\aa\\a.txt"));

        /*String s = bufferedReader.readLine();
        System.out.println("s = " + s);

        String s1 = bufferedReader.readLine();
        System.out.println("s = " + s1);

        String s2 = bufferedReader.readLine();
        System.out.println("s = " + s2);

        //没有对象读取时返回null
        String s3 = bufferedReader.readLine();
        System.out.println("s = " + s3);*/

        /*String s;
        while ((s=bufferedReader.readLine())!=null){
            System.out.println(s);
        }

        //关流
        bufferedReader.close();*/


        //字符缓冲输出流
        BufferedWriter bufferedWriter = new BufferedWriter
    (new FileWriter("D:\\aa\\a.txt",true));//true是开启续写,是从最后一行开始续写

        bufferedWriter.write("11");


        //换行
        bufferedWriter.newLine();

        bufferedWriter.write("21");

        bufferedWriter.close();

    }
}

### DeepSeek-V2.5的实用集成涉及多个方面,包括环境配置、数据准备、模型调用和性能优化。 #### 环境配置 为了确保DeepSeek-V2.5能够顺利运行,需要搭建合适的开发环境。这通常涉及到安装必要的依赖库和支持工具。具体而言,可以使用虚拟环境来隔离项目的依赖项,从而避免版本冲突等问题[^1]。 ```bash python -m venv deepseek_env source deepseek_env/bin/activate # Linux/MacOS deepseek_env\Scripts\activate # Windows pip install -r requirements.txt ``` #### 数据准备 高质量的数据对于任何机器学习项目都至关重要。针对DeepSeek-V2.5的应用场景,需收集并预处理相应的训练数据集。这些操作可能包括清洗噪声、标注类别标签以及划分训练验证测试集合等步骤。 #### 模型调用 一旦完成了前期准备工作之后,则可以通过API接口轻松地调用DeepSeek-V2.5的功能模块来进行预测分析或其他任务执行工作。下面是一个简单的Python脚本示例: ```python from deepseek import load_model, predict model_path = "path/to/deepseek_v2_5" loaded_model = load_model(model_path) input_data = [...] # 输入待测样本特征向量列表 predictions = predict(loaded_model, input_data) print(predictions) ``` #### 性能优化 最后,在实际部署过程中还需要关注系统的响应速度与资源利用率等方面的表现情况,并据此采取相应措施加以改进。例如调整超参数设置、采用分布式计算框架等方式提高效率降低延迟时间等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值