java基础-11-io流

本文详细介绍了JUnit在Java中的应用,以及文件I/O流(包括字节流、字符流、节点流和处理流)的创建、操作和使用,如FileInputStream、FileOutputStream、FileReader、FileWriter等,以及BufferedReader和BufferedWriter的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • junit使用
  • 文件io流
    • 创建文件
    • 文件的常用操作
    • 流的分类
    • 字节流
      • FileInputStream
      • FileOutputStream
    • 字符流
      • FileReader
      • FileWriter
    • 节点流和处理流
      • BufferedReader,BufferedWriter
      • BufferedInputStream和BufferedOutputStream

junit使用

可以用来测试方法,不用写到main函数中就能运行
写@Test

public class junit {  
    public static void main(String[] args) {  
  
    }  
    @Test  
    public void m1(){  
        System.out.println("m1方法被调用");  
    }  
}

文件io流

概念:
文件在程序中是以流的形式来操作的
流:数据在数据源(文件)和程序(内存)之间经历的路径

Pasted image 20240304205143.png

创建文件

三种方式:

  1. new File(String Filepath)
  2. new FIle(File parent,String child)//第一个参数为父级目录
  3. new FIle(String parent,String child)
    creatNewFile来创建文件
    文件路径E:\XXX.txt
String s = new String("E:\\New01");  
File file = new File(s);  
try {  
    file.createNewFile();  
} catch (IOException e) {  
    throw new RuntimeException(e);  
}

文件的常用操作

  1. getName
  2. getAbsoluteFile//获取绝对路径
  3. getParent
  4. length//返回文件字节大小
  5. exists
  6. isFile
  7. isDirectory//是否是目录
String s = new String("E:\\New01");  
File file = new File(s);  
System.out.println("文件是否存在"+file.exists());  
  
System.out.println(file.getAbsoluteFile());  
  
System.out.println(file.getName());  
  
System.out.println(file.getParent());
  1. mkdir()创建一级目录
  2. mkdirs()创建多级目录
  3. delete()删除目录或文件
String dirPath="E:\\nenene";  
File file = new File(dirPath);  
if (file.mkdir()) {  
    System.out.println(file.getName()+"创建成功");  
}else {  
    System.out.println(file.getName()+"创建失败");  
}  
File file1 = new File("E:\\nenene\\a.txt");  
try {  
    file1.createNewFile();  
} catch (IOException e) {  
    throw new RuntimeException(e);  
}  
  
String s = new String("E:\\nenene\\新建 BMP 图像.bmp");  
File file2 = new File(s);  
file2.delete();

流的分类

流是用来对文件进行读写操作的

按操作数据类型不同分为:字节流( 8 bit,8位)二进制文件(视频,音乐等),
字符流(一个字符)文本文件

字节流的输入流为InputStream,输出流为OutputStream
字符流的输入流为Reader, 输出流为Writer

由这四个抽象类派生出来的子类的名称都是以这几个类为其后缀名

流相当于外卖小哥把食物(数据)从物流中心(文件,磁盘)拿到用户(java程序,内存)中

Pasted image 20240302173735.png

字节流

FileInputStream

是InputStream的实现子类,用来对文件进行读操作
创建一个流对象,当这个流对象与文件关联起来后,就通过这个对象来操作文件

这就是一个流,即一个外卖小哥,流中携带着数据,(类似外卖小哥携带着食物)能够从他这读取数据

  • 创建流对象方式:
    new FileInputStream(File file);
    通过放入一个File类型的对象,来使这个流与文件关联

new FileInputStream(String FilePath);
通过放入文件路径,来使这个流与文件关联

最后记得用close方法关闭

  • 读取流对象的数据的方式:
    int read(),从此输入流中读取1个字节的数据返回回来,需要用int类型变量接收,但是要输出需要转成char或String类型,结束时会返回-1
    fileInputStream = new FileInputStream(FilePath);//创建一个流
    while((readDate=fileInputStream.read())!=-1){  //从流中读取数据
        System.out.print((char) readDate);  //转为char输出
    }  
    
String FilePath="E:\\nenene\\hello.txt";  
FileInputStream fileInputStream =null;  
int readDate;  

