JavaI/O操作四

向文件中写入数据

用FileInputStream和FileOutputStream实现txt文件的复制

/*
*将E://dss.txt文件内容复制到d:new 1.txt中
*/
public static void main(String[] args) throws IOException {
		 //TODO Auto-generated method stub
		FileInputStream fin = new FileInputStream("E://dss.txt");
		FileOutputStream fout = new FileOutputStream("d:new 1.txt");
		//定义一次复制的字节数,注意大小需要是2的整数次方,否则可能出现乱码
		byte[] bt = new byte[1024];
		int iRead = 0;
		//读取E://dss.txt中的内容
		while(-1 != (iRead = fin.read(bt))) {
			//写入到d:new 1.txt中
			fout.write(bt, 0, iRead);
		}

		fout.close();
		fin.close();

		
		print("E://dss.txt","E://sss.txt");
	}

此外,FileInputStream和FileOutputStream还可以完成其它格式的文件读写操作

猜你喜欢

转载自blog.csdn.net/dongcheng123456789/article/details/88599923
今日推荐