IO的使用说明

IO有字符流和字节流两种,字节流可以读取任何数据(图片、二进制、纯文本、properties...)字符流只能读纯文本。切记!!!!!

怎么看要用相对应的Io类

一、

源:InputStream,Reader

目的:OutputStream,Writer

二、

明确具体的设备。

是硬盘就加File;

是键盘用System.in(是一个InputStream对象);

是内存用数组;是网络用Socket流。

同样目的是哪个设备:

是硬盘就加File;

是键盘用System.out(是一个OutoutStream对象);

是内存用数组;是网络用Socket流。

三、

明确是否还需要其他额外功能呢,例如

①是否需要较高的效率,即是否需要使用缓冲区,是就加上Buffered;

②是否需要转换,是,就使用转换流,InputStreamReader 和OutputStreamWriter。


InputStream:

一般不用他,一般用FileInputStream

构造可以用File也可以直接写路径

读取方法:

while((i = input.read())!=-1){
System.out.println((char)i);
}

当read()返回-1的时候说明到尾了!

但是还有两个read()用法



intread()
从此输入流中读取一个数据字节。
intread(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
intread(byte[] b, int off, int len)
从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。

如第二个用法:

例1:

byte[] buf = new byte[12];
int len = 0;
while((len =input.read(buf))!=-1){


           System.out.print(new String(buf, 0, len));  
}


例2:

  1.  byte[] buf = new byte[12];  
  2.             int len = in.read(buf);  
  3.               
  4.             System.out.println(buf);  
  5.             System.out.println(new String(buf, 0, len));  

一般不用Reader,因为如果使用就要重写其中的两个方法,所以一般使用FileReader或者BufferReader

FileReader:

读取方法:

while((i=reader.read())!=-1){
System.out.println((char)i);
}
这里和InputStream的一样,但是注意他没有readLine()方法!!


BufferReader:

他有readLin()方法!不过他的构造需要一个FileReader对象!!

读取方法:

BufferedReader bReader = new BufferedReader(reader);
String line = null;

while((line=bReader.readLine())!=null){
System.out.println(line);
}

一行行读取!!


写文件:

FileOutputStream

File file = null;
FileOutputStream output = null;
try {
file = new File("src/info.properties");
output = new FileOutputStream(file,true);

byte[] data = "testtest".getBytes("GB2312");
output.write(data);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(output!=null)
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

在构造的时候回发现有多重构造方法:

FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(FileDescriptor fdObj)
创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。

FileOutputStream(File file, boolean append)  append 如果为true则是在后面追加信息


FileWriter  :


FileWriter  wt = null;
try {
wt = new FileWriter ("E:/b.txt",true);

wt.write("oo=oo");
wt.write("\r\nxx=xx");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

try {
wt.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


回车是\t\n


byte和string相互转换

1、string 转 byte[]

	String str = "Hello";
	byte[] srtbyte = str.getBytes();

2、byte[] 转 string

	byte[] srtbyte;
	String res = new String(srtbyte);
	System.out.println(res);

3、设定编码方式相互转换

	String str = "hello";
	byte[] srtbyte = null;
	try {
    		srtbyte = str.getBytes("UTF-8");
    		String res = new String(srtbyte,"UTF-8");
    		System.out.println(res);
	} catch (UnsupportedEncodingException e) {
   		 // TODO Auto-generated catch block
    		e.printStackTrace();
	}
其中有涉及带编码: UTF-8、gbk
PlatformIO 是一个用于嵌入式系统开发的开源生态系统,提供统一的开发环境和交互式命令行工具,支持多种开发平台和框架。使用 PlatformIO 可以方便地进行项目管理、库管理、跨平台编译和调试等。而 Blinker 是一个基于 IoT 的应用开发平台,提供了设备快速接入、控制和数据可视化等功能。 要在 PlatformIO使用 Blinker,你需要遵循以下步骤: 1. 安装 PlatformIO:如果你还没有安装 PlatformIO,可以通过 PlatformIO 的集成开发环境(IDE)扩展在 Visual Studio Code、Atom 等代码编辑器上进行安装。 2. 创建项目:在 PlatformIO IDE 中创建一个新项目,并选择对应的开发板和框架。确保你选择的开发板和框架支持 Wi-Fi 功能,因为 Blinker 通常需要网络连接。 3. 添加 Blinker 库:在 PlatformIO 中,你可以通过库管理器搜索并添加 Blinker 库到你的项目中。你可以通过 PlatformIO 的库搜索功能找到 Blinker 库,并按照说明进行安装。 4. 编写代码:在 Blinker 库添加到项目后,你需要编写代码以实现设备的初始化、连接到 Blinker 平台以及定义 Blinker 对象的功能。Blinker 库通常会提供一些示例代码和API文档,你可以根据这些资源来编写你的应用程序。 5. 配置网络信息:在代码中,你需要正确配置设备的 Wi-Fi 信息以及 Blinker 平台的认证信息,包括设备的 UUID 和 Token。这些信息可以从 Blinker 平台获取。 6. 编译上传:完成代码编写和配置后,你可以通过 PlatformIO 的编译和上传功能将代码烧录到你的开发板上。确保你的设备连接到了电脑,并且正确配置了 PlatformIO 的上传设置。 7. 运行和调试:上传代码后,设备应该能够连接到 Blinker 平台,并且你可以开始使用 Blinker 提供的控制和数据功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值