关于IO流和文件的个人理解

我是个喜欢代码的人~

package io;
import java.io.File;
import java.io.IOException;
//关于File常用的方法总结
public class file_ {
	public static void main(String[] args) {
		File file = new File("E:\\IO1");
		File file1 =  new File(file,"if");
		File file2 = new File(file,"you");
		
		file2.mkdir();
		file1.mkdirs();
		
		System.out.println(file2.getParent());
		System.out.println(file.getAbsolutePath());
		
		File file3 = new File("E:\\IO1\\Create1");
		try {
			if(file3.createNewFile()) {
				System.out.println("fine");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(file3.exists()) {
			System.out.println("exist");
		}
		
		if(file2.isDirectory()) {
			System.out.println("path file ");
		}
		
		if(file3.isFile()) {
			System.out.println("is norm file");
		}
		
		if(file3.delete()) {
			System.out.println("delete");
		}
		
		String[] lists = file.list();
		for(String li:lists) {
			System.out.println(li);
		}
		
		File[] files = file.listFiles();
		for(File file5:files) {
			System.out.println(file5.getAbsolutePath());
		}
		
		
		
	}
}
package io;

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

public class Io_ {

	public static void main(String[] args) {
		//字节流
		FileInputStream input  =null;//常用方法  int read(),int read(byte[]b),void close()
		FileOutputStream ouput =null;//常用方法void write(int b),int write(byte[]b,begin,long);void close()
		try {
			ouput = new FileOutputStream("E:\\IO1\\f");
			input = new FileInputStream("E:\\gaohuqun\\ec\\eclipse\\artifacts.xml");
			byte []b=new byte[1024];
			int a;
			while((a=input.read(b))!=-1) {
				ouput.write(b,0,a);
			}
			ouput.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(input!=null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(ouput!=null) {
				try {
					ouput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
package io;

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

public class Io__ {

	public static void main(String[] args) {
		FileReader  filereader = null;
		FileWriter  filewriter = null;
		try {
			filereader = new FileReader("E:\\IO1\\a.txt");//int read,int read(char[]b),void close()
			filewriter = new FileWriter("E:\\IO1\\b.txt",true);//void write(int c) void write(char[] ,begin ,len)
			char [] c =new char[1024];
			int a;
			while((a=filereader.read(c))!=-1) {
				filewriter.write(c, 0, a);
			}
			filewriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(filereader!=null) {
				try {
					filereader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(filewriter!=null) {
				try {
					filewriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		}
	}
	
}

 

你可能感兴趣的:(java)