- 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流
概念:
文件在程序中是以流的形式来操作的
流:数据在数据源(文件)和程序(内存)之间经历的路径
创建文件
三种方式:
- new File(String Filepath)
- new FIle(File parent,String child)//第一个参数为父级目录
- 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);
}
文件的常用操作
- getName
- getAbsoluteFile//获取绝对路径
- getParent
- length//返回文件字节大小
- exists
- isFile
- 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());
- mkdir()创建一级目录
- mkdirs()创建多级目录
- 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程序,内存)中
字节流
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类似
读数据:
- read()//一个字符一个字符的读,用int接收,转为char输出
- 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();