java中I/O流,输入与输出

File

构造方法
File(String pathnames);
File(String parent,String child);
File(File parent,String child);

创建文件

1. if (!file.exists()) {
2. // 创建这个文件
3. try {
4. file.createNewFile();
5. } catch (IOException e) {
6. e.printStackTrace();
7. }
8. }
9. 该方法不允许创建目录,必须所有的父目录都存在
10. file.createNewFile();

创建目录

//创建父目录

file.getParentFile().mkdirs();
//直接创建文件夹
file.mkdirs();//如果是一个文件,也会变成文件夹

获取路径

    file.getAbsolutePath()

获取长度

    file.length();

重命名

    file.reNameTo(File dest);//当前目录是重命名,其他目录是剪切

IO流

流必须关闭

InputStream

close 关闭流
read(byte[] b) 将数据读取在byte[]里面 byte[]作为缓冲区,你给多大,就读多大

OutputStream

close 关闭流
write(byte[] b);

FileInputStream

实现了具体的抽象方法
public static void main(String[] args) {
        // 如果需要读文件
        File file = new File("D:/UsersAPI.java");
        if (file.exists()) {
            // 开始读取file
            // 字节流 文件读取
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                // 水漂大小
                byte[] buf = new byte[1024];
                StringBuffer sb = new StringBuffer();
                // int len = fis.read(buf);
                // while (len != -1) {
                // len = fis.read(buf);
                // if (len != -1)
                // sb.append(new String(buf, 0, len, "utf-8"));
                // }
                int len = 0;
                while ((len = fis.read(buf)) != -1) {
                    sb.append(new String(buf, 0, len, "utf-8"));
                }
                // 字节如何转换成String
                // String content = new String(buf, 0, len, "utf-8");
                System.out.println(sb.toString());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

FileOutputStream

close 关闭流
write(byte[] b);
public static void main(String[] args) {

        // 定义一个文件
        File file = new File("D:/message.txt");
        FileOutputStream fos = null;
        try {
            // 可以跟上一个 boolean值 是否追加
            fos = new FileOutputStream(file, true);
            // 写数据
            fos.write("哈哈哈哈".getBytes());
            // 刷新缓冲区,讲数据刷到硬盘上.
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

读文件操作

1. FileInputStream fis = new ...
2. //1.定义长度
3. int len=0;
4. //2.循环读取所有数据
5. byte[] buf = new byte[每一次读的数据];
6. while((len=fis.read(buf))!=-1)
7. {
8. 读的数据在byte里面.保存起来
9. }
10. fis.close();

写文件操作

1. FileOutputStream fos = new ...
2. fos.write(byte[]); // "".getBytes();
3. fos.flush();
4. fos.close();

序列化

将对象转为可以存储,或者网络传输的一种方式
1.实现接口
2.给定一个随机的ID号

ObjectInputStream

初始化一个 文件存放的地方
Object obj = readObject();

ObjectOutputStream

初始化需要一个 文件存放地的输出流
writeObject(Object obj);

public class SerializeDemo {

    // 序列化 反序列化
    // 将对象转为可以存储的方式

    public static void main(String[] args) {
        // app上面的 自动登录
        // User user = new User("张三", "123456");
        // 序列化写入
        // write(user);
        // User user = read();
        // System.out.println(user.toString());
        // 自动登录
        System.out.println("欢迎光临...系统");
        File file = new File("D:/user");
        if (file.exists()) {
            System.out.println("自动登录");
            User user = read();
            System.out.println("欢迎光临" + user.getName());
        } else {
            Scanner input = new Scanner(System.in);
            System.out.println("输入用户名");
            String name = input.next();
            System.out.println("输入密码");
            String pwd = input.next();
            User user = new User(name, pwd);
            System.out.println("登录成功,保存资料,下次自动登录");
            write(user);
        }

    }

    private static User read() {
        // 序列化的读取
        // 从那个文件中读取
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("d:/user");
            ois = new ObjectInputStream(fis);
            // 有可能类型转换异常
            User user = (User) ois.readObject();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void write(User user) {
        // 你需要存储的地址
        OutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("d:/user");
            oos = new ObjectOutputStream(fos);
            // 写入
            oos.writeObject(user);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


//下面是User类

public class User implements Serializable {

    private static final long serialVersionUID = 123123;
    // 1.序列化 实现接口 Serializable
    // 2. 除了基础数据类型, 存放的其他对象都需要序列化

    private String name;
    private String pwd;

    // 一定要序列化
    // Goods goods;

    public User(String name, String pwd) {
        super();
        this.name = name;
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", pwd=" + pwd + "]";
    }

    public User() {
        super();
    }

}

字符流

Reader

BufferedReader

构造方法需要一个转接流

InputStreamReader 用来将字节读取后转换成字符
    1. is = new FileInputStream("D:/IOTest/helloworld.txt");
    2. // 转换流
    3. InputStreamReader inputStreamReader = new InputStreamReader(is);
    4. // 字符流
    5. reader = new BufferedReader(inputStreamReader);
    读取
    1. // 和字节流一样的使用方法
    2. // char[] chars = new char[1024];
    3. // int len = 0;
    4. // while ((len = reader.read(chars)) != -1) {
    5. // System.out.println(new String(chars, 0, len));
    6. // }
    7.
    8. String lines = "";
    9. // 如果返回来的是null,就表明读完了
    10. while ((lines = reader.readLine()) != null) {
    11. System.out.println(lines);
    12. }

两种写法一个是一次读一行,一个是读一个字节

ublic static void main(String[] args) {
        try {
            FileReader fileReader = new FileReader("D:/IOTest/helloworld.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String lins = "";
            while ((lins = bufferedReader.readLine()) != null)
                System.out.println(lins);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static void main(String[] args) {
        // 从文件中字符流读取
        // 每隔100毫秒打印一个字符。
        // abcd
        // abcd..
        File file = new File("D:\\IOTest\\helloworld.txt");
        try {
            FileReader fr = new FileReader(file);
            // 字符流
            BufferedReader br = new BufferedReader(fr);
            int i = 0;
            while ((i = br.read()) != -1) {
                System.out.print((char) i);
                Thread.sleep(100);
            }
            // String lines=null;
            // while((lines=br.readLine())!=null)
            // {
            // //转数组
            // char[] chars = lines.toCharArray();
            // for (char c : chars) {
            // System.out.print(c);
            // Thread.sleep(100);
            // }
            // System.out.println();
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Writer

BufferedWriter

public static void main(String[] args) {
        FileWriter out;
        try {
            out = new FileWriter("d:/cc.txt");
            BufferedWriter bw = new BufferedWriter(out);
            // 写数据
            bw.write("你好世界,hello world");
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void zhuanjie() {
        try {
            FileOutputStream fos = new FileOutputStream("d:/cc.txt", true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            // 字符流
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write("你好");
            bw.flush();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

字符串常见操作

1. startWith("") 是否以字符串开头
2. endWith("") 是否以字符串结尾
3. split("") 按照格式切割字符串 数组
4. contains("") 是否包含另外一个字符串
5. compareTo("") 按字典排序
6. toLowerCase toUpperCase 转小写和转大写
7. subString() 从哪里截取到哪里
8. replaces() 替换 all()
9. indexOf() 获取当前字符串在本字符串中的第一次出现的位置 没有返回-1
10. trim() 去空格
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值