113day(Java流)

本文探讨了Java中标准输入流的使用方法,并对比了不同操作系统下输入终止符的区别。通过实例演示了如何从控制台读取用户输入并显示其字节数,同时介绍了在Windows和Mac OS系统中回车换行符的不同表现。此外,还提供了一个简单的文件写入示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《2018年2月1日》【连续113天】

标题:Java流;

内容:

A.

 

public static void main(String[] args) {
		System.out.println("Hello world!");
		byte[] buffer =new byte[1024];
		try {
			int len =System.in.read(buffer);
			String s =new String(buffer,0,len);
			System.out.println("读到了"+len+"个字节");
			System.out.println(s);
			System.out.println("s的长度:"+s.length());
		} catch (IOException e) {
			e.printStackTrace();
		}


其中,汉字占两个字节,普通字符是一个字节;

我在Mooc上观看老师的操作,输入n个字符,len是n+1,但我在eclipse运行上,len一直是n+2,debug时,发现如下:
以输入Aa为例:
字节数组的内容如下:

13,10在ASCII码表中查得为回车键,换行键;

问题猜想:老师用的是Mac os系统,它的回车换行是\r,

而我用的是windows系统,换行是\r\n。

打开macbook验证了一下:

 

B.文件读写:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello world!");
		byte[] buffer =new byte[1024];
		for(int i=0;i<buffer.length;i++) {
			buffer[i]=(byte)i;
		}
		try {
			FileOutputStream out =new FileOutputStream("a.dat");
			out.write(buffer);
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


建立了一个a.dat文件;

 

明日计划:学习;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值