文件


文件流


Java IO流类


分类


File实例

import java.io.File;
import java.io.IOException;

public class Demo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//打开一个文件
		File f = new File("d:\\aa.txt");
		
		System.out.println("文件路径" + f.getAbsolutePath());
		
		System.out.println("剩余空间" + f.getFreeSpace());
		
		//创建文件
		File f2 = new File("d:\\fasd.txt");
		if ( !f2.exists() ) {
			//创建文件
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} else {
			System.out.println("文件已经存在");
		}
		
		//创建文件和文件夹
		File f3 = new File("d:\\ff");
		
		if( !f3.isDirectory() ){
			try {
				f3.mkdir();
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			System.out.println("文件夹已经存在");
		}
		
		//列出一个文件夹下面的所有文件
		File f4 = new File("D:\\ff");
		
		if ( f4.isDirectory()) {
			File[] lists = f4.listFiles();
			for (int i = 0; i < lists.length; i++) {
				System.out.println("list" + i + ":" + lists[i].getName());
			}
		}
	}

}

FileInputStream实例

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo3 {
	public static void main(String[] args) {
		File f = new File("D:/aa.txt");
		FileInputStream fis = null;
		//引文File没有读写能力,所以需要使用InputStream
		try {
			
			fis = new FileInputStream(f);
			
			//定义一个字节数组
			byte[] bytes = new byte[2048];
			int n = 0;
			
			//循环读取
			while( (n=fis.read(bytes)) != -1){
				//吧字节转换成String
				String s = new String(bytes, 0 , n);
				System.out.println(s);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			//关闭文件流必须放这里
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

FileOutputStream实例

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestFileOutputStream {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File f = new File("d:/aa.txt");
		
		FileOutputStream fos = null;
		
		try {
			fos = new FileOutputStream(f);
			
			String s = "把地球甩掉\r\n";
			String s2 = "一颗心扑通扑通扑通地乱跳";
			//定义字节数组
			fos.write(s.getBytes());
			fos.write(s2.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}


ImageStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class TestImage {
	public static void main(String[] args) {
		//先把图片读入到内存 ——》写入某个文件
		//因为是二进制文件,因此使用字节流完成。
		
		//输入流
		
		FileInputStream fis = null;
		
		//输出流
		
		FileOutputStream fos = null;
		
		try {
			fis = new FileInputStream("D:/3.PNG");
			fos = new FileOutputStream("D:/computer.PNG");
			
			byte[]	 buf = new byte[512];
			int n = 0;
			//循环读取
			while ((n = fis.read(buf)) != -1) {
				//输出到指定文件。
				fos.write(buf);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

文件字符流

Reader

Writer

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestReaderWriter {
	public static void main(String[] args) {
		//文件中取出字符流对象(输入流)
		FileReader fr = null;
		//写入文件(输出流)
		FileWriter fw = null;
		
		try {
			//创建dr对象
			fr = new FileReader("D:/test.txt");
			
			int n = 0;
			//读入到内存
			char c[] = new char[1024];
			while ( (n = fr.read(c))!= -1) {
				String s = new String(c,0,n);
				
				System.out.println(s);
			}
			
			while ( (n = fr.read(c)) != -1) {
				fw.write(c, 0 ,n);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			try {
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

缓冲字符流

BufferedReader

BufferedWriter

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferRW {
	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			//先创建FileReader对象
			FileReader fr = new FileReader("D:/test.txt");
			br = new BufferedReader(fr);
			
			//先创建FileWriter对象
			FileWriter fw = new FileWriter("D:/my.txt");
			bw = new BufferedWriter(fw);
			
			//循环读取文件
			String s = "";
			while ( (s = br.readLine()) != null) {
				System.out.println(s);
			}
			//循环写文件
			String s2 = "";
			while ( (s2 = br.readLine()) != null) {
				bw.write(s2  + "\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}








本文转载:CSDN博客