【java基础知识】IO流(含字节流和字符流的详细方法)

input和output

流的本质是数据传输

File类

构造方法

File f1 = new File("E://java_study//javaMysql");
System.out.println(f1);

File f2 = new File("E://java_study","javaMysql");
System.out.println(f2);

File f3 = new File("E://java_study");

File f4 = new File(f3, "javaMysql");
System.out.println(f4);

/* 上述代码输出
E:\java_study\javaMysql
E:\java_study\javaMysql
E:\java_study\javaMysql
 */

创建功能

// 创建文件
File f1 = new File("D://java_study//html.txt");
System.out.println(f1.createNewFile());

// 创建一级目录
File f2 = new File("D://java_study//ddddd");
System.out.println(f2.mkdir());

// 创建多级目录
File f3 = new File("D://java_study//mysqlhh//zheshiyi");
System.out.println(f3.mkdirs());

路径

File f2 = new File("D://java_study//ddddd");
// 绝对路径

File f3 = new File("Student//html.txt");
// 相对路径
System.out.println(f3.createNewFile());

删除

// 删除路径 (目录和文件一样方法)
System.out.println(f3.delete());

// 注意:删除要一层一层的删,先删父文件里的子文件(或内容),才能删父文件

常用方法

File f = new File("D:\\java_study\\Student\\javaa.txt");
  System.out.println(f.createNewFile());
  System.out.println(f.isDirectory()); // 判断是否是目录
  System.out.println(f.isFile()); // 判断是否是文件
  System.out.println(f.exists());
  System.out.println(f.getAbsoluteFile()); // D:\java_study\Student\javaa.txt 返回绝对路径
  System.out.println(f.getPath()); // D:\java_study\Student\javaa.txt 返回抽象路径,就是上面定义的
  System.out.println(f.getName()); // java.txt

  File f2 = new File("D:\\java_Study\\ddddd");
  String[] list = f2.list();
  for (String list1 : list) {
      System.out.println(list1);
  }
/*html.txt
  mysqlhh
 */
  File[] files = f2.listFiles();
  for (File f1 : files) {
      if (f1.isFile()) {
          System.out.println(f1.getName());
      }
  }
  // html.txt

遍历目录 得到文件,用到递归

public static void main(String[] args) {
    File f = new File("D:\\java_study");
    getFilePath(f);
}
public static void getFilePath(File f) {
    File[] f1 = f.listFiles();
    if (f1 != null) {
        for (File f2 : f1) {
            if (f2.isDirectory()) {
                getFilePath(f2);
            } else {
                System.out.println(f2.getAbsoluteFile());
            }
        }
    }
}

字节流

写入数据方法
FileOutputStream fos = new FileOutputStream("D:\\java_study\\Student\\outPutStream.txt");
// 相当于FileOutputStream fos = new FileOutputStream(new File("D:\\java_study\\Student\\outPutStream.txt"));

// 写一个
fos.write(97); // 输出a

// 写入字节数组
byte[]  bys = {97,98,99,100,101};
fos.write(bys); // abcde
// 用String转换
byte[] byss = "abcde".getBytes();
fos.write(byss); // abcde

// 从off的索引开始,写len个数据,写局部数据
fos.write(bys, 1, 3); // bcd

// 释放资源(相当于关闭)
fos.close();
追加数据和换行
// 要追加元素,就要在后面加True
FileOutputStream fos = new FileOutputStream("D:\\java_study\\Student\\outPutStream.txt", true);

for (int i = 0; i < 10; i++) {
    fos.write("hello".getBytes());
    // window换行用\r\n
    fos.write("\r\n".getBytes());
}

fos.close();
try…catch…finally处理异常
FileOutputStream fos = null;
try {
    fos = new FileOutputStream("D:\\java_study\\Student\\outPutStream.txt", true);
    fos.write("hello".getBytes());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        // 写释放资源的操作
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
字节流读数据

一次读一个

FileInputStream fis = new FileInputStream("D:\\java_study\\Student\\outPutStream.txt");
int by;
while ((by=fis.read()) != -1) {
    System.out.print((char)by);
}
fis.close();

一次读一个数组(推荐,配合缓冲流更快)

FileInputStream fis = new FileInputStream("D:\\java_study\\Student\\outPutStream.txt");
byte[] bys = new byte[1024]; // 1024及其整数倍
int len; // 读取的个数
while ((len=fis.read(bys)) != -1) {
    System.out.println(new String(bys, 0, len));
}
fis.close();
案例

复制图片

FileInputStream fis = new FileInputStream("D:\\美图\\天堂旅行团1.png");

// 创建目的地对象
FileOutputStream fos = new FileOutputStream("D:\\java_study\\Student\\天堂旅行团1.png");


// 复制图片
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys)) != -1) {
    fos.write(bys, 0, len);
}

// 释放资源
fis.close();
fos.close();
字符缓冲输入和输出流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\美图\\天堂旅行团1.png"));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\java_study\\Student\\天堂旅行团1.png"));

字符流

中文在GBK中占2个字节,在UTF-8中占3个字节

编码和解码要用同一个字符集

编码和解码
String s = "中国";
// 编码
byte[] bys = s.getBytes(); // 默认是用UTF-8
byte[] bys1 = s.getBytes("GBK");
System.out.println(Arrays.toString(bys)); // [-28, -72, -83, -27, -101, -67]
System.out.println(Arrays.toString(bys1)); // [-42, -48, -71, -6]

// 解码
String ss = new String(bys);
System.out.println(ss); // 中国

String sss = new String(bys1, "GBK");
System.out.println(sss); // 中国

方法

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\java_study\\Student\\outPutStream.txt"), "UTF-8");

osw.write("中国");
osw.close(); // 先关闭,不然下面输出不了
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\java_study\\Student\\outPutStream.txt"), "UTF-8");
// 读取数据
int ch;
while ((ch = isr.read()) != -1) {
    System.out.print((char) ch);
}

isr.close();

close方法是先刷新,在关闭

写数据的方法(5种)
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\java_study\\Student\\outPutStream.txt"), "UTF-8");

osw.write(97);
osw.flush(); // 不同于osw.close();没有关闭

char[] ch = {'a', 'b', 'c', 'd', 'e'};
osw.write(ch);

osw.write(ch, 1, 3); // bcd

osw.write("abcde");
osw.write("abcde", 1, "avcde".length());

osw.close();

读数据参考字节流,只不过将byte改为了char而已,2中方法参考

简写方式(常用)

简写的方式不能定义字符集,只能用默认的字符集,如要要修改字符集,还是要用全称

简写是全称的子类,可以调用父亲的方法

FileWriter fw = new FileWriter("D:\\java_study\\Student\\outPutStream.txt");
FileReader fr = new FileReader("D:\\java_study\\Student\\outPutStream.txt");
字符缓冲流

也可以参照字节缓冲流的用法

特有功能

BufferedReader br = new BufferedReader(fr);
fr.readLine // 只读一行的字符串
BufferedWriter bw = new BufferedWriter(fw);
fw.newLine // 换行,相当于win的\t\n

常用复制文件方法(用特有功能)

String line;
while ((line = br.readLine()) != null) {
    bw.write(line);
    bw.newLine();
    bw.flush();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凉了的凉茶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值