JAVA萌新学习 day27 IO流
一.概念
File
简介:java中用来描述 磁盘上的文件或者文件夹的一个类。
作用: 可以对磁盘上的文件或者文件夹 进行管理。
构造方法:
/*第一种构造方法*/
File parent=new File("d:/a");// 当前的parent表示 d盘下的 a文件夹
File child=new File(parent,"b.txt");//表示 d:/a/b.txt 注意:parent是file类型
/*这是第三种构造方法*/
File child1=new File("d:/a","b.txt");//d:/a/b.txt 注意:第一个参数是String
/*第二种构造方法*/
File file=new File("d:/a.txt");//给定一个路径(绝对|相对) 去描述一个磁盘文件
IO流
概念:
磁盘上的文件和内存之间进行交互,数据 的交互需要有一个媒介或者管道,把这个媒介或者管道就称为IO流,也被称为输入输出流【I:Input O:Output】
可以把流理解为管道。
分类:
流向分:
- 输入流 :外部输入到java程序(内存)中的流
- 输出流 :java程序(内存)中向外写出的流。
操作数据单元:
- 字节流:数据传输过程中 以字节未单位
- 字符流:数据传输过程中 以 字符为单位
输入流类:
InputStream 是一个抽象类,没法实例化。
所以我们通常会用他的子类---->
FileInputStream
以Stream单词结尾的流 都是字节流。
input或者Reader 这种的都是 输入流。
所以FileInputStream 是 文件字节输入流。
输入常用方法:
int read();//每次读取一个单位 返回值就是读取到的值
int read(字节数组[]|字符数组[])//每次读取一个完整的数组 返回值是读到数组中字节|字符个数
read(字节数组[]|字符数组[],起始位置,读取个数)//不常用
close()//关闭流的方法
FileInputStream每次读一个字节:
FileInputStream fis=new FileInputStream(new File("d:/a/b.txt"));
//int read=fis.read();//从文件中通过 fis 这个管道 输入到内存中一个字节。
//当read=-1的时候 表示 当前文件已经读完了。
int read;
while((read=fis.read())!=-1){//有东西 还可以继续读
System.out.print((char)read);//把read 转成字符输出
}
FileInputStream 每次读一个字节数组
int len;
byte [] bs=new byte[5];
while((len=fis.read(bs)!=-1){
for(byte b:bs){
System.out.println((char)b);
}
}
复制一:
File come=new File("a.txt");//数据的来源
File go=new File("b.txt");//数据的去向
FileInputStream fis=new FileInputStream(come);//输入流
FileOutputStream fos=new FileOutputSteam(go);//输出流
int read;
wile((read=fis.read())!=-1){//每次读一个字节
fos.write(read);
}
fos.close();
fis.close();
复制二:
File come=new File("a.txt");//数据的来源
File go=new File("b.txt");//数据的去向
FileInputStream fis=new FileInputStream(come);//输入流
FileOutputStream fos=new FileOutputSteam(go);//输出流
int len;
byte bs[]=new byte[1024];
wile((len=fis.read(bs))!=-1){//每次读一个完整的数组
fos.write(read,0,len);
}
fos.close();
fis.close();
字符流:
输入流:
Reader ------>FileReader 字符输入流
输出流:
Writer ------>FileWriter 字符输出流
只能操作文本 速度比字节快
复制文本:
File come=new File("a.txt");//数据的来源
File go=new File("b.txt");//数据的去向
FileReader fr=new FileReader(come);
FileWriter wf=new FileWriter(go);
int len;
char [] cs=new char[1024];
while((len=fr.read(cs))!=-1){
wf.write(cs,0,len);
}
二.代码
1.package file
TestFile
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
/**
* @Author:Tjy
* @Date:2019/12/26 9:05
* @Introduction:
*/
public class TestFile {
@Test
public void deleteFile(){
File file = new File("d:/a");
boolean delete = file.delete();//只能删除空文件夹 或者 文件
System.out.println(delete?"删除成功":"删除失败");
}
@Test
public void testGetAbsoluteFile(){
File file=new File("a.txt");//相对路径
System.out.println(file.getAbsoluteFile());//得到一个绝对路径 File类型的
}
@Test
public void testGetAbsolutePath(){
File file = new File("a.txt");
System.out.println(file.getAbsolutePath());//跟上面的唯一区别就是返回值类型 不同 但是路径值相同
}
@Test
public void testCanonicalFile() throws IOException {
File file = new File("a.txt");
System.out.println(file.getCanonicalFile());//相对路径的规范形式(还是绝对路径)
}
@Test
public void getName(){
File file = new File("a.txt");
System.out.println(file.getName());//获得文件名的方法
}
@Test
public void getParent(){
File file=new File("d:/a/b.txt");//返回当文件的父路径
System.out.println(file.getParent());
}
@Test
public void getPath(){
File file=new File("d:/a/b.txt");//返回当前文件的路径 绝对路径就返回绝对路径 相对路径就返回相对路径
System.out.println(file.getPath());
}
@Test
public void testIsDir(){
File file = new File("a.txt");
System.out.println(file.isDirectory());
}
@Test
public void testIsFile(){
File file = new File("d:/a/b.txt");
if (!file.exists()){
System.out.println("文件不存在");
}
System.out.println(file.isFile());//判断是否是文件 而非文件夹
}
@Test
public void testIsHidden(){
File file = new File("d:a/b.txt");
System.out.println(file.isHidden());
}
@Test
public void testList(){
File file = new File("d:/TeaEnv");
String[] list = file.list();//返回当前文件夹 下的所有子文件和子文件夹的字符串数组
for (String f:list) {
System.out.println(f);
}
}
@Test
public void testListFiles(){
File file = new File("d:/TeaEnv");
File[] files = file.listFiles();//返回当前文件夹下的所有子文件和子文件夹 File数组类型
for(File f:files){
System.out.println(f);//返回的是file类型
}
}
@Test
public void testMKdir(){
File file = new File("d:/b/c/d/e/f/g/h/j/j/j");
if(!file.exists()){//如果不存在
file.mkdirs();
}
}
@Test
public void testRename(){
File file = new File("d:/a/b.txt");
File file1 = new File("d:/a/bb.txt");
file.renameTo(file1);//重命名方法 注意:参数为file类型 且必须和源文件在相同目录下
}
public void del(File file){//递归删除
if(!file.exists()){
System.out.println("文件不存在或者已经被删除");
}else{//文件存在
if(file.isFile()){//当前file是文件 直接删除
file.delete();
}else{//当前file是文件夹
File[] files = file.listFiles();//当前文件夹下的所有子文件
if(files.length==0){//没有子文件或者子文件夹
file.delete();
}else{//当前file中有子文件
for(File f:files){//递归删除子文件
del(f);
}
file.delete();//删除自己
}
}
}
}
@Test
public void testDel(){//测试递归删除
File file = new File("d:/b");
del(file);
}
@Test
public void testPathSp(){
System.out.println(File.separator);
System.out.println(File.separatorChar);
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
}
}
TestFileMethods
import org.junit.Test;
import java.io.File;
import java.io.IOException;
/**
* @Author:Tjy
* @Date:2019/12/26 8:46
* @Introduction: 这是一个对File类方法测试的类
*/
public class TestFileMethods {
public static void main(String[] args) throws IOException {
File file = new File("d:/a.txt");
if(file.exists()){//判断文件是否存在
System.out.println("文件存在");
}else{
System.out.println("文件不存在");
boolean newFile = file.createNewFile();//创建文件的方法
System.out.println(newFile?"文件创建成功":"创建失败");
}
boolean r = file.canRead();
boolean w = file.canWrite();
boolean e = file.canExecute();
System.out.println(r?"该文件可读":"该文件不可读");
System.out.println(w?"该文件可写":"该文件不可写");
System.out.println(e?"该文件可执行":"该文件不可执行");
}
}
2.package io
TestBufferedInputStream
import com.sun.prism.shader.Solid_TextureRGB_AlphaTest_Loader;
import java.io.*;
/**
* @Author:Tjy
* @Date:2019/12/26 16:26
* @Introduction:
*/
public class TestBufferedInputStream {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:/a/b.txt")));
byte[] arr = new byte[4];
int len = bis.read(arr);
String string = new String(arr, 0, len);
System.out.println(string);
bis.mark(77);
len = bis.read(arr);
string = new String(arr, 0, len);
System.out.println(string);
bis.reset();
len = bis.read(arr);
string = new String(arr, 0, len);
System.out.println(string);
}
}
TestBufferedOutputStream
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
/**
* @Author:Tjy
* @Date:2019/12/26 16:45
* @Introduction:
*/
public class TestBufferedOutputStream {
public static void main(String[] args) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("d:/a/bv.txt")));
bos.write("我是一个小宝贝".getBytes());
bos.flush();
}
}
TestBufferedReader
import java.io.*;
/**
* @Author:Tjy
* @Date:2019/12/26 19:39
* @Introduction:
*/
public class TestBufferedReader {
public static void main(String[] args) {
FileReader fr =null;
try {
fr = new FileReader(new File("d:/a/b.txt"));
BufferedReader br = new BufferedReader(fr);
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
//String s = br.readLine();//读一个文本行
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestBufferedWriter
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Author:Tjy
* @Date:2019/12/26 19:46
* @Introduction:
*/
public class TestBufferedWriter {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d:/a/n.txt")));
bw.write("a");
bw.newLine();
bw.write("b");
bw.newLine();
bw.write("c");
bw.newLine();
bw.write("d");
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestFastCopy
import com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader;
import org.junit.Test;
import java.io.*;
/**
* @Author:Tjy
* @Date:2019/12/26 16:48
* @Introduction:
*/
public class TestFastCopy {
public void fast() throws Exception {
long start = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\85379\\Documents\\Bandicam\\1.mp4")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\85379\\Documents\\Bandicam\\3.mp4")));
byte bs[]=new byte[1024*100];
int len;
while ((len=bis.read(bs))!=-1){
bos.write(bs,0,len);
}
bos.close();
bis.close();
long end = System.currentTimeMillis();
System.out.println("战斗暴龙兽用时:" + (end - start));
}
@Test
public void testfast() throws Exception {
fast();
}
}
TestInputStreamReader
import org.junit.Test;
import java.io.*;
/**
* @Author:Tjy
* @Date:2019/12/26 15:43
* @Introduction:转换流
*/
public class TestInputStreamReader {
public static void main(String[] args) {//完成了 字节流到字符流的转化 其次 还可以对给内容经行重新编码
File file = new File("d:/a/b2.txt");
File file1= new File("d:/a/b3.txt");
try {
FileInputStream fis = new FileInputStream(file);//字节流
FileOutputStream fos = new FileOutputStream(file1);
//InputStreamReader isr = new InputStreamReader(fis);//字符流
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
OutputStreamWriter psw=new OutputStreamWriter(fos,"gbk");
int read ;
while ((read=isr.read())!=-1){
System.out.println((char)read);
psw.write(read);
psw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param file 操作文件
* @param ori 文件本身的编码格式
* @param tar 新的编码格式
* @throws IOException
*/
public void changCE(File file, String ori, String tar) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),ori);//转换流
File parent = file.getAbsoluteFile().getParentFile();
String newName = file.getName().split("\\.")[0] + "_cce1" + "." + file.getName().split("\\.")[1];
File newF = new File(parent, newName);
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(newF),tar);
int read;
while((read=isr.read())!=-1){
osw.write(read);
osw.flush();
}
}
@Test
public void testCCE() throws IOException {
changCE(new File("d:/a/b.txt"),"gbk","utf-8");
}
}
TestReader
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @Author:Tjy
* @Date:2019/12/26 14:50
* @Introduction:
*/
public class TestReader {
public static void main(String[] args) throws IOException {
File file = new File("d:/a/b.txt");
FileReader fr = new FileReader(file);
char [] cs=new char[5];
// int read = fr.read();
// int len;
// while((len=fr.read(cs))!=-1){
// for (int i =0;i<cs.length;i++){
// System.out.print(cs[i]);
// }
//
// }
}
}
TestStream
import org.junit.Test;
import org.omg.Messaging.SyncScopeHelper;
import java.io.*;
/**
* @Author:Tjy
* @Date:2019/12/26 10:39
* @Introduction: 字节流测试
*/
public class TestStream {
@Test
public void testFileInputStream() throws IOException {
File file = new File("d:/a/bb.txt");
FileInputStream fis = new FileInputStream(file);
int read;
while((read = fis.read())!=-1){
System.out.print((char)read);
}
fis.close();
//byte 字节 整型 一个字节8位 256状态 ---->ASCII 美标 127种 A B
//GBK 2 UNICODE 3
}
@Test
public void testFileOutPutStream() throws IOException {
File file = new File("d:/a/b.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(97);
fos.close();
}
@Test
public void testCopy() throws IOException {
long start = System.currentTimeMillis();//获取当期系统时间 1970到现在的毫秒值
File come = new File("C:\\Users\\85379\\Documents\\Bandicam\\1.mp4");
File go = new File("C:\\Users\\85379\\Documents\\Bandicam\\3.mp4");
FileInputStream fis = new FileInputStream(come);//创建一个输入流对象
FileOutputStream fos = new FileOutputStream(go);
// int read;
// while((read=fis.read())!=-1){
//
// fos.write(read);
// }
byte[] bytes=new byte[1024];//共用时2157
int len ;//本次读多少个字节
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println("共用时" + (end - start));//252ms
}
@Test
public void testRead() throws IOException {
File file = new File("d:/a/b.txt");
File file1 = new File("d:/a/fff.txt");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file1);
byte[] bytes = new byte[5];
int len;
while((len =fis.read(bytes))!=-1){
//fos.write(bytes);//每次都会写五个长度 如果是最后一次的话 就会有问题
fos.write(bytes,0,len);//每次读多少个 就写对应个 个数
}
fos.flush();
}
}
TestWriter
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Author:Tjy
* @Date:2019/12/26 14:59
* @Introduction:
*/
public class TestWriter {
public static void main(String[] args) throws IOException {
FileWriter wr = new FileWriter(new File("d:/a/v.txt"));
wr.write("我i是一个小可爱 ");//可以直接写一个字符串
wr.flush();//要及时的 强制刷新 否则可能没有写满缓存区 导致 数据没法输出
}
}