[javase]IO
2016-04-28 21:29
537 查看
一、流
java流分为两种,一种字节流,另一种是字符流,分别由四个抽象类表示:InputStream, OutputStream, Reader, Writer
1、输出流
FileOutputStream使用的2种方法
FilterOutputStream之BufferedOutputStream
Writer之BufferedWriter
Writer之OutputStreamWriter
1、输人流
InputStreamReader
读/写文件
java流分为两种,一种字节流,另一种是字符流,分别由四个抽象类表示:InputStream, OutputStream, Reader, Writer
注:在网络程序设计中到底是使用字符流还是字节流呢? 答案:有一个简单的判断方法:如果通讯的双方都是java语言编写,最好使用字符流或更高级的流类。如果通讯的另一端是未知的,最好使用字节流,并事先约定好通讯规则发送和接收数据
1、输出流
FileOutputStream使用的2种方法
//1.创建文件 File file = new File("d:\\test.txt"); FileOutputStream fos = new FileOutputStream(file); // FileOutputStream fos = new FileOutputStream("d:\\test.txt", true); //2.写入内容 byte[] b = "xiaoming a".getBytes(); fos.write(b); //3.销毁流 fos.close();ByteArrayOutputStream使用方法
public void byteArrayOutputStream() throws IOException { int a = 0; int b = 1; int c = 2; ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(a); bout.write(b); bout.write(c); byte[] buff = bout.toByteArray(); for (int i = 0; i < buff.length; i++) { System.out.println(buff[i]); } System.out.println("***********************"); ByteArrayInputStream bin = new ByteArrayInputStream(buff); while ((b = bin.read()) != -1) { System.out.println(b); } }
FilterOutputStream之BufferedOutputStream
@Test public void filterOutputTest() { FileOutputStream fos = null; BufferedOutputStream bos = null; try { fos = new FileOutputStream("io.txt"); //1 BufferedOutputStream //如果第二参数<=b.length+b1.length+b2.length或没有第二参数,缓存空间不够,会写入部分内容并bos.flush() //否则b,b1,b2内容会缓存再bos里,等待bos.close或bos.flush //如果bos.close()或者bos.flush(),会立即吸入io.txt bos = new BufferedOutputStream(fos,16); byte[] b = "小明".getBytes(); byte[] b1 = "大红".getBytes(); byte[] b2 = "小刚".getBytes(); bos.write(b); bos.write(b1); bos.write(b2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bos != null) { bos.flush(); bos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
Writer之BufferedWriter
@Test public void bufferedWriterTest() throws IOException { Writer w = null; BufferedWriter bw = null; try { w = new FileWriter("io.txt"); bw = new BufferedWriter(w); bw.write("sdsdafdasdfads"); bw.newLine(); bw.write("小明啊.............."); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (bw != null) { bw.flush(); bw.close(); } } }
Writer之OutputStreamWriter
@Test public void outputStreamWriterTest() throws IOException { OutputStreamWriter osw = new OutputStreamWriter(System.err); BufferedWriter out = new BufferedWriter(osw); out.append("hello world").append("! 你好").append(", world"); System.out.println(osw.getEncoding()); out.flush(); }
1、输人流
InputStreamReader
读/写文件
//读/写文件 public void inputStreamReaderTest() throws Exception { InputStreamReader isr = new InputStreamReader(new FileInputStream("d://catalina.out.2016-04-12-20.06.out")); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d://co.out")); int i; while ((i = isr.read()) != -1) { osw.write(i); } osw.close(); isr.close(); }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树