try {  
    fileInputStream = new FileInputStream(FilePath);//创建一个流  
    
    while((readDate=fileInputStream.read())!=-1){  //从流中读取数据
        System.out.print((char) readDate);  //转为char输出
    }  




} catch (Exception e) {  
    throw new RuntimeException(e);  
}finally {  
    try {  
        fileInputStream.close();  
    } catch (IOException e) {  
        throw new RuntimeException(e);  
    }  
}

int read(byte[] b) b的长度即为一次读取的字节数量,批量读取多个字节到byte数组,
返回读取到的字节数,需用int类型变量接收,然后输出时用String

String FilePath="E:\\nenene\\hello.txt";  
FileInputStream fileInputStream =null;  
int DateLength;  
byte[] bytes=new byte[6];//创建byte作为参数  
  
//try {  
    fileInputStream = new FileInputStream(FilePath);  
    while((DateLength=fileInputStream.read(bytes))!=-1){  
        System.out.print(new String(bytes,0,DateLength));  




    }  
//} catch (Exception e) {  
// throw new RuntimeException(e);  
//}finally {  
//    try {  
        fileInputStream.close();  
//    } catch (IOException e) {  
//        throw new RuntimeException(e);  
    }  
}

FileOutputStream

创建:
fileOutputStream = new FileOutputStream(path,true);//true是用来标志是否追加

写数据方式:
String str="hello,hep";
fileOutputStream.write(str.getBytes());//需要转成byte字节类型

String path="E:\\nenene\\hello.txt";  
FileOutputStream fileOutputStream = null;  
try {  
    fileOutputStream = new FileOutputStream(path,true);  
    String str="hello,hep";  
    fileOutputStream.write(str.getBytes());

要完成文件拷贝,注意要边读边写

字符流

FileReader

创建:
new FileInputStream(String FilePath);//用文件路径,与FileinputStream类似

读数据:

  1. read()//一个字符一个字符的读,用int接收,转为char输出
  2. read(char[] b)//批量读取多个字符到字符数组b中,返回读取到的字符长度
FileReader fileReader = null;  
try {  
    int length=0;  
    char[] buf=new char[1024];  
    fileReader = new FileReader("E:\\nenene\\a.txt");  
    while ((length=fileReader.read(buf))!=-1){  
        System.out.println(new String(buf,0,length));  
    }

FileWriter

write()写入
有几种格式如
write(char[],off,len)
write(String str)等

最后要用close()关闭或flush()刷新才会把操作更新到文件中

fileWriter = new FileWriter("E:\\nenene\\hello.txt",true);  
fileWriter.write("风雨之后,定见彩虹");

节点流和处理流

  • 节点流是底层流/低级流,直接和数据源相关,如前面几个
  • 处理流(包装流)对节点流进行包装,消除不同节点流之间的差异,提供更方便的方式来操作,使用了修饰器设计模式,不会直接与数据源相连,效率更高

BufferedReader,BufferedWriter

处理流,字符流的包装流
BufferedReader的readline()方法返回一个String,结尾时返回null
BufferedWriter的newline()方法类似换行
这段代码抛出了异常,所以没有try catch

(当写入一段数据后,记得用flush方法刷新,或者直接关闭)

BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\nenene\\a.txt"));  
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\nenene\\hello.txt"));  
String line;  
  
while ((line=bufferedReader.readLine())!=null){  
    bufferedWriter.write(line);  
    bufferedWriter.newLine();  
}  
bufferedReader.close();  
bufferedWriter.close();

BufferedInputStream和BufferedOutputStream

用法和前面的FileInputStream,FileOutputStream类似

(当写入一段数据后,记得用flush方法刷新,或者直接关闭)

BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\nenene\\7ae1670bb77d43f8ab93c79b71d4a4e1.png"));  
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\nenene\\copy03.png"));  
  
byte[] bytes=new byte[1024];  
int DateLength=0;  
int Date;  
while ((DateLength=bufferedInputStream.read(bytes))!=-1){  
    bufferedOutputStream.write(bytes,0,DateLength);  
}  
bufferedInputStream.close();  
bufferedOutputStream.close();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值