IO读写文件

java文件读写

首先要理解IO的两种读写文件类
在这里插入图片描述一类是针对二进制文件的字节流
另一类是针对文本文件的字符流(此时需要注意编码方式,否则容易文字乱码)

读文件

package write;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ReadStringFromFile {
    private static final String RESOURCE_FILE_NAME = "测试文件.txt";

    public static void main(String[] args) {
        File sourceFile = new File("." + File.separator, RESOURCE_FILE_NAME);
        //将输入的封装在Scanner里面
        //public final static InputStream in = null;
        //InputStream得到的是字节  Scanner进行封装
        Scanner in = new Scanner(System.in);
        classicWay(sourceFile);
        collWay(sourceFile);
    }

    public static void classicWay(File sourceFile) {
        System.out.println("-----经典的处理方式------");
        try (
                FileInputStream fis = new FileInputStream(sourceFile);
                //将byte转为字符
                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
                //缓存
                BufferedReader reader = new BufferedReader(isr);

        ) {
            String line = null;
            //readLine读出一行
            //public abstract int read()
            //返回值 int:
            //he character read, or -1 if the end of the stream has been
            //        *         reached
            while ((line = reader.readLine()) != null) {
                System.out.println(line.trim().toUpperCase());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void collWay(File sourceFile) {
        System.out.println("-----collWay----");
        try (FileInputStream fis = new FileInputStream(sourceFile);
             InputStreamReader ir = new InputStreamReader(fis);
             BufferedReader reader = new BufferedReader(ir)
        ) {
            //| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
            reader.lines().map(String::trim).map(String::toUpperCase).forEach(System.out::println);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

写文件

package write;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class WriteToFiles {
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        File targetFile = createFile();
        wirteToFile(targetFile);
        System.out.println("程序执行结束");
    }

    private static void wirteToFile(File file) throws FileNotFoundException {
        //创建一个从文件到程序的byte传输流
        try (
                //建立一个程序到文件的管道(Byte)
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos,StandardCharsets.UTF_8);
            //使用PrintWriter提供的方法
            PrintWriter pw = new PrintWriter(osw);
            ){
            System.out.println("输入的内容会写入文件,如果输入空行则结束");
            while (true){
                String lineToWrite = scanner.nextLine().trim();
                System.out.println("输入的内容为:"+lineToWrite);
                if (lineToWrite.isEmpty()){
                    System.out.println("输入结束");
                    break;
                }else {
                    //写入到文件
                    pw.println(lineToWrite);
//                    pw.write(lineToWrite);
                    pw.flush();
                }
            }
        } catch (IOException e) {
            //文件删除 磁盘被拔掉了等错误
            e.printStackTrace();
        }


    }

    private static File createFile() throws IOException {
        System.out.println("请输入文件名");
        String fileName = scanner.nextLine().trim();

        File f =new File("D:\\hello"+File.separator,fileName+".txt");
        if (f.isFile()){
            System.out.println("目标文件存在,删除"+f.delete());
        }
        //createNewFile()——->新建文件
        System.out.println(f.createNewFile());
        return f;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值