Java I/O操作二

从文件中读出数据

用FileReader实现

/*
 * 读取指定文件内容,并打印到终端
 * @param path	待读取的文件路径
 * @throws IOException
 */
	public static void readOneFile(String path) throws IOException {
		//创建FileReader对象
		FileReader fin = new FileReader(path);
		//定义一次读取的字符数目
		char [] cbuf = new char[30];
		int iLength = 0, iRead = 0;
		while (iRead  != -1) {
			iRead = fin.read(cbuf);
			System.out.println(iRead);
			//
			if (iRead == -1)
				break;
			iLength += iRead;
			//打印读取的内容
			System.out.printf("(%d) %s\n", iRead, new String(cbuf, 0, iRead));
		}
		fin.close();
		//打印读取内容的长度
		System.out.println(iLength);
	}

猜你喜欢

转载自blog.csdn.net/dongcheng123456789/article/details/88599464