/** * 1.1 读取硬盘上的一个文本文件 * * @throws IOException */ public static void main(String[] args) throws IOException { // FileInputStream FileInputStream fis = new FileInputStream("E:/lalalal.txt"); // 1.定义一个缓冲区 byte[] byte[] bytes = new byte[1024];// 1k 1.5k // int data; while ((data = fis.read(bytes)) != -1) { // 有数据可读 // byte[] String String temp = new String(bytes, 0, data); System.out.println(temp); } fis.close(); }
/** * 1.字符流读取文件 * * @throws IOException */ public static void main(String[] args) throws IOException { FileReader reader = new FileReader("E:/lalala.txt"); char[] chars = new char[1024]; int data; while ((data = reader.read(chars)) != -1) { String temp = new String(chars, 0, data); System.out.println(temp); } reader.close(); }
/** * 1.1 带缓冲区 的读取 BufferedReader * * @throws IOException */ public static void main(String[] args) throws IOException { Reader reader = new FileReader("E:/lalala.txt"); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); reader.close(); }
/** * 二进制读取 DataInputStream * * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub InputStream is = new FileInputStream("D:/啦啦啦.png"); DataInputStream dis = new DataInputStream(is); OutputStream os = new FileOutputStream("E:/啦啦啦.png"); DataOutputStream dos = new DataOutputStream(os); byte[] bytes = new byte[1024]; int data; while ((data = dis.read(bytes)) != -1) { dos.write(bytes, 0, data); } dos.close(); os.close(); dis.close(); is.close(); System.out.println("OK"); }