java IO流学习笔记一

IO 两大主流 16位和8位



16位对应writer和reader

fileWriter /fileReader 对文件进行读写

示例代码:

FileWriter out=new FileWriter("test.txt");
out.write("张三");
out.close();


char[] buf=new char[1024];

FileReader in=new FileReader("test.txt");
int len=in.read(buf);
System.out.println(new String(buf,0,len));

StringWriter/StringReader 对内存里的位置进行读写

示例代码如下:

try {
throw new Exception("test");
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
System.out.println(e.getMessage());
// System.out.println(sw.toString());
}

PipedWriter/PipedReader 两个线程间进行通讯

 

 

 

8位对应InputStream和OutputStream

 

fileInputStream/FileOutputStream 对文件进行读写

示例代码:

public static void main(String[] args) throws Exception
{
FileOutputStream out=new FileOutputStream("test.txt");
out.write("张三".getBytes());

byte[] buf=new byte[1024];
File f=new File("test.txt");
FileInputStream in=new FileInputStream(f);
int len=in.read(buf);
System.out.println(new String(buf,0,len));
in.close();






}

 

ByteArrayInputStream/ByteArrayOutputStream 对字节数组进行读写

示例代码:

public static void main(String[] args)
{
String tmp="abc";
byte[] src=tmp.getBytes();
ByteArrayInputStream input=new ByteArrayInputStream(src);
ByteArrayOutputStream output=new ByteArrayOutputStream();
transform(input,output);
byte[] result=output.toByteArray();
System.out.println(new String(result));

transform(System.in,System.out);

}
public static void transform(InputStream in,OutputStream out)
{
int ch=0;
try
{
while((ch=in.read())!=-1)
{
int upperCh=(int)(Character.toUpperCase((char)ch));
out.write(upperCh);
}
}catch(Exception e)
{
e.printStackTrace();
}
}

 

PipedInputStream/PipOutputStream 线程间通讯使用

示例代码:

class Sender extends Thread {
private PipedOutputStream out = new PipedOutputStream();


public PipedOutputStream getOutputStream() {
return out;
}


public void run() {
String strInfo = new String("hello,receiver!");
try {
out.write(strInfo.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}


}


class Receiver extends Thread {
private PipedInputStream in = new PipedInputStream();


public PipedInputStream getInputStream() {
return in;
}


public void run() {
String strInfo = new String("hello,receiver!");
byte[] buf = new byte[1024];


try {
int len = in.read(buf);
System.out.println("the following message comes from sender:\n" + new String(buf, 0, len));
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}


}


class PipedStreamTest {
public static void main(String[] args) throws Exception {
Sender t1 = new Sender();
Receiver t2 = new Receiver();


PipedOutputStream out = t1.getOutputStream();
PipedInputStream in = t2.getInputStream();
out.connect(in);


t1.start();
t2.start();
}


}


注:数据流中不存在字符串的IO操作,那样要用reader和writer
二者的桥梁在inputStreamReader/outputStreamWriter
bufferedWriter/bufferedReader

bufferedInputStream/bufferedOutputStream

示例代码:

public static void main(String[] args) throws Exception
{
FileOutputStream fos=new FileOutputStream("count.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos);
dos.writeUTF("ab中国");
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();


FileInputStream fis=new FileInputStream("count.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
System.out.println(dis.readUTF());
byte[] buf=new byte[1024];
int len=dis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();

}

 

来提高IO访问速度的



高级IO操作

信道IO java.nio.channels



java流的处理上分为
字符流 处理单元为2个字节的unicode字符,操作字符\字符数组\字符串
Reader
CharArrayReader()
StringArrayReader()
FileReader()



Writer
CharArrayWriter()
StringWriter()
FileWriter()
PipedWriter()








字节流 处理一个字节,操作字节和字节数组
inputStream
ByteArrayInputStream() 创建一个字节数组输入流
StringBufferInputStream() 把一个String 作为一个inputStream
FileInputStream() 把一个文件作为一个inputStream,实现文件读取
PipInputStream() 创建一个输入流管道
SequenceInputStream() 序列输入流,把多个inputStream合并


outputStream
ByteArrayOutputStream() 把信息存入内存中的一个缓冲区
FileOutputStream ()
PipedOutputStream() 通讯管道的发送端



两种不同导向的stream之间的转换
把一个字节为导向的stream转换为一个以字符为导向的stream





java IO的一般使用原则
1 按数据来源(去向)分类
是文件: fileInputStream, fileOutputStream, fileReader, fileWriter
是byte[]: byteArrayInputStream/byteArrayOutputStream
是char[]: charArrayReader/charArrayWriter
是String stringBufferInputStream/StringBufferOutputStream
StringReader/StringWriter
网络数据流: inputStream/outputStream reader writer




2 按是否格式化输出分:
printStream /printWriter

3 按是否要缓冲分:
bufferedInputStream/bufferedOutputStream
bufferedReader/burreredWriter

4 按数据格式分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值