FileRead方法

package test;


import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

	private static final String space_operator = " ";
	private static final double pi = Math.PI;
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws Exception {

		FileReader fr = new FileReader("demo.txt");
		
		/*
		 * 第一种方法一个字符一个字符的读取
		 */
		/*
		int ch = 0;
		while((ch = fr.read()) != -1)
		{
			System.out.print((char)ch);
		}
		*/
		
		/*
		 * 第二种方法我们直接将内容读到一个字符数组里面
		 * 返回的参数是读到了几个字符
		 */
		int len = 0;
		char[] buf = new char[1024];
		while((len = fr.read(buf)) != -1)
		{
			/*
			 * 直接转换成为字符串
			 * 用字符串的构造函数
			 */
			System.out.println(new String(buf,0,len));
		}
		
	
	}
}

  

posted @ 2019-11-03 20:07  WINDZLY  阅读(739)  评论(0编辑  收藏  举报