标签:style blog java color os 数据
字符流:
FileReader
FileWriter
BufferedReader
BufferedWriter
-------------------------
字节流:
InpuStream
OutputStream
想要操作图片数据,这时就要用到字节流。
import java.io.*; class FileStream { public static void main(String[] args) throws Exception { //writeFile(); readFile3(); } public static void writeFile()throws Exception{ FileOutputStream fos = new FileOutputStream("fos.txt"); fos.write("abcde".getBytes()); fos.close(); } public static void readFile1()throws Exception{ FileInputStream fis = new FileInputStream("fos.txt"); int ch = 0; while((ch = fis.read()) != -1){ System.out.println((char)ch); } fis.close(); } public static void readFile2()throws Exception{ FileInputStream fis = new FileInputStream("fos.txt"); byte[] buf = new byte[1024]; int len = 0; while((len = fis.read(buf)) != -1){ System.out.println(new String(buf, 0, len)); } fis.close(); } //available() //缺点:如果内容过大,容易占用内存,还是以第2种方法为主。 public static void readFile3()throws Exception{ FileInputStream fis = new FileInputStream("fos.txt"); int num = fis.available(); byte[] buf = new byte[num];//定义了一个刚刚好的缓冲区,不用再循环了 fis.read(buf); System.out.println(new String(buf)); fis.close(); } }
标签:style blog java color os 数据
原文地址:http://www.cnblogs.com/nophy/p/3862190.html