Java io文件操作


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//參考文章
//https://www.cnblogs.com/xll1025/p/6418766.html
//http://www.cnblogs.com/xdp-gacl/p/3634409.html
//https://www.cnblogs.com/xing901022/p/3933417.html
public class IOpractise {


	public void  iotest() {
		int b= 0;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("C:\\Users\\userName\\Desktop\\test.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("系統找不到指定文件!");
			System.exit(-1);
			
		}
		
		long num =0;
		try {
			while((b=fis.read())!=-1){
				//System.out.println((char)b);
				System.out.println(b);
				num++;
			}
			fis.close();
			System.out.println();
			System.out.println("總共讀了"+num + "個字節的文件");
		} catch (IOException e) {		
			e.printStackTrace();
			System.out.println("文件讀取錯誤!");
		}
	
	}
	
	public static void main(String[] args) throws IOException {
		IOpractise iop = new IOpractise();
		//iop.iotest3("C:\\Users\\userName\\Desktop\\webelement.txt");
		iop.iotest ();
	}
	
	public void iotest1() throws IOException{
		
		try {
			FileInputStream fis = new FileInputStream("C:\\Users\\userName\\Desktop\\test.txt");
			int read = 0;
			while((read=fis.read())!=-1){
				System.out.println(read);				
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
	
	public String iotest2() throws IOException{
		StringBuffer result = new StringBuffer();
		
		try {
			BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\userName\\Desktop\\webelement.txt"));
			String str = null;
			while ((str=bf.readLine())!=null){
				result.append(str);
				//System.out.println(result);				
			}
			bf.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return result.toString();
	}
	
	
	public void iotest3(String filename) throws IOException{
		StringBuffer result = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String str = null;
			while((str=br.readLine())!=null){
				result.append(System.lineSeparator()+str);
				//System.out.println();
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(result.toString());
		
	}
	
	public void iotest4() throws IOException{
		try {
			FileWriter fw = new FileWriter("C:\\Users\\userName\\Desktop\\1111111111111.txt");
			
			fw.write("123llove");
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			FileReader fr = new FileReader("C:\\Users\\userName\\Desktop\\1111111111111.txt");
			int it =0;
			while((it=fr.read())!=-1){
				System.out.print((char)it);
				
			}
			fr.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void iotest5(){
		try {
			FileReader fr = new FileReader("C:\\Users\\userName\\Desktop\\webelement.txt");
			int it =0;
			char[] buf = new char[10];
			StringBuilder sb = new StringBuilder();
			try {
				while((it=fr.read(buf))!=-1){
					sb.append(new String(buf,0,it));
				}
				String str = sb.toString();
				System.out.println(str);
				fr.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	public void iotest6() throws IOException{
		FileWriter fw = new FileWriter("C:\\Users\\userName\\Desktop\\1111111111111.txt",true);
		try {
			fw.write("1234567890testtest!!!!!!!");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void iotest7(){
		try {
			FileReader fr = new FileReader("C:\\Users\\userName\\Desktop\\1111111111111.txt");
			FileWriter fw = new FileWriter("C:\\Users\\userName\\Desktop\\2222222222222.txt");
			char [] buf = new char[2];
			int it =0;
			while ((it=fr.read(buf))!=-1){
				String str = new String(buf,0,it);
				fw.write(str);
			}
			fw.flush();
			fw.close();
			fr.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void iotest8() throws IOException{
		try {
			FileReader fr = new FileReader("C:\\Users\\userName\\Desktop\\2222222222222.txt");
			BufferedReader br = new BufferedReader(fr);
			String str = null;
			StringBuilder sb = new StringBuilder();
			while ((str=br.readLine())!=null){
				sb.append(str);
			}
			br.close();
			System.out.println(sb.toString());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void iotest9() throws IOException{
		try {
			BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\userName\\Desktop\\2222222222222.txt"));
			BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\userName\\Desktop\\3333333333333.txt"));
			String str = null;
			while ((str=br.readLine())!=null){
				bw.write(str);
				bw.newLine();
			}
			bw.flush();
			bw.close();
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void iotest10() throws IOException{
		try {
			FileInputStream fis = new FileInputStream("C:\\Users\\userName\\Desktop\\3333333333333.txt");
			FileOutputStream fos = new FileOutputStream("C:\\Users\\userName\\Desktop\\4444444444444.txt");
			int it = 0;
			byte[] byt = new byte[1024];
			while ((it=fis.read(byt))!=-1){
				fos.write(byt,0 ,it);
			}
			fos.flush();
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	

	

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章