java 文件基本操作總結

這裏對java對文件的基本操作進行了總結,代碼實現了最常用的幾種操作,其中包括有:控制檯輸入,文件讀取,文件寫入,還有文件拷貝


代碼貼上:


/*
	 * 控制檯輸入一行
	 */
	public static String consoleInput()
	{
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(System.in));
		String line = null;
		try
		{
			line = reader.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if(reader != null)
			{
				try
				{
					reader.close();
					reader = null;
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return line;
	}
	
	/*
	 * 文件寫入操作
	 * 按照系統默認編碼像文件中寫入一行
	 */
	public static void writeLine(String str, String filePath)
	{
		BufferedWriter bw = null;
		
		try
		{
			bw = new BufferedWriter(new FileWriter(filePath));
			bw.write(str);
			bw.newLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if(bw != null)
			{
				try
				{
					bw.close();
					bw = null;
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	/*
	 * 文件的寫入操作
	 * 按照指定編碼格式寫入文件
	 */
	public static void writeLine_setChar(String str)
	{
		String filePath = "a.txt";
		BufferedWriter bw = null;
		
		
		
		//設置編碼格式
		//String charSet = System.getProperty("file.encoding"); 
		String setChar = "utf-8";
		
		try
		{
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), setChar));
			bw.write(str);
			bw.newLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if(bw != null)
			{
				try
				{
					bw.close();
					bw = null;
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	/*
	 * 文件的讀取操作
	 * 把一個文件中的內容輸出到控制檯上
	 */
	public static void readFileToConsole(String filePath)
	{
		BufferedReader br = null;
		try
		{
			//按照系統默認編碼格式讀取文件
			//br = new BufferedReader(new FileReader(filePath));
			
			
			//按照指定的編碼格式讀取文件
			String charSet = "gbk";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charSet));
			String line;
			while((line = br.readLine()) != null)
			{
				System.out.println(line);
			}
			
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			if(br != null)
			{
				try
				{
					br.close();
					br = null;
				} catch (Exception e2)
				{
					System.out.println(e2.getMessage());
				}
			}
		}
	}
	
	public static void copyFile(String source, String destination)
	{
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		try
		{
			String charSet = "utf-8";
			br = new BufferedReader(new InputStreamReader(new FileInputStream(source), charSet));
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), charSet));
			String line;
			while((line = br.readLine()) != null)
			{
				bw.write(line);
				bw.newLine();
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if(br != null)
				{
					br.close();
					br = null;
				}
				if(bw != null )
				{
					bw.close();
					bw = null;
				}
			} catch (IOException e2)
			{
				// TODO: handle exception
			}
		}
				
	}


發佈了17 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章