Java读取流文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CoderTnT/article/details/86621829
            // 1.创建文件对象
			File srcFile  = new File("D:\\a.xml");
            // 2.创建一个流,指向目标文件
			InputStream is = null;
			try {
				is = new FileInputStream(srcFile);
			//3.创建一个用来存储读取数据的缓冲数组
				byte[]array = new byte[15360];
			//4.循环往外流(count为每次读取数组中的有效字节总数)
				int count = is.read(array);
			// 5.循环打印
				while (count != -1) {
					// 将byte[] -》 String
					// 将byte数组读取到的有效字节转换成字符串
					String str= new String(array, 0, count);
                    System.out.print(str);
					count = is.read(array);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			// 6.关闭io流
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		

猜你喜欢

转载自blog.csdn.net/CoderTnT/article/details/86621829
今日推荐