【8.3笔记】输入和输出的处理(一)

一、输入输出的概念

站在程序的角度上,从文件读取进来是输入,写入到文件、内存、控制台是输出
在这里插入图片描述

二、File类

1、File类的常用方法
在这里插入图片描述
File类文件创建,显示信息,删除如下

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

/*
 * @Author:Qiutong
 * @Date:2020/8/3
 * @Description:创建文件,获取文件信息,删除文件
 */
public class FileDemo {
   //创建文件
    public void creatFile(File file){
        if (!file.exists())
        try {
            file.createNewFile();
            System.out.println("文件已创建!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //显示文件信息
    public void showFile(File file){
        if (file.exists()) {
            if (file.isFile()) {
                System.out.println("文件的名字为:" + file.getName());
                System.out.println("文件的相对路径为:" + file.getPath());
                System.out.println("文件的绝对路径为:" + file.getAbsolutePath());
                System.out.println("文件的字节为:" + file.length() + "字节");
            }
            if (file.isDirectory()) {
                System.out.println("是目录");
            }
        }else{
            System.out.println("文件不存在!");
        }
    }
    //删除文件
    public void delete(File file){
        if (file.exists()){
            file.delete();
            System.out.println("文件已删除!");
        }
    }
    //Test
    public static void main(String[] args) {
        FileDemo fileDemo=new FileDemo();
        File file=new File("e:/qiu/love.txt");
        fileDemo.creatFile(file);
        fileDemo.showFile(file);
    }

三、流

通过流来读写文件
流是一组有序的数据序列
以先进先出的方式发送信息的通道

java流的分类
在这里插入图片描述
InputStream常用的方法
int read()
int read(byte[] b)
int read(byte[] b,int off,int len)
void close()
int available():可以读取的字节数目

子类FileInputStream常用的构造方法
FileInputStream(File file)文件
FileInputStream(String name)文件路径

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

/*
 * @Author:Qiutong
 * @Date:2020/8/3
 * @Description:
 */
public class TestFileInputStream {
    public static void main(String[] args) throws IOException {
        File file=new File("e:/青春.txt");
        FileInputStream fis=new FileInputStream(file);
        int tmp=-1;
        while ((tmp=fis.read())!=-1){
            System.out.print((char)tmp);
        }
        byte[] b=new byte[1024];
        fis.read(b);
        byte[] rst=null;
        for (int i = 0; i < b.length; i++) {
            if (b[i]==0){
                rst= Arrays.copyOf(b,i);
                break;
            }
        }
        String str=new String(rst);
        System.out.println(str);

    }
    public static String readFile(String path){
//        File f=new File(path);
        FileInputStream fis= null;
        String str=null;
        try {
            fis=new FileInputStream(path);
            byte[] b=new byte[1024];
            byte[] rst=null;
            for (int i = 0; i < b.length; i++) {
                if (b[i]==0){
                    rst=Arrays.copyOf(b,i);
                }
            }
            str=new String(rst);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    //单字节
    public static String readByOne(String path){
        FileInputStream fis=null;//扩大作用域
        String str=null;
        try {
            fis=new FileInputStream(path);
            StringBuffer sb=new StringBuffer();
            int tmp;
            while((tmp=fis.read())!=-1){
                char c=(char)tmp;
                sb.append(c);
            }
            str=sb.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        finally {//即使try中发生了异常,也可以关闭数据流
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }
}

OutputStream常用的方法
int write(int c)
int write(byte[] buf)
int write(byte[] b,int off,int len)
void close()
int flush():强制把缓冲区的数据写到输入流中

子类FileInputStream常用的构造方法
FileOutputStream(File file)文件
FileOutputStream(String name)文件路径
FileOutputStream(String name,boolean append)
前两种会直接覆盖调原来的文件内容,boolean参数true是会直接在原内容的基础上追加

public class FileOutputStreamDemo {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("e:/qiu/learn.txt",true);
            String info="天天向上";
            byte[] b=info.getBytes();
            fos.write(b,0,b.length);
            System.out.println("文件写入成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

复制,从一个中读取同时写入到另一个

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

/*
 * @Author:Qiutong
 * @Date:2020/8/3
 * @Description:复制文本文件
 */
public class Test2 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("E:/我的青春谁做主.txt");
            fos=new FileOutputStream("E:/qiu/test.txt");
            byte[] words=new byte[1024];
            int tmp=-1;
            while((tmp=fis.read(words))!=-1){
                fos.write(words,0,tmp);
            }
            System.out.println("文件已复制完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

晚安。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 前言 1-4 1.1. JAVA特点 1-4 1.2. 运行原理 1-4 1.3. JAVA目录 1-4 2. 一•基础知识 2-4 2.1. 配置环境 2-4 2.2. Java中基本概念 2-5 3. 二•定义,关键字和类型 3-5 3.1. 注释的三种形式 3-5 3.2. Java代码中的“;”、“{}”、“ ” 3-5 3.3. 标识符 3-5 3.4. 数据类型 3-6 3.5. 命名规则 3-6 4. 三•表达式和控制流 4-6 4.1. 变量和作用域 4-6 4.2. 操作符 4-7 4.3. 数字类型之间的转换 4-7 4.4. 强制类型转换 4-8 4.5. 转换的二种类型 4-8 4.6. 控制流 4-8 4.7. 循环语句 4-8 5. 四•数组 5-9 5.1. 声明数组 5-9 5.2. 创建数组 5-9 5.3. 初始化数组 5-10 5.4. 多维数组 5-10 5.5. 数组拷贝 5-10 6. 五•对象和类 6-11 6.1. 面向对象与面向过程 6-11 6.2. 对象的概念 6-12 6.3. 成员变量和局部变量 6-12 6.4. 成员方法 6-12 6.5. This关键字 6-13 6.6. 访问控制符 6-13 6.7. 构造方法 6-13 6.8. 数据和方法的隐藏―――封装 6-14 6.9. 方法的重载 6-15 7. 六•高级语言特性 7-15 7.1. 封装 (encapsulation) 7-15 7.2. 继承 (inherit) 7-16 7.2.1. JAVA继承特点 7-16 7.2.2. 父类(SuperClass)和 子类(SubClass)的关系 7-17 7.2.3. 系统构造一个对象的顺序 7-17 7.3. 多态(polymorphism) 7-18 7.3.1. 方法的覆盖(overridding) 7-18 7.3.2. 多态的分类 7-18 7.3.3. 运行时多态的三原则 7-19 7.3.4. 关系运算符:instanceof 7-20 7.4. 静态变量,方法和类 7-20 7.5. Singleton模式 7-22 7.6. final关键字 7-22 7.6.1. final变量不能被改变; 7-22 7.6.2. final方法不能被改写; 7-23 7.6.3. final类不能被继承; 7-23 7.6.4. String 类 7-23 7.7. 抽象类 7-24 7.8. 接口 (模板方法模式) 7-25 7.9. Object 类 7-27 7.10. 封装类 7-28 7.11. 内部类 7-29 7.11.1. 内部类的分类 7-29 7.11.2. 成员内部类 7-29 7.11.3. 局部内部类 7-30 7.11.4. 静态内部类 7-30 7.11.5. 匿名内部类 7-31 7.12. 集合 7-31 7.12.1. 集合接口类层次 7-32 7.12.2. 集合类层次 7-33 7.12.3. 五个最常用的集合类之间的区别和联系 7-33 7.12.4. 比较 7-35 7.13. 反射 7-37 8. 七•异常 8-37 8.1. 异常的基本概念 8-37 8.2. 捕获异常 8-38 8.3. 处理异常 8-38 8.4. 捕捉多个异常 8-38 8.5. finally 声明 8-38 8.6. 异常调用栈 8-39 8.7. 异常层次 8-39 8.8. 一些未检查的异常 8-39 8.9. 写你自己的异常 8-39 8.10. 抛出你自己的异常 8-40 9. 八•图形用户接口 9-40 10. 九•AWT(Abstract Window Toolkit) 事件模型 10-41 11. 十•The AWT Component Library 11-41 12. 十一•JFC(Java Foundation Classes) 12-41 13. 十二•Applets 13-41 14. 十三•线程Thread 14-41 14.1. 线程原理 14-41 14.2. 线程实现的两种形式 14-42 14.3. 线程的生命周期 14-43 14.4. Thread的方法 14-43 14.5. 共享数据的并发处理 14-44 14.6. 使用互斥锁的注意事项 14-44 15. 十四•标准I/O流与文件 15-46 15.1. 对文件的操作 15-46 15.2. 处理跨平台性 15-46 15.3. 对象的序列化接口 15-47 15.4. I/O流基础 15-47 15.5. 流的分类 15-47 15.6. I/O输入输出 15-48 16. 十五•网络编程 16-52 16.1. 网络基础知识 16-52 16.2. TCP Socket 16-54 16.2.1. 建立TCP服务器端 16-54 16.2.2. 建立TCP客户端 16-55 16.3. 建立URL连接 16-55 16.4. UDP socket 16-58 16.4.1. 建立UDP 发送端 16-58 16.4.2. 建立UDP 接受端 16-59 17. java5.0的新特性 17-59 17.1. 泛型 17-59 17.1.1. 说明 17-59 17.1.2. 用法 17-60 17.1.3. 泛型的通配符"?" 17-62 17.1.4. 泛型方法的定义 17-63 17.1.5. 泛型类的定义 17-63 17.1.6. 泛型与异常 17-64 17.1.7. 泛型的一些局限型 17-65 17.2. 增强的for循环 17-66 17.3. 自动装箱和自动拆箱 17-69 17.3.1. 在基本数据类型和封装类之间的自动转换 17-69 17.4. 类型安全的枚举 17-70 17.5. 静态引入 17-71 17.6. C风格的格式化输出 17-72 17.7. Building Strings(StringBuilder类) 17-73 17.8. 可变长的参数 17-73 17.9. JAVA5.0 的注释 (Annotation) 17-73 17.10. Callable 和 Future接口 17-74

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youzi-qiu7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值