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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值