现在的位置: 首页 > 综合 > 正文

20110829 日作业,复制文件,简单IO操作,我自己扩展过的,比题干要求的功能多点

2018年05月11日 ⁄ 综合 ⁄ 共 1650字 ⁄ 字号 评论关闭
/*
*	20110829 日作业
*	coded by Younger.shen申延刚 younger.x.shen@gmail.com
*	blog : blog.csdn.net/hack2me
*	blog : youngershen.com/blog/  (you should cross GFW to view my sites)
*   
*/


import java.util.*;
import java.io.*;


public class HomeWork1{

	private BufferedReader reader = null;
		
	private BufferedWriter writer = null;

	private String fileName = null;

	private static int start = -1;

	//private String dumpFileName = null;

	private ArrayList<String> dumpString = new ArrayList<String>();

	public HomeWork1(){
	
			fileName = "./xx.txt";
	
	}

	public HomeWork1(String path){
		
		fileName = path;
	
	}

	private void gatherString(){
	
		try{
			
				reader = new BufferedReader(new FileReader(fileName));

				String temp = reader.readLine();

				while(temp != null){
		
					
					System.out.println(temp);

					dumpString.add(temp);

					temp = reader.readLine();
		
				}
				

		
		}catch(IOException e){
			
			
				System.out.println(e.toString());


			}finally{
			
					if(reader!=null){

						try{
						
								reader.close();

						}catch(IOException e){
						
							System.out.println("IO Error");
						}
					}
			
			}
				
				
		
		}
	
	
	private String getString(int i){
	
			return dumpString.get(i);
			
	
	}
	public void dumpToFile(){
	
		//reader = new BufferedReader(new FileReader(fileName));
		gatherString();

	try{
		writer = new BufferedWriter(new FileWriter("temp.txt",true));
		//System.out.println("i am here");
		System.out.println(dumpString.size());
		for(int i = 0; i < dumpString.size();i++){
		
			String t = getString(i);
		//	System.out.println("i am 1");
			writer.write(t,0,t.length());
			writer.write("\r");
			writer.write("\n");
		//	System.out.println("writing");
			writer.flush();
		}
	}catch(IOException e){
	
	
			System.out.println("dump error");
	
	}finally{
	
		try{

			writer.close();
		
		}catch(IOException e){
		
			System.out.println("IO Error");	
		}
	}

		
	
	}

	public static void main(String[] args){
	
			if(args.length == 0){
			
				HomeWork1 hw1 = new HomeWork1();
				hw1.dumpToFile();
				//System.out.println("dumoing");
			
			}else {
			
				HomeWork1 hw1 = new HomeWork1(args[0]);
				hw1.dumpToFile();
			
			}
	
	}
}

抱歉!评论已关闭.