IO学习笔记
来源:互联网 发布:简单的java程序代码 编辑:程序博客网 时间:2024/05/22 06:31
流式输入输出图:
java.io包中定义了多个流类型的来实现输入输出功能,可以从不同的角度进行分类
1:按数据流的方向不同可以分为输入流和输出流(都是站在程序的角度上来说的,,如读文件写到程序中是输入流,写文件则是输出流)
2:按处理数据单位不同可以分为字节流和字符流
3:按照功能的不同可以分为节点流和处理流
J2SDK 所提供的所有的流类型都位于包java.io内
节点流和处理流
节点流:原始的管道,没有加任何处理的
处理流:加上了对数据的处理的管道:
InputStream:继承自InputStream的流都是用于向程序中输入数据,都是以字节的形式来写数据,且数据的单位为字节(8bit)下图深色为节点流,浅色为处理流
InputStream抽象类的方法
import java.io.*;public class TestFileInputStream{public static void main(String[] args){FileInputStream in = null;int b =0;try{in = new FileInputStream("D:/javaCode/array/DateSort.java");int count = 0; while ((b=in.read())!=-1) { System.out.print((char)b); count++; } System.out.println(count);}catch(IOException e){System.out.println("文件读取错误");System.out.println(e);System.exit(-1);}}}
int read() throws IOException //读取一个字节并以整数的形式返回,如果返回-1则已到输入流的末尾
int read( byte[] buffer) throws IOException; //读取序列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已到了输入流的末尾返回-1
outputStream:继承该类的流是用于向程序中输出数据,且数据的单位为字节,深色的为节点流,浅色的为处理流
outputStream的方法
//复制文件import java.io.*;public class TestFileOutputStream{public static void main(String[] args){FileInputStream in = null;FileOutputStream out = null;int b = 0;try{in = new FileInputStream("D:/javaCode/io/TestFileInputStream.java");out = new FileOutputStream("D:/javaCode/io/test.java");}catch (FileNotFoundException e){System.out.println("没有找到指定文件");}//文件复制try{while ((b=in.read())!=-1){out.write(b);}in.close();out.close();}catch (IOException e){System.out.println("文件出错");}}}
void write(int b) throws IOException
void write(byte[] b) throws IOException
void write(byte[] b,int off,int len) throws IOException//将一个字节类型的数组从指定的位置off开始的将len个字节写入到输出流
void close() throws IOException;//关闭流释放内存资源
void flush() throws IOException//将输出流中缓冲的数据全部写到目的地
Reader:j继承Reader的流都是用于向程序中输入数据,且数据的处理单位是字符(16bite);深色的是节点流,浅色的是处理流
Reader的基本方法
import java.io.*;public class TestFileRead{public static void main(String[] args){FileReader read = null;try{read = new FileReader("D:/javaCode/array/DateSort.java");int count = 0;while((count=read.read())!=-1){System.out.print((char)count);}read.close();}catch (FileNotFoundException e){System.out.println("文件出错");}catch(IOException e1){System.out.println("文件不存在");}}}
Writer:j继承Writer的流都是用于向程序中输出数据,且数据的处理单位是字符(16bite);深色的是节点流,浅色的是处理流
Writer的基本方法:
import java.io.*;public class TestFileWrite{public static void main(String[] args){FileWriter write = null;try{write = new FileWriter("D:/javaCode/io/test1.java");for(int i = 0;i<500000;i++){write.write((char)i);}write.close();}catch (IOException e){System.out.println("文件出错");}}}
处理流:
import java.io.*;public class TestBufferStream1{public static void main(String[] args){try{FileInputStream in = new FileInputStream("D:/javaCode/io/TestBufferStream1.java");BufferedInputStream bufferedIn = new BufferedInputStream(in);int c =0;System.out.println("****"+(char)bufferedIn.read());System.out.println("&&&*"+(char)bufferedIn.read());bufferedIn.mark(100);for(int i =0; i<10&&(c=bufferedIn.read())!=-1;i++){System.out.println("%%%%"+(char)c);}bufferedIn.reset();for(int i =0;i<10&&(c= bufferedIn.read())!=-1;i++){System.out.println("$$$$$$"+(char)c);}in.close();bufferedIn.close();}catch (FileNotFoundException e ){System.out.println(e);}catch(IOException e2){System.out.println(e2);}}}
import java.io.*;public class TestBufferStream2{public static void main(String[] args){ try {BufferedWriter bwrite = new BufferedWriter(new FileWriter("D:/javaCode/io/data.txt"));BufferedReader breader = new BufferedReader(new FileReader("D:/javaCode/io/data.txt"));String s = null;for(int i =0 ;i<=100;i++){s = String.valueOf(Math.random());bwrite.write(s);bwrite.newLine();} bwrite.flush(); while ((s=breader.readLine())!=null) { System.out.println(s); } bwrite.close(); breader.close(); } catch (FileNotFoundException e) { System.out.println("文件不存在"); }catch(IOException e2){ System.out.println("文件异常"); }}}
import java.io.*;public class TransForm1{public static void main(String[] args){try{ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:/javaCode/io/bat.txt")); osw.write("fdagdsflgfodsjsojhigfi"); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter(new FileOutputStream("D:/javaCode/io/bat.txt",true),"ISO8859_1"); osw.write("fdagdsflgfodsjsojhigfi"); System.out.println(osw.getEncoding()); osw.close();}catch (IOException e){System.out.println(e);}}}
import java.io.*;public class TransForm2{public static void main(String[] args){try{InputStreamReader isr = new InputStreamReader(System.in);BufferedReader in = new BufferedReader(isr);String s = in.readLine();while(s!=null){if(s.equalsIgnoreCase("exit")) break;System.out.println(s.toUpperCase());s = in.readLine();}isr.close();in.close();}catch (IOException e){System.out.println(e);}}}
import java.io.*;public class TestDataInputStream{public static void main(String[] args){ByteArrayOutputStream baos = new ByteArrayOutputStream();DataOutputStream dos = new DataOutputStream(baos);try{dos.writeDouble(Math.random());dos.writeBoolean(true);//开始读该文件的数据ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());DataInputStream dis = new DataInputStream(bais);System.out.println(bais.available());System.out.println(dis.readDouble());System.out.println(dis.readBoolean());bais.close();dis.close();}catch (IOException e){System.out.println(e);}}}
import java.io.*;public class TestPrintStream1{public static void main(String[] args){PrintStream ps = null;try{ FileOutputStream fis = new FileOutputStream("D:/javaCode/io/data.txt");ps = new PrintStream(fis);}catch (IOException e){System.out.println(e);}if(ps!=null){System.setOut(ps);//通过这句话将字符写到文件D:/javaCode/io/bat.txt}int ln = 0;for(char i =0;i<600000;i++){System.out.print(i);if((ln++) >=100) {System.out.println();ln = 0;}}}}
import java.io.*;public class TestPrintStream2{public static void main(String[] args){String fileName = args[0]; if(fileName!=null) list(fileName,System.out);}public static void list(String fileName,PrintStream ps){try{BufferedReader br = new BufferedReader(new FileReader(fileName));String s = null;while((s=br.readLine())!=null){ps.println(s);}br.close();}catch (IOException e){System.out.println(e);}}}
import java.io.*;import java.util.*;public class TestPrintStream3{public static void main(String[] args){BufferedReader br = new BufferedReader(new InputStreamReader(System.in));try{FileWriter fw = new FileWriter("D:/javaCode/io/log4j.txt",true);PrintWriter pw = new PrintWriter(fw);String s = null;while ((s=br.readLine())!=null){if(s.equalsIgnoreCase("Exit")) break;System.out.println(s.toUpperCase());pw.println("-----");pw.println(s.toUpperCase());pw.flush();}pw.print("---"+new Date()+"---");pw.flush();pw.close();}catch (IOException e){System.out.println(e);}}}
import java.io.*;public class TestObjectIo{public static void main(String[] args){T t = new T();t.k=8;try{FileOutputStream fos = new FileOutputStream("D:/javaCode/io/data.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(t);oos.flush();oos.close();FileInputStream fis = new FileInputStream("d:/javaCode/io/data.txt");ObjectInputStream ois = new ObjectInputStream(fis);T tread = (T)ois.readObject();System.out.print(tread.j+" "+ tread.k+" "+tread.i+" "+tread.d);}catch (Exception e){System.out.println(e);}}}class T implements Serializable{int j =29;int i =12; double d =2.3; transient int k =14;//transient关键字是用来修饰属性,若用这个关键字修饰之后表示该属性在序列化时候不会对该属性进行操作,处于透明状态}
- Java IO学习笔记
- Java IO学习笔记
- 学习笔记--IO流
- java IO学习笔记
- IO学习笔记(一)
- IO学习笔记(二)
- IO学习笔记(三)
- Java IO学习笔记
- IO多路复用学习笔记
- Java IO学习笔记
- Java IO学习笔记
- IO流学习笔记
- JAVA IO学习笔记
- IO流学习笔记
- java io 学习笔记
- 学习笔记-IO
- IO学习笔记
- Java学习笔记-IO
- 每天一个学习一个新的php函数(2) fgetcsv()/fgets()
- 2015年05月02日 问题
- leetcode 日经贴,Cpp code -Populating Next Right Pointers in Each Node II
- DDS文件格式
- 第57题 Sum Root to Leaf Numbers
- IO学习笔记
- shader 入门 《顶点着色器 的输出 和 片段着色器的 输入》
- DDS支持的Mipmap Texture/Cubemap Texture/Volume Texture 纹理理解
- Choose the best route
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- kbuild makefile
- Strange Way to Express Integers(中国剩余定理+不互质)
- 时间序列分析基础概念
- 字符串-01. 在字符串中查找指定字符(15)