JAVA基础--IO流
数据流的方向: 输入流, 输出流
数据单位: 字节流, 字符流
功能不同: 节点流, 处理流
JDK提供的4种抽象流:
输入流: 字节流(InputStream), 字符流(Reader) , 站在程序角度判断是输入还是输出.
输出流: 字节流(OutputStream),字符流(Writer).
字节流: 010101.... 字符流: 以字符读取.
java里1个字符=2个字节
节点流: 程序从一个特定数据源读写数据
处理流: 连接在已经存在的流(节点流或者处理流)
InputStream/OutputStream
Reader/Writer
FileInputStream/FileOutputStream
FileReader/FileWriter
BufferedInputStream/BufferedOutputStream
BufferedReader/BufferedWriter
ByteArrayInputStream/ByteArrayOutputStream
InputStreamReader/OutputStreamWriter
DataInputStream/DataOutputStream
PrintStream/PrintWriter
ObjectInputStream/ObjectOutputStream
InputStream, 向程序输入数据, 数据的单位为字节 8bit, 用read方法往里读, 每次读一个字节(0~255之间的数). 返回实际读取的字节数. 如果返回-1, 代表到了结尾.
close()用于关闭流, skip()是跳过几个字节不读.
OutputStream, 从程序中往外写数据. write()方法往外写.
Reader向程序输入数据, 数据单位是字符(16bit)
Writer从程序输出数据, 数据单位是字符(16bit)
void write(String str), String有一个toCharArray()方法用于转换成字符数组.
节点流类型:
类型 字符流 字节流
File FileReader FileInputStream
FileWriter FileOutputStream
Memory Array CharArrayReader ByteArrayInputStream
CharArrayWriter ByteArrayOutputStream
Memory String StringReader
StringWriter
Pipe PipedReader PipedInputStream
PipedWriter PipedOutputStream
FileInputStream:字节输入流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.io.*; public class TestFileInputStream { public static void main(String[] args) { int b = 0 ; FileInputStream in = null ; try { in = new FileInputStream( "d:\\share\\java\\io\\TestFileInputStream.java" ); } catch (FileNotFoundException e) { System.out.println( "找不到指定文件" ); System.exit(- 1 ); } try { long num = 0 ; while ((b=in.read())!=- 1 ){ System.out.print(( char )b); num++; } in.close(); System.out.println(); System.out.println( "共读取了 " +num+ " 个字节" ); } catch (IOException e1) { System.out.println( "文件读取错误" ); System.exit(- 1 ); } } } |
FileOutputStream: 字节输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.*; public class TestFileOutputStream { public static void main(String[] args) { int b = 0 ; FileInputStream in = null ; FileOutputStream out = null ; try { in = new FileInputStream( "d:/share/java/HelloWorld.java" ); out = new FileOutputStream( "d:/share/java/io/HW.java" ); while ((b=in.read())!=- 1 ){ out.write(b); } in.close(); out.close(); } catch (FileNotFoundException e2) { System.out.println( "找不到指定文件" ); System.exit(- 1 ); } catch (IOException e1) { System.out.println( "文件复制错误" ); System.exit(- 1 ); } System.out.println( "文件已复制" ); } } |
FileReader: 字符输入流:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.*; public class TestFileReader { public static void main(String[] args) { FileReader fr = null ; int c = 0 ; try { fr = new FileReader( "d:\\share\\java\\io\\TestFileReader.java" ); int ln = 0 ; while ((c = fr.read()) != - 1 ) { //char ch = (char) fr.read(); System.out.print(( char )c); //if (++ln >= 100) { System.out.println(); ln = 0;} } fr.close(); } catch (FileNotFoundException e) { System.out.println( "找不到指定文件" ); } catch (IOException e) { System.out.println( "文件读取错误" ); } } } |
FileWriter: 字符输出流:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.*; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null ; try { fw = new FileWriter( "d:\\bak\\unicode.dat" ); for ( int c= 0 ;c<= 50000 ;c++){ fw.write(c); } fw.close(); } catch (IOException e1) { e1.printStackTrace(); System.out.println( "文件写入错误" ); System.exit(- 1 ); } } } |
用FileReader和FileWriter将一个文件copy到另一个文件里:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.io.*; public class TestFileWriter2 { public static void main(String[] args) throws Exception { FileReader fr = new FileReader( "d:/java/io/TestFileWriter2.java" ); FileWriter fw = new FileWriter( "d:/java/io/TestFileWriter2.bak" ); int b; while ((b = fr.read()) != - 1 ) { fw.write(b); } fr.close(); fw.close(); } } |
处理流: BufferedInputStream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.io.*; public class TestBufferStream1 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream( "d:\\share\\java\\HelloWorld.java" ); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0 ; System.out.println(bis.read()); System.out.println(bis.read()); bis.mark( 100 ); for ( int i= 0 ;i<= 10 && (c=bis.read())!=- 1 ;i++){ System.out.print(( char )c+ " " ); } System.out.println(); bis.reset(); for ( int i= 0 ;i<= 10 && (c=bis.read())!=- 1 ;i++){ System.out.print(( char )c+ " " ); } bis.close(); } catch (IOException e) {e.printStackTrace();} } } |
处理流: BufferedWriter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.*; public class TestBufferStream2 { public static void main(String[] args) { try { BufferedWriter bw = new BufferedWriter( new FileWriter( "d:\\share\\java\\dat2.txt" )); BufferedReader br = new BufferedReader( new FileReader( "d:\\share\\java\\dat2.txt" )); String s = null ; for ( int i= 1 ;i<= 100 ;i++){ s = String.valueOf(Math.random()); bw.write(s); bw.newLine(); } bw.flush(); while ((s=br.readLine())!= null ){ System.out.println(s); } bw.close(); br.close(); } catch (IOException e) { e.printStackTrace();} } } |
转换流: 字节流->字符流, InputStreamReader, OutputStreamWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.*; public class TestTransForm1 { public static void main(String[] args) { try { OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( "F:\\mashibing\\01第一部分J2SE\\java\\io\\char.txt" )); osw.write( "mircosoftibmsunapplehp" ); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter( new FileOutputStream( "F:\\mashibing\\01第一部分J2SE\\java\\io\\char.txt" , true ), "ISO8859_1" ); // latin-1 继续写, 去掉true, 擦掉原来的再写 osw.write( "mircosoftibmsunapplehp" ); System.out.println(osw.getEncoding()); osw.close(); } catch (IOException e) { e.printStackTrace(); } } } |
典型用法: InputStreamReader转换字节流到字符流后再处理流BufferedReader.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.io.*; public class TestTransForm2 { public static void main(String args[]) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = null ; try { s = br.readLine(); while (s!= null ){ if (s.equalsIgnoreCase( "exit" )) break ; System.out.println(s.toUpperCase()); s = br.readLine(); } br.close(); } catch (IOException e) { e.printStackTrace(); } } } //阻塞 |
数据流: 如何把long类型的数写到文件里?
ByteArrayOutputStream-->DataOutputStream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.io.*; public class TestDataStream { 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()); System.out.println(bais.available()); //有多少个字节? 9 =double:8个, boolean:1个 DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dos.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } } } |
Print流: PrintWriter:字符输出流 PrintStream :字节输出流, 这两个都不会抛出异常.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.io.*; public class TestPrintStream1 { public static void main(String[] args) { PrintStream ps = null ; try { FileOutputStream fos = new FileOutputStream( "d:\\bak\\log.dat" ); ps = new PrintStream(fos); } catch (IOException e) { e.printStackTrace(); } if (ps != null ){ System.setOut(ps); //输出到控制台更改到输出到文件 } int ln = 0 ; for ( char c = 0 ; c <= 60000 ; c++){ System.out.print(c+ " " ); if (ln++ >= 100 ){ System.out.println(); ln = 0 ;} } } } |
把一个文件读出来.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 f,PrintStream fs){ try { BufferedReader br = new BufferedReader( new FileReader(f)); String s = null ; while ((s=br.readLine())!= null ){ fs.println(s); } br.close(); } catch (IOException e) { fs.println( "无法读取文件" ); } } } |
日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.*; import java.io.*; public class TestPrintStream3 { public static void main(String[] args) { String s = null ; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); try { FileWriter fw = new FileWriter( "d:\\bak\\logfile.log" , true ); //Log4J PrintWriter log = new PrintWriter(fw); while ((s = br.readLine())!= null ) { if (s.equalsIgnoreCase( "exit" )) break ; System.out.println(s.toUpperCase()); log.println( "-----" ); log.println(s.toUpperCase()); log.flush(); } log.println( "===" + new Date()+ "===" ); log.flush(); log.close(); } catch (IOException e) { e.printStackTrace(); } } } |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 | ----- NIHAO ----- NIHAO ===Wed Apr 06 18 : 20 : 02 CST 2016 === ----- HAHA ----- NIZAONA ----- WO XIANG NI LE ===Wed Apr 06 18 : 20 : 44 CST 2016 === |
Object流:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.io.*; public class TestObjectIO { public static void main(String args[]) throws Exception { T t = new T(); t.k = 8 ; FileOutputStream fos = new FileOutputStream( "d:/bak/testobjectio.dat" ); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(t); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream( "d:/bak/testobjectio.dat" ); ObjectInputStream ois = new ObjectInputStream(fis); T tReaded = (T)ois.readObject(); System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k); } } class T implements Serializable //把object直接转换成字节流写到硬盘上, { int i = 10 ; int j = 9 ; double d = 2.3 ; transient int k = 15 ; //写的时候不考虑, 所以默认0 } |
结果:
1 | 10 9 2.3 0 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步