java文件及读写操作

import java.io.File;
import java.io.FilenameFilter;

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

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

import java.io.BufferedReader;
import java.io.BufferedWriter;


public class FileTest{
	
	public static void main(String[] args){
		
		/*String parentPath = "c:/1";
		File parent = new File(parentPath);
		
		String child = "abc.txt";
		File file = new File(parent, child);*/
		
		/*String pathname = "c:/1/abc.txt";
		File file = new File(pathname);*/
		
		//第三种构造函数
		/*String parent = "c:/1/";
		String child = "abc.txt";
		File file = new File(parent, child);
		
		if(file.exists() == false){
			file.createNewFile();
		}*/
		
		//File file = new File("c:/1/");
		/*
		String[] list = file.list();
		for(String name : list){
			System.out.println(name);
		}*/
		
		//File[] list = file.listFiles();
		//for(File f : list){
			//System.out.println(f.getName());
			//System.out.println(f.getPath());
			//System.out.println(f.getParent());
			//System.out.println(f.getParentFile().getName());
			//System.out.println(f.getParentFile().getPath());
		//}
		/*
		File file = new File("c:/1/abc.txt");
		file.delete();*/
		
/* 		File file = new File("c:" + File.separator + "1");
		String[] list = file.list(new FilenameFilter(){
			
			public boolean accept(File dir, String name){
				
				if(name.endsWith(".java123")){
					return true;
				}
				return false;
			}
		});
		
		String[] list1 = {};
		System.out.println(list1.length);
		
		for(String item : list){
			System.out.println(item);
		} */
		/*
		File file = new File("c:/2");
		System.out.println(file.isDirectory());
		
		if(file.isDirectory()){
			System.out.println("文件夹已存在");
		}else{
			file.mkdir();
		}
		*/
		/*
		FileInputStream fs = null;
		
		try{
			
			fs = new FileInputStream("c:/1/abc.txt");//abc.txt必须存在,否则Exception in thread "main" java.io.FileNotFoundException
			byte[] bytes = new byte[1024];
			bytes[0] = '$';
			
			int realCount = 0;
			while((realCount = fs.read(bytes, 1, 1023)) != -1){
				String str = new String(bytes, 0, realCount);
				System.out.println(str);
			}
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			try{
				
				fs.close();
			}catch(Exception e){
				
				e.printStackTrace();
			}
			
		}*/
		/*
		FileOutputStream fo = null;
		try{
			
			String str = "关于南海问题,\r\n中国是有权抵制的。";
			fo = new FileOutputStream("c:/1/abc.txt");
			
			fo.write(str.getBytes());
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			try{
				fo.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}*/
		
		/*
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try{
			fis = new FileInputStream("c:/1/K.jpg");
			fos = new FileOutputStream("c:/1/L.png");
			
			byte[] bytes = new byte[2018];
			int realCount = 0;
			
			while((realCount = fis.read(bytes)) != -1){
				
				fos.write(bytes, 0, realCount);
				fos.flush();
			}
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			try{
			
				fos.close();
				fis.close();
			}catch(Exception e){
				
				e.printStackTrace();
			}
		}*/
		
		/*
		FileReader fr = null;
		FileWriter fw = null;
		
		try{
			
			fr = new FileReader("c:/1/readme.txt");
			fw = new FileWriter("c:/1/abc.txt");
			
			char[] chars = new char[1024];
			int realCount = 0;
			
			while((realCount = fr.read(chars)) != -1){
				
				fw.write(chars, 0, realCount);
				fw.flush();
			}
			
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			
			try{
			
				fr.close();
				fw.close();
			}catch(Exception e){
				
				e.printStackTrace();
			}
		}*/
		
		//如果字符文件比较小,可以通过使用BufferedReader, BufferedWriter提高读写速度
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		String str = null;
		
		try{
			
			br = new BufferedReader(new FileReader("c:/1/readme.txt"));
			bw = new BufferedWriter(new FileWriter("c:/1/abc.txt"));
			
			while((str = br.readLine()) != null){
				bw.write(str + "\r\n");
				bw.flush();
			}
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			
			try{
				
				br.close();
				bw.close();
			}catch(Exception e){
				
				e.printStackTrace();
			}
		}
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值