Java io 操作

package tlistpackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Administrator
 *
 */
public class listdemo {
	public static void main(String[] args) throws IOException
	{
//		System.out.println(Runtime.getRuntime().getClass());
//		System.out.println(Math.round(23.55));
//		System.out.println(Math.ceil(26.333));
//		System.out.println((int)(Math.random()*10+1));
//		Date d=new Date();
//		System.out.println(d.toString());
//		//格式化时间
//		System.out.println(DateFormat.getDateInstance().format(d));
//		 char[] buffer=new char[1024];
//		                      int len=0;
// 		while((len=rf.read(buffer))!=-1)
//		{
//			System.out.print(new String(buffer,0,len));
//		}
//		WriteFile("txtdemo.txt","dirk.wang");
//		String textString= ReadFile("txtdemo.txt");
//		System.out.print(textString);
//		InputWriteFile("txtdemo.txt","dirk.wang,show time");
//		String str= InputReadFile("txtdemo.txt");
//		System.out.print(str);
		CopyFile("txtdemo.txt","new.txt");
	}
	

	/**
	 * @param path 路径
	 * @return
	 */
	@SuppressWarnings("finally")
	public static String ReadFile(String path) 
	{
		FileReader fs=null;
		String str="";
		try {
			 fs=new FileReader(path);
			char[] buffer =new char[1024];
			int len=0;
		
			while((len=fs.read(buffer))!=-1)
			{
				str+=new String(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
		        e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if(fs!=null)
			{
				try {
					fs.close();
				} catch (IOException e2) {
					e2.printStackTrace();
				}
			}
			return str;
		}
	}
	
	/**
	 * @param path  路径
	 * @param text  写入的内容
	 */
	public static void WriteFile(String path,String text)
	{
		FileWriter fw =null;
		try {
			//true 表示追加写入文件
			fw=new FileWriter(path,true);
			fw.write(text);
			//fw.write(text);
			fw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			if(fw!=null)
			{
				try {
					fw.close();
				} catch (IOException e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
		}
	}
	
	
	/**
	 * 通过二进制流读取文件
	 * @param path  路径
	 * @return
	 */
	public static String InputReadFile(String path)
	{
		File file=new File(path);
		StringBuilder str=new StringBuilder();
		if(file.isFile())
		{
			FileInputStream fs=null;
			try {
				fs=new FileInputStream(file);
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fs.read(buffer))!=-1)
				{
					str.append(new String(buffer,0,len));
				}
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally {
				if(fs!=null)
				{
					try {
						fs.close();
					} catch (IOException e2) {
						// TODO: handle exception
						e2.printStackTrace();
					}
				}
			}
		}
		
		return str.toString();
	}
	
	/**
	 * @param path  路径
	 * @param text  内容
	 */
	public static void InputWriteFile(String path,String text)
	{
		File file=new File(path);
		FileOutputStream fos=null;
		if(file.isFile())
		{
			try {
				fos=new FileOutputStream(file,true);
				fos.write(text.getBytes());
				fos.flush();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally {
				if(fos!=null)
				{
					try {
						fos.close();
					} catch (IOException e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
			}
			
		}
	}
	
	public static void CopyFile(String oldPath,String newPath)
	{
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try {
			fis=new FileInputStream(oldPath);
			fos=new FileOutputStream(newPath);
			int len=0;
			byte[] buffer =new byte[1024];
			while((len=fis.read(buffer))!=-1)
			{
				fos.write(buffer,0,len);
				fos.flush();
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
}

  

posted @ 2017-11-05 19:38  WangJunZzz  阅读(287)  评论(0编辑  收藏  举报