package com.file;
import java.io.File;
import java.io.FilenameFilter;
//自定义过滤器
class myFiles implements FilenameFilter{
@Override
public boolean accept(File dir, String name) {
// 返回所有后缀为.java的文件
return name.endsWith(".java");
}
}
//文件处理
public class Demo1 {
public static void main(String[] args) {
File file = new File("F:\\java");
ListFile(file);
}
public static void ListFile(File dir){
File[] files = dir.listFiles(new myFiles());
for(File file :files){
System.out.println(file.getName());
}
}
}
-------
package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//输入流
public class Demo4 {
public static void main(String[] args) {
File file = new File("E:"+File.separator+"a.txt");
FileInputStream fileinput =null;
try {
fileinput= new FileInputStream(file);
byte[] filebyte = new byte[1024];
int length=0;
while((length = fileinput.read(filebyte))!=-1){
System.out.print(new String(filebyte,0,length));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-------字节输入流的使用package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//CopyImages异常处理
public class Demo6 {
public static void CopyImage(){
File inputFile = new File("E:"+File.separator+"z.jpg");
File outPutFile = new File("F:"+File.separator+"a.jpg");
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try{
fileInputStream = new FileInputStream(inputFile);
fileOutputStream= new FileOutputStream(outPutFile);
byte[] filebyte = new byte[1024];
int length=0;
while((length=fileInputStream.read(filebyte))!=-1){
fileOutputStream.write(filebyte,0,length);
}
}catch (Exception e) {
System.out.println("拷贝图片失败.....");
throw new RuntimeException(e);
}finally{
try {
if(fileOutputStream!=null){
fileOutputStream.close();
System.out.println("关闭输出流成功....");
}
} catch (IOException e) {
System.out.println("关闭输出流失败....");
throw new RuntimeException(e);
}
try {
if(fileInputStream!=null){
fileInputStream.close();
System.out.println("关闭输入流成功....");
}
} catch (IOException e) {
System.out.println("关闭输入流失败....");
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
CopyImage();
}
}
------字节输入输出流Copy图片
package com.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//字节缓冲流拷贝图片
public class Demo7 {
public static void main(String[] args) {
CopyImages();
}
public static void CopyImages(){
FileInputStream fileInputStream=null;
BufferedInputStream bufferredInputStream=null;
FileOutputStream fileOutputStream=null;
BufferedOutputStream bufferedOutputStream=null;
try {
File inputFile = new File("E:"+File.separator+"z.jpg");
File outPutFile = new File("F:"+File.separator+"w.jpg");
fileInputStream = new FileInputStream(inputFile);
bufferredInputStream = new BufferedInputStream(fileInputStream);
fileOutputStream = new FileOutputStream(outPutFile);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int temp = 0;
while((temp=bufferredInputStream.read())!=-1){
bufferedOutputStream.write(temp);
// bufferedOutputStream.flush();
}
} catch (Exception e) {
System.out.println("打印照片失败...");
throw new RuntimeException(e);
// TODO: handle exception
}finally{
if(bufferredInputStream!=null){
try {
bufferredInputStream.close();
} catch (IOException e) {
System.out.println("关闭输入流失败...");
throw new RuntimeException(e);
}
}
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
System.out.println("关闭输出流失败....");
throw new RuntimeException(e);
}
}
}
}
}
-----字节缓冲流package com.file;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
//字符流
public class Demo8 {
public static void main(String[] args) throws IOException {
File file = new File("F:"+File.separator+"ArrayList.txt");
FileReader fileReader = new FileReader(file);
char[] buf = new char[1024];
int temp=0;
while((temp=fileReader.read(buf))!=-1){
System.out.println(new String(buf,0,temp));
}
}
}
-----字符流 处理中文乱码
package com.file;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//使用字符流copy文件
public class Demo9 {
public static void main(String[] args) throws IOException {
File inputFile =new File("F:"+File.separator+"a.txt");
File ouputFile = new File("E:"+File.separator+"c.txt");
FileReader fileReader = new FileReader(inputFile);
FileWriter fileWiter = new FileWriter(ouputFile);
int temp = 0;
char[] buf = new char[1024*8];
while((temp=fileReader.read(buf))!=-1){
fileWiter.write(buf,0,temp);
}
fileWiter.close();
fileReader.close();
}
}
-----字符输入输出流使用,字符流不能用来Copy图片
字节流
输入字节流:
-----------| InputStream 所有输入字节流的基类 抽象类
-----------------| FileInputStream 读取文件数据的输入字节流
-----------------| BufferedInputStream 缓冲输入字符流 该类出现的目的是为了提高读取文件 数据的效率。 这个类其实只不过是在内部维护了一个8kb的字节数组而已。
输出字节流:
-----------| OutputStream 所有输出字节流的基类。 抽象类。
----------------| FileOutputStream 向文件输出数据的输出字节流
----------------| BufferedOutputStream 缓冲输出字节流 该类出现的目的也是为了提高向文件写数据的效率。 这个类的也只不过是在内部维护了一个8kb的字节数组而已。
字符流 : 字符流 = 字节流 + 编码(解码)
输入字符流:
---------| Reader 所有输入字符流的基类。 抽象类。
----------------| FileReader 读取文件数据的输入字符流。
----------------| BufferedReader 缓冲输入字符流 该类出现的目的是为了提高读取文件数据的效率与拓展FileReader的(readLine)功能。 这个类的也只不过是在内部维护了一个8kb的字符数组而已。
输出字符流:
---------| Writer 所有输出字符流的基类。 抽象类
----------------| FileWriter 向文件输出数据的输出字符流
----------------| BufferedWriter 缓冲输出字符流 该类出现的目的是为了提高写文件数据的效率与拓展FileWriter的(newLine)功能.