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
			}
		}
				
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值