------------- android培训、java培训、期待与您交流! -------------
----------------------------------------IO流----------------------------------------
IO流:按读、写方式分:
1.字节流:
1).输出流:OutputStream(抽象类):(三个写的方法)
|--FileOutputStream(基本流):(1.输出一个字节,一个字节数组,一个字节数组的一部分>)
|--ByteArrayOutputStream:向缓存区输出一个byte[]数组;
|--ObjectOutputStream(序列化流):
|--FilterOutputStream(没学)
|--BufferedOutputStream(类--缓冲流)
|--DataOutputStream:能够写入Java的基本数据类型;
|--PrintStream(字节打印流):
2).输入流:InputStream(抽象类):
|--FileInputStream(基本流):(两个读取的方法<1.读取一个字节;2.读取一个字节数组>)
|--ByteArrayInputStream:从缓存区读取byte[]数组的内容;
|--ObjectInputStream(反序列化流):
|--FilterInputStream(没学)
|--BufferedInputStream(类--缓冲流)
|--DataInputStream:能够读取Java的基本数据类型:
2.字符流:
1).输出流:Writer(抽象类):
(五个写方法<1.输出一个字符,一个字符数组,一个字符数组的一部分,一个字符串,一个字符串的一部分>)
|--OutputStreamWriter(转换流):可以将一个"字符流",转换为"字节流":
|--FileWriter(类--字符流)
|--BufferedWriter:(缓冲流)
|--PrintWriter:(字符打印流)
2).输入流:Reader(抽象类):(两个读取的方法<1.读取一个字符;2.读取一个字符数组>)
|--InputStreamReader(转换流):可以将一个"字节流",转换为"字符流":
|--FileReader(类--字符流)
|--BufferedReader:(缓冲流)
-------------------------------- FileOutputStream------------------------------------
1.输出流:FileOutputStream:
构造方法:文件可以不存在,会自动创建一个新的;
FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
追加写入:
FileOutputStream(String name, boolean append):创建一个向具有指定 name 的文件中写入数据的输出文件流。
FileOutputStream(File file, boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
输出数据:
public void write(int b):输出一个字节;(b作为编码,输出的是对应的"字符")
public void write(byte[] b):输出一个字节数组
public void write(byte[] b,int off,int len):输出一个字节数组的一部分;
关闭流:
close();
2.输入流:FileInputStream(类)
构造方法:一定要确保文件存在,否则抛出异常;
FileInputStream(File file):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的File对象file指定。
FileInputStream(String name):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名name指定。
读取的方法:
public int read():读取一个字节;
public int read(byte[] b):读取一个字节数组。
工作方式:
1.如果文件长度够的话,会尽量的填充满byte[]数组b;
2.返回值:本次读取的字节数;
复制文本文件(步骤):
1.读取:输入流:FileInputStream;
2.一次读取一个字节(或者一次读取一个字节数组)
3.写入:输出流:FileOutputStreasm:
4.一次写入一个字节(或者一次写入一个字节数组)
5.释放资源
public class Demo { public static void main(String[] args) { try { // 1.String的构造 FileOutputStream out = new FileOutputStream("Demo06_File.txt"); // 2.File的构造 /* * File file = new File("C:\\test\\test2.txt"); FileOutputStream * out2 = new FileOutputStream(file); */ // 3.输出数据 // 1).write(int n):输出一个字节 out.write(97); // out.write(98); // 2).write(byte[] byteArray):输出一个字节数组 /* * byte[] byteArray = "abcdFFFFFF".getBytes(); out.write(byteArray); * * //3).write(byte[] b,int off,int len):输出一个字节数组的一部分 byte[] * byteArray2 = "你好5588Java".getBytes(); //输出"5588" * out.write(byteArray2,4,4); */ // 3.释放资源:大家以后要养成习惯,用完后,一定要close()关闭流; out.close(); // 模拟程序正在运行 /* * while(true){ * * } */ } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
IO流_字节流_输出流_输出换行和追加写入:
public class Demo { public static void main(String[] args) { try { // 1.实例化一个输出流对象 FileOutputStream out = new FileOutputStream("demo07_File.txt", true); // 2.输出数据 out.write("第一行".getBytes()); out.write("\r\n".getBytes());// 输出一个换行 out.write("第二行".getBytes()); out.write("\r\n".getBytes()); // 3.释放资源 out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
IO流_字节流_输出流_加入异常处理:
public class Demo { public static void main(String[] args) { String file = "demo08_File.txt"; try { writeToFile(file); } catch (IOException e) { e.printStackTrace(); } FileOutputStream out = null; try { out = new FileOutputStream(file); out.write("你好".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void writeToFile(String fileName) throws IOException { FileOutputStream out = new FileOutputStream(fileName); out.write("第一行".getBytes()); out.write("\r\n".getBytes()); out.write("第二行\r\n".getBytes()); out.close(); } }
public class Demo { public static void main(String[] args) { try { File file = new File("demo09_File.txt"); if (!file.exists()) { file.createNewFile(); } FileInputStream in = new FileInputStream("demo09_File.txt"); // FileInputStream in2 = new FileInputStream(file); // 一次读取一个字节 /* * int n = in.read(); while(n != -1){ System.out.println("读取的字节为:" + * n); System.out.println("将字节转换为字符:" + (char)n); n = in.read(); } */ // 常用的写法 /* * int n = 0; while((n = in.read()) != -1){ * System.out.println("将字节转换为字符:" + (char)n); } */ // 一次读取一个字节数组 byte[] byteArray = new byte[3]; int n = 0; while ((n = in.read(byteArray)) != -1) { /* * System.out.println("n = " + n); * System.out.println("读取的字节数组中的内容:" + * Arrays.toString(byteArray)); System.out.println("读取的字符:"); * for(byte b : byteArray){ System.out.print((char)b + " "); } * System.out.println(); */ // 怎样将一个字节数组,转换为一个String:使用String的构造方法 String str = new String(byteArray, 0, n); System.out.println("从0开始,取:" + n + " 个长度:"); System.out.println("读取的内容为:" + str); } in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
复制图片文件:
1. 一次读写一个字节
2.一次读写一个字节数组;
public class Demo { public static void main(String[] args) throws IOException { // 1.输入流 FileInputStream in = new FileInputStream("C:\\aaa\\哥有老婆.mp4"); // 2.输出流 FileOutputStream out = new FileOutputStream("demo11_哥有老婆_copy2.mp4"); // 3.一次读写一个字节数组 /* * int n = 0; while((n = in.read()) != -1){ out.write(n); } */ int len = 0; byte[] byteArray = new byte[1024]; while ((len = in.read(byteArray)) != -1) { out.write(byteArray, 0, len); } // 4.释放资源 out.close(); in.close(); System.out.println("复制完毕"); } }
带缓冲的字节流:
public class Demo { public static void main(String[] args) throws IOException { // 构造一个带缓冲的输出流 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream("demo12_File.txt")); out.write("你好哇".getBytes()); out.write("\r\n".getBytes()); out.write("Java,HelloWorld".getBytes()); // out.flush(); out.close();// flush() + close(); // 构造一个带缓冲的输入流: BufferedInputStream in = new BufferedInputStream(new FileInputStream( "demo12_File.txt")); // 一次读取一个字节 /* * int n = 0; while((n = in.read()) != -1){ System.out.println((char)n); * } */ // 一次读一个字节数组 int n = 0; byte[] byteArray = new byte[1024]; while ((n = in.read(byteArray)) != -1) { String str = new String(byteArray, 0, n); System.out.println("读取的内容:" + str); } in.close(); } }
转换流_OutputStreamWriter:
public class Demo { public static void main(String[] args) throws IOException { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream( "demo15_File.txt")); // out.write(97); char[] charArray = { 'a', 'b', '你', '好' }; out.write(charArray); out.write(charArray, 2, 2); out.write("\r\n终于可以输出一个字符串了\r\n"); String str = "我爱Java,我爱祖国!"; out.write(str, 7, 4); out.close(); } }
转换流_InputStreamReader:
public class Demo { public static void main(String[] args) throws IOException { InputStreamReader in = new InputStreamReader(new FileInputStream( "demo16_File.txt")); // 一次读取一个字符 /* * int n = 0; while((n = in.read()) != -1){ * System.out.println("读取的int值:" + n); System.out.println("读取的字符:" + * (char)n); } */ // 一次读取一个字符数组 int n = 0; char[] charArray = new char[6]; while ((n = in.read(charArray)) != -1) { String str = new String(charArray, 0, n); System.out.println("读取的内容:" + str); } in.close(); } }
字符流_FileWriter_FileReader:
public class Demo { public static void main(String[] args) throws IOException { FileReader in = new FileReader("demo16_File.txt"); FileWriter out = new FileWriter("demo17_File_copy_2.txt"); // 一次读写一个字符 /* * int n = 0; while((n = in.read()) != -1){ out.write(n); } */ // 一次读写一个字符数组 int n = 0; char[] charArray = new char[1024]; while ((n = in.read(charArray)) != -1) { out.write(charArray, 0, n); } // 释放资源 out.close(); in.close(); } }
字符流_缓冲流_BufferedWriter_BufferedReader:
public class Demo { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader( "demo17_File_copy.txt")); String s = null; while ((s = in.readLine()) != null) {//字符缓冲流可以一行一行的读取 System.out.println(s); } } }
字符流_读写文件的五种方式:
public class Demo { public static void main(String[] args) throws IOException { // method1(); // method2(); // method3(); // method4(); method5(); System.out.println("复制完毕!"); } // 基本字符流,一次读写一个字符 private static void method1() throws IOException { FileReader in = new FileReader("demo10_File.txt"); FileWriter out = new FileWriter("demo19_method1.txt"); // 一次读写一个字符 int n = 0; while ((n = in.read()) != -1) { out.write(n); } in.close(); out.close(); } // 基本字符流,一次读写一个字符数组 private static void method2() throws IOException { FileReader in = new FileReader("demo10_File.txt"); FileWriter out = new FileWriter("demo19_method2.txt"); // 一次读写一个字符 int n = 0; char[] charArray = new char[1024]; while ((n = in.read(charArray)) != -1) { out.write(charArray, 0, n); } in.close(); out.close(); } // 缓冲流,一次读写一个字符 private static void method3() throws IOException { BufferedReader in = new BufferedReader( new FileReader("demo10_File.txt")); BufferedWriter out = new BufferedWriter(new FileWriter( "demo19_method3.txt")); // 一次读写一个字符 int n = 0; while ((n = in.read()) != -1) { out.write(n); } in.close(); out.close(); } // 缓冲流,一次读写一个字符数组 private static void method4() throws IOException { BufferedReader in = new BufferedReader( new FileReader("demo10_File.txt")); BufferedWriter out = new BufferedWriter(new FileWriter( "demo19_method4.txt")); // 一次读写一个字符 int n = 0; char[] charArray = new char[1024]; while ((n = in.read(charArray)) != -1) { out.write(charArray, 0, n); } in.close(); out.close(); } // 缓冲流,一次读写一行 private static void method5() throws IOException { BufferedReader in = new BufferedReader( new FileReader("demo10_File.txt")); BufferedWriter out = new BufferedWriter(new FileWriter( "demo19_method5.txt")); String row = null; while ((row = in.readLine()) != null) { out.write(row); out.newLine(); } in.close(); out.close(); } }
数据操作流_DataInputStream_DataOutputStream:能读写取基本数据类型
public class Demo { public static void main(String[] args) throws IOException { write(); read(); } public static void write() throws IOException{ DataOutputStream out = new DataOutputStream(new FileOutputStream("demo01.txt")); out.writeByte(127); out.writeShort(128); out.writeChar(97); out.writeInt(2000); out.writeLong(8000); out.writeFloat(3.0F); out.writeDouble(4.0); out.close(); System.out.println("写入完毕!"); } public static void read() throws IOException{ DataInputStream in = new DataInputStream(new FileInputStream("demo01.txt")); byte v1 = in.readByte(); short v2 = in.readShort(); char v3 = in.readChar(); // int v4 = in.read();//虽然read()方法也返回int,但不要用这个,这个是父类中读取的方法; int v4 = in.readInt(); long v5 = in.readLong(); float v6 = in.readFloat(); double v7 = in.readDouble(); in.close(); System.out.println("v1 = " + v1); System.out.println("v2 = " + v2); System.out.println("v3 = " + v3); System.out.println("v4 = " + v4); System.out.println("v5 = " + v5); System.out.println("v6 = " + v6); System.out.println("v7 = " + v7); } }
字节缓冲流_ByteArrayOutputStream_ByteArrayInputStream:
public class Demo { public static void main(String[] args) throws IOException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); FileInputStream in = new FileInputStream("demo02.txt"); // 一次读取一个字节数组 int n = 0; byte[] byteArray = new byte[3]; while ((n = in.read(byteArray)) != -1) { // 以前我们这里要么转换为字符串输出;要么写入到另一个文件 // 现在,我们什么都不做,先将已读取的字节数组缓存起来 byteOut.write(byteArray, 0, n); } in.close(); // 从缓冲区读取byte[]数组 ByteArrayInputStream byteIn = new ByteArrayInputStream( byteOut.toByteArray()); // 一次读取一个字节,或者一个字节数组,或者直接转换为一个字符串 String str = new String(byteOut.toByteArray()); System.out.println(str); // 一次读取一个字节,写入到另一个文件 FileOutputStream fileOut = new FileOutputStream("demo02_copy.txt"); n = 0; while ((n = byteIn.read()) != -1) { fileOut.write(n); } fileOut.close(); System.out.println("写入完毕!"); } }
打印流复制文本文件:
public class Demo { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("demo05.txt")); PrintWriter out = new PrintWriter(new FileWriter("demo05_copy.txt"),true); //一次读取一行数据 String row = null; while(( row = in.readLine()) != null){ out.println(row);//write() + newLine() + flush() } out.close(); in.close(); } }
随机访问流_RandomAccessFile类:
public class Demo { public static void main(String[] args) throws IOException { // write(); read(); } private static void read() throws IOException { RandomAccessFile in = new RandomAccessFile("demo07.txt", "r"); System.out.println("文件指针位置:" + in.getFilePointer()); int v1 = in.readInt(); System.out.println("读取int后,指针位置:" + in.getFilePointer()); boolean v2 = in.readBoolean(); System.out.println("读取boolean后,指针位置:" + in.getFilePointer()); short v3 = in.readShort(); System.out.println("读取short后,指针位置:" + in.getFilePointer()); String v4 = in.readUTF(); System.out.println("读取UTF后,指针位置:" + in.getFilePointer()); // 将指针移到boolean位置,重新读取boolean值 in.seek(4); boolean v5 = in.readBoolean(); in.close(); System.out.println("v1 = " + v1); System.out.println("v2 = " + v2); System.out.println("v3 = " + v3); System.out.println("v4 = " + v4); System.out.println("v5 = " + v5); } private static void write() throws IOException { RandomAccessFile out = new RandomAccessFile("demo07.txt", "rw"); out.writeInt(120); out.writeBoolean(true); out.writeShort(20); out.writeUTF("你好"); out.close(); System.out.println("写入完毕!"); } }
序列化流_ObjectOutputStream_ObjectInputStream:
public class Student implements Serializable { private static final long serialVersionUID = 1L; String name; transient int age;// 不需要被序列化 char sex; String address; }
public class Demo { public static void main(String[] args) throws IOException, ClassNotFoundException { Student stu = new Student(); stu.name = "刘德华"; stu.age = 20; // 构造一个"序列化"流 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( "demo08.txt")); out.writeObject(stu); out.close(); // 反序列化 ObjectInputStream in = new ObjectInputStream(new FileInputStream( "demo08.txt")); Object obj = in.readObject();// 从文件读取对象信息,并在内存中创建这个对象空间 System.out.println("obj = " + obj); Student stu2 = (Student) obj; System.out.println(stu2.name + "," + stu2.age); // System.out.println("stu == stu2 : " + (stu == stu2));//false System.out.println("程序结束!"); } }
Properties类_作为Map的基本功能:
public class Demo { public static void main(String[] args) { Properties pro = new Properties(); /* * pro.put("it001", "张三"); pro.put("it002", "李四"); pro.put("it003", * "王五"); * * Set keySet = pro.keySet(); for(Object obj : keySet){ * System.out.println(obj + "--" + pro.get(obj)); } */ // 特有方法测试 pro.setProperty("it001", "张三");// --->put() pro.setProperty("it002", "李四"); pro.setProperty("it003", "王五"); // System.out.println(pro.getProperty("it001"));//--->get() // 遍历 Set<String> keys = pro.stringPropertyNames(); for (String key : keys) { System.out.println(key + "--" + pro.getProperty(key)); } } }
Properties类_操作配置文件:
public class Demo { public static void main(String[] args) throws IOException { Properties pro = new Properties(); // **************写入数据**************// // 1.填充集合 /* * pro.setProperty("金币", "200000"); pro.setProperty("钻石", "1000"); * pro.setProperty("疲劳", "200"); pro.setProperty("活跃度", "100"); * //2.准备一个字符流(输出流) BufferedWriter out = new BufferedWriter(new * FileWriter("game.properties")); * //3.调用Properties的store()方法,将集合中的数据写入到配置文件 pro.store(out, "游戏配置文件"); * out.close(); System.out.println("写入完毕"); */ // *************读取数据*******************// Properties inPro = new Properties(); // 1.准备一个输入流 FileReader fileIn = new FileReader("game.properties"); // 2.调用load()方法读取配置文件信息 inPro.load(fileIn); // 3.关闭文件流 fileIn.close(); // 4.遍历集合 Set<String> keys = inPro.stringPropertyNames(); for (String key : keys) { System.out.println(key + "---" + inPro.getProperty(key)); } } }