IO流

//ByteArrayInputStream
public static void main(String[] args) throws IOException {

		String str = "zsy love jh !";
		byte[] byteStr = str.getBytes();

		ByteArrayInputStream bais = new ByteArrayInputStream(byteStr);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		int c;
		while ((c = bais.read()) != -1) {
			baos.write(c);

		}

		FileOutputStream os = new FileOutputStream("zsy.txt");

		String o = baos.toString();
		System.out.println(o);
		baos.writeTo(os);
		bais.close();
		baos.close();

	}

}


z这个例子比较好:

public static void main(String[] args) throws Exception {

		// OutputStream os = new FileOutputStream("D:/jhh.txt");

		// BufferedOutputStream bos = new BufferedOutputStream(os);

		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				"D:/zsy.txt"));

		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("jhh.txt"));

		byte[] buffer = new byte[1024];
		int length = 0;
		while ((length = bis.read(buffer, 0, 1024)) != -1) {
			bos.write(buffer, 0, length);
		}

		bis.close();
		bos.close();

	}


Finputstream

public static void main(String[] args) throws Exception {

		//读
		InputStream is = new FileInputStream("D:/zsy.txt");

		//写
		OutputStream os = new FileOutputStream("D:/jh.txt", true);

		byte[] buffer = new byte[1024];

		int length;
		while ((length = is.read(buffer, 0, 1024)) != -1) {

			String str = new String(buffer, 0, length);
			System.out.println(str);
			os.write(buffer, 0, length);
		}

		is.close();
		os.close();

	}


BufferedReader reader = new BufferedReader(new FileReader(
				"BufferedOutPutStreams.java"));

		BufferedWriter writer = new BufferedWriter(
				new FileWriter("t.txt", true));

		String str;
		while ((str = reader.readLine()) != null) {
			System.out.println(str);
			writer.write(str + "\n");
		}

		reader.close();
		writer.close();

	}

private ByteArrayOutputStream getBaos(TJzxtRyRxzp tJzxtRyRxzp) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		if (tJzxtRyRxzp != null) {
			try {
				InputStream in = new ByteArrayInputStream(tJzxtRyRxzp.getRxzp());
				BufferedInputStream bis = new BufferedInputStream(in);
				BufferedOutputStream bos = new BufferedOutputStream(baos);
				int i;
				while ((i = bis.read()) != -1) {
					bos.write(i);
				}
				bos.flush();
				bis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return baos;
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值