File
File类:表示文件盒目录路径名的抽象表示形式。
File类可以实现文件的创建、删除、重命名、得到路径、创建时间等等,是唯一与文件本身有关的操作类。
- File.separator 路径分隔符 /,跨平台自动转换
- File.pathSeparator 路径分隔符 ;
File file = new File("C:\\Users\\GA\\Desktop\\123\\1.txt");
if(!file.exists()){
try {
//创建文件
boolean b = file.createNewFile();
System.out.println("文件创建 :"+true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除文件
System.out.println(file.delete());
//得到文件的上一级路径(也就是文件所在文件夹)
System.out.println(file.getParent());
//判断一个路径是否是文件夹
System.out.println(file.isDirectory());
//判断一个路径是否是文件
System.out.println(file.isFile());
File file = new File("C:\\Users\\GA\\Desktop\\123\\111");
//列出文件夹的所有文件名
String[] s = file.list();
for(String ss : s){
System.out.println(ss);
}
//列出文件夹中所有文件,以File数组返回
File[] f = file.listFiles();
for(File f1 : f){
System.out.println(f1.length()+" Kb");
}
//创建文件夹
boolean b = file.mkdir();
//重命名文件夹
File file1 = new File("C:\\Users\\GA\\Desktop\\123\\1");
file.renameTo(file1);
递归文件搜索
/**
* 文件搜索 找出指定目录指定拓展名文件
* @author GA
*
*/
public class Demo {
public static void main(String[] args) {
File fff = new File("c:\\");
findFile(fff,"avi");
}
//使用递归算法实现文件查找
public static void findFile(File file,String extName) {
if(file == null){
return;
}
//如果是目录,name获取该目录下的所有文件的File对象
if(file.isDirectory()){
File[] f1 = file.listFiles();
if(f1 != null){
for(File f2 : f1){
findFile(f2,extName);
}
}
}else{
//当file是文件时候,判断是否为指定拓展名 全部 转换小写
String path = file.getPath().toLowerCase();
if(path.endsWith(extName)){
System.out.println(file.getPath());
}
}
}
}
IO流
IO流的分类
- 依据数据类型分: 字符流 和 字节流
- 依据数据流向分: 输入流 和 输出流
输入流 输出流 ( Input Output )
字节流 字符流 ( InputStream OutputStream ) ( Reader Writer )
字节流
字节输出流
//字节输出流
public static void outputStream() {
try {
//创建一个文件字节输出流对象 true:文件不覆盖,追加到后面
OutputStream outputStream = new FileOutputStream("C:\\Users\\GA\\Desktop\\123\\1.txt",true);
//向文件中输出
String info = "过五关";
byte[] bytes = info.getBytes();
for(int i=0;i<bytes.length;i++){
outputStream.write(bytes[i]);
}
//关闭流
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
字节输入流
//字节输入流
public static void inputStream() {
//创建一个文件字节输入流对象
try {
File file = new File("C:\\Users\\GA\\Desktop\\123\\1.txt");
InputStream inputStream = new FileInputStream(file);
//指定每次要读取的字节数组
byte[] bytes = new byte[1024];
int len = -1;
StringBuilder sb = new StringBuilder();
while((len = inputStream.read(bytes))!=-1){
sb.append(new String(bytes,0,len));
}
System.out.print(sb);
//关闭流
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
字符流
字符输出流
//字符输出流
public static void write() {
File file = new File("C:\\Users\\GA\\Desktop\\123\\1.txt");
try {
//构造一个字符输出流对象 追加输出
Writer writer = new FileWriter(file,true);
String info = "好好学习 day day up";
//向文件输出
//writer.write(info.toCharArray());
writer.write(info);
//关闭流
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
字符输入流
//字符输入流
public static void reader() {
File file = new File("C:\\Users\\GA\\Desktop\\123\\1.txt");
try {
//构造一个字符输入流对象
Reader reader = new FileReader(file);
char[] cs = new char[8];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len = reader.read(cs))!=-1){
sb.append(new String(cs,0,len));
}
System.out.println(sb);
//关闭流
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
综合 : 复制文件
/**
* 文件复制
* @param src 源文件
* @param dest 目标路径
*/
public static void copeFile(String src,String dest) {
//获取文件名字
String ext = src.substring(src.lastIndexOf("\\"));
File srcFile = new File(src); //源文件
File destFile = new File(dest+File.separator+ext); //目标目录
InputStream inputStream = null;
OutputStream outputStream = null;
//构造输入输出流
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(destFile);
byte[] bt = new byte[1024];
int len = -1;
//读出马上写
while((len = inputStream.read(bt))!= -1){
outputStream.write(bt,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{ //流关闭
try {
if(inputStream != null)inputStream.close();
if(outputStream != null)outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
转换流
- 转换流,可以讲一个字节流转换为字符流,也可以将一个
- OutputStreamWriter : 可以将输出的字符流转换为字节流的输出形式 桥梁
- InputStreamReader : 将输入的字节流转换为字符流输入形式 桥梁
<span style="white-space:pre"> </span>/**
* 使用转换流,把字符流转换成字节流输出
* 字符串输出到指定目录
* OutputStreamWriter
*/
public static void main(String[] args) {
try {
//构造一个字节输出流
OutputStream out = new FileOutputStream("C:\\Users\\GA\\Desktop\\123\\12345.txt");
String info = "山不在高,有仙则名";
//通过字节输出流构造一个字符输出流
Writer writer = new OutputStreamWriter(out);
writer.write(info);//输出
writer.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用转换流,把字节流转换成字符流输出
* 打印记事本内容
* InputStreamReader
*/
public static void main(String[] args) {
try {
//构造一个字节输入流
InputStream in = new FileInputStream("C:\\Users\\GA\\Desktop\\123\\12345.txt");
//通过字节输出流构造一个字符输出流
Reader reader = new InputStreamReader(in);
char[] cr = new char[1024];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len = reader.read(cr))!=-1){
sb.append(new String(cr,0,len));
}
reader.close();
in.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
缓冲流
好处:能够搞笑的读写信息,将数据缓冲起来,然后一起写入
必须依赖一个流,本身不具备流功能
BufferedInputStream
为另一个输入流添加一些功能,在创建BufferedInputStream时,会创建一个内部缓冲区数组,用于缓冲。
字节缓冲流
/**
* 字节缓冲流 读取记事本文件
* @author GA
*
*/
public static void main(String[] args) {
try {
InputStream in = new FileInputStream("C:\\Users\\GA\\Desktop\\123\\12345.txt");
//根据字节输入流构造一个字节缓冲流
BufferedInputStream bis = new BufferedInputStream(in);
Reader reader = new InputStreamReader(bis);
char[] c = new char[1024];
int len = -1;
StringBuffer sb= new StringBuffer();
while((len=reader.read(c))!=-1){
sb.append(new String(c,0,len));
}
System.out.println(sb);
reader.close();
bis.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字节缓冲流 字符串写入到记事本文件
* @author GA
*
*/
public static void main(String[] args) {
try {
OutputStream out = new FileOutputStream("C:\\Users\\GA\\Desktop\\123\\11.txt");
//根据字节输出流构造一个字节缓冲流
BufferedOutputStream bos = new BufferedOutputStream(out);
String info = "山不在高,有仙则名";
bos.write(info.getBytes());
bos.flush(); //刷新缓冲区
bos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
字符缓冲流
/**
* 字符缓冲流 字符串写入操到指定记事本文件
* @author GA
*/
public static void main(String[] args) {
try {
Writer writer = new FileWriter("C:\\Users\\GA\\Desktop\\123\\1.txt");
//根据字符输出流构造一个字符缓冲流
BufferedWriter bw = new BufferedWriter(writer);
String info = "小黑";
bw.write(info);
bw.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字符缓冲流 读取记事本文件并且打印出来
* @author GA
*/
public static void main(String[] args) {
try {
Reader reader = new FileReader("C:\\Users\\GA\\Desktop\\123\\12345.txt");
BufferedReader br = new BufferedReader(reader) ;
char[] c = new char[1024];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len=br.read(c))!=-1){
sb.append(c,0,len);
}
System.out.println(sb);
br.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
打印流
- PrintStream 字节打印流
- PrintWriter 字符打印流
/**
* 字节打印流 字符串输出到指定目录记事本文件
* @author GA
*/
public static void main(String[] args) {
try {
OutputStream outputStream = new FileOutputStream("C:\\Users\\GA\\Desktop\\123\\1.txt");
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
//构造字节打印流
PrintStream ps = new PrintStream(bos);
ps.println(3.14f);
ps.println(1878);
ps.println(true);
ps.println("发货方式的回复积分附件");
//关闭流
ps.close();
bos.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用PrintWriter 字符打印流
* @author GA
*/
public static void main(String[] args) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\GA\\Desktop\\123\\1.txt"));
PrintWriter printWriter = new PrintWriter(bufferedWriter);
printWriter.print("\r\n");//输出回车加换行
printWriter.println(105);
printWriter.println("干哈的后果升上上");
printWriter.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}