Java IO流

Java IO流,第1张

Java IO流 IO包中的其他类 

IO包中的其他类
打印流o
·  PrintWriter 与 PrintStream
· 可以直接 *** 作输入流和文件。
序列流
· SequencelnputStream
·对多个流进行合并。
*** 作对象
ObjectlnputStream 与 ODjieaiOOutputStream
·被 *** 作的对象需要实现 Serializable (标记接口);
·练习:文件分割程序。

RandomAccessFile
·随机访问文件,自身具备读写的方法。
·通过skipBytes(intx), seek(intx)来达到随机访问。
管道流
PipedInputStream 和 PipedOutputStream
·输入输出可以直接进行连接,通过结合线程使用。 

			import java.io.*;
			class Read  implements  Runnable
			{
				private  PipedInputStream  in;
				Read(PipedInputStream i)
				{
					this in = in /
				}
				public void run()
				{
					try
					{

						byte[] buf = new byte[1024];
						System.out.println("读取前。。没有数据阻塞");
						int len = in.read(buf);
						System.out.println("读到数据。。但塞结束");

						String s = new String(buf, 0, len);
						system.out.println(s);
						in.close();
					}
					catch (IOException e)
					{
						throw new  RuntimeException ("管道读取流失败");
					}
				}
			}
			class Write  implements  Runnable
			{
				private  PipedOutputStream  out;
				Write(PipedOutputStream  out)
					this.out = out;
			}
			public void run()
			{
				try
				{
					system.out.printin("开始写入数据、筹待6秒后。");

					Thread.sleep(6000);
					out.write("piped lai la".getBytes());
					out.close();
				}
				catch (Exception e)
				{
					throw new  RuntimeException("管道输出流失败");
				}
			}
		}
				
		class  PipedStreamDemo
		{
			public static void main(String[]args) throws  IOException
			{
				PipedInputStream  in = new  PipedInputStream();
				PipedOutputStream  out = new  PipedOutputStream();
				in.connect(out);
				Read r = new Read();
				write w = new weite();
			}
		}
			


· *** 作基本数据类型
DatalnputStream 与 DataOutputStream
· *** 作字节数组
" ByteArraylnputStream 与 ByteArrayOutputStream
· *** 作字符数组I
CharArrayReader 与 CharArrayWrite
· *** 作字符串
StringReader 与 StringWriter

		mport java.io.*;
		class  DataStreamDemo

		{
			public static void main(String[] args) throws  IOException
			{
				//writeData():
				//readData();
				// writeUTFDemo ();
				//OutputStreamWriterosw  = new OutputStreamWriter(new File Output Btream("ghk. txt"), "ghk");
				//osw.write(“你好”);
				//osw.close();
				// readUTFDemp();

			}

			readUTFDemo();
		}
		public static void  readUTFDemo ()
		{
			DataInputStream  dis = new  DataInputStream (new File  Inputstream ("utfdate."));
			String s = dis.readUTF();
			System.out.println(s);
			dis.close();
		}

		public static void  writeUTFDemo () throws  IOException 
		{
			Dataoutputstream  dos = new  Dataoutputstream (new  Fileoutput  Stram("utfdate. txt"));
			dos writeUTF("你好");
			dos.close();
		}
	}
		

 

用于 *** 作字节数组的流对象。
ByteArrayInputstream :在构造的时候,需要接收数据源,。而且数据源是一个字节数组,
Bytechnicatgoutsbrean :在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组,这就是数据目的地.
因为这两个流对象都 *** 作的数组,并没有使用系统资源
所以,不用进行close关闭,
在流 *** 作规律讲解时:
源设备,
键盘System. in,硬盘 FileStream ,内存 Arraystream .目的设备:
控制台system. out,硬盘 Filestream ,内存 ArrayStream .
用流的读写思想来 *** 作数据。

	import java.io.*;
	class  ByteArrayStream
	{
		public static void main(String[]args)
		{
			//数据源。
			ByteArayinputStreamb  is = new  ByteArrayInputStream (“ABCDEFD”, get Bytea());
			// 数据目的
			ByteArrayOutputStream bos  = new  ByteArrayOutputStream ();
			int by = 0;
			while ((by = bis.read())!- 1)
			{
				bos.write(by);
			}
			System.out.println(bos.size());
			System.out.println(bos.toString());
		}
	}
字符编码


·字符流的出现为了方便 *** 作字符。
·更重要是的加入了编码转换。
·通过子类转换流来完成:
InputStreamReader
OutputStreamWriter  
·在两个对象进行构造的时候可以加入字符集。

 编码:字符串变成字节数组。
解码:字节数组变成字符串。

String-- > byte[]; str.getBytes(charsetName );
    bytel]-- > String:new String(byte[], charsetName );

import java.util.*;
	{
		class  EncodeDemo
		{
			public static void main(String[] args) throws Exception
			{
				string s = “你好”;

				byte[] b1 = s.ɡetBytes("GBK");

				System.out.println(Arrays.toString(b1));
				Strings1 = new String(b1, "ISO8859-1");
				system.out.println("sì=" + s1);

				//对s1进行iso859-1编码。
				byte[]b2 = s1.getBytes("iso9859−1");
				System.out.printIn(Arrays.toString(b2));

				Strings2 = new String(b2, "gb/:");

				system.out.println("s2="ts2);

			}
		}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717666.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存