文件操作相关

import java.io.*;
import java.util.Scanner;

public class A322文件相关 {

    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        String name =  creatFile();
        String n1 = "";
        System.out.println("请选择编码方式");
        System.out.println("1.UTF-8");
        System.out.println("2.GB18030");
        int n = scan.nextInt();
        if(n==1){
            n1 ="UTF-8";
        }else{
            n1 ="GB18030";
        }
        System.out.println();
        writeFile(name,n1);
        readFile(name,n1);
    }



    public static String creatFile() throws IOException{
        System.out.println("请输入新建文件名");
        Scanner scan = new Scanner(System.in);
        String a = scan.nextLine()+".txt";
        File file = new File(a);
        file.createNewFile();
        return a;
    }

    public static void writeFile(String path,String charSet) throws IOException{

        try(OutputStream st = new FileOutputStream(path)) {
            try(OutputStreamWriter aa = new OutputStreamWriter(st,charSet)) {
                System.out.println("请输入文字");
                Scanner scan = new Scanner(System.in);
                    String s = scan.nextLine();
                    aa.write(s, 0, s.length());
                    aa.flush();
                System.out.println("写入成功");
            }

        }
    }



    public static void readFile(String path,String charSet) throws IOException{
        try (InputStream st = new FileInputStream(path)){
           try(InputStreamReader aa = new InputStreamReader(st,charSet)){
               Scanner scanner = new Scanner(aa);
               while(scanner.hasNext()){
                   System.out.println(scanner.nextLine());
               }
           }
            System.out.println("读取成功");
        }
    }


}

=序列化Serialize=======

import java.io.*;

public class SerializeDemo {
    // 声明实现 Serializable 接口
    // 声明之后,就拥有了可以序列号和反序列化的功能了
    static class Person implements Serializable {
        String name;
        int age;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        try (InputStream is = new FileInputStream("找到的卧底的名单.binary")) {
            try (ObjectInputStream ois = new ObjectInputStream(is)) {
                Person p = (Person) ois.readObject();
                System.out.println(p.name);
                System.out.println(p.age);
                System.out.println("======================");
                Person q = (Person) ois.readObject();
                System.out.println(q.name);
                System.out.println(q.age);
            }
        }
    }

    /*
    public static void main(String[] args) throws IOException {
        Person p = new Person();
        p.name = "二娃"; p.age = 34;
        Person q = new Person();
        q.name = "带娃"; q.age = 18;

        try (OutputStream os = new FileOutputStream("找到的卧底的名单.binary")) {
            try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
                oos.writeObject(p);
                oos.writeObject(q);

                oos.flush();
            }
        }
    }
    */
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值