No_16_0324 Java基础学习第二十三天
文档版本 | 开发工具 | 测试平台 | 工程名字 | 日期 | 作者 | 备注 |
---|---|---|---|---|---|---|
V1.0 | 2016.03.24 | lutianfei | none |
登录注册IO版
- 如下代码仅为UserDaoImpl类文件,其他原码参考
day22_login_regist
工程
public class UserDaoImpl implements UserDao {// 为了保证文件一加载就创建private static File file = new File("user.txt");static {try {file.createNewFile();} catch (IOException e) {System.out.println("创建文件失败");// e.printStackTrace();}}@Overridepublic boolean isLogin(String username, String password) {boolean flag = false;BufferedReader br = null;try {// br = new BufferedReader(new FileReader("user.txt"));br = new BufferedReader(new FileReader(file));String line = null;while ((line = br.readLine()) != null) {// 用户名=密码String[] datas = line.split("=");if (datas[0].equals(username) && datas[1].equals(password)) {flag = true;break;}}} catch (FileNotFoundException e) {System.out.println("用户登录找不到信息所在的文件");// e.printStackTrace();} catch (IOException e) {System.out.println("用户登录失败");// e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {System.out.println("用户登录释放资源失败");// e.printStackTrace();}}}return flag;}@Overridepublic void regist(User user) {/** 为了让注册的数据能够有一定的规则,我就自己定义了一个规则: 用户名=密码*/BufferedWriter bw = null;try {// bw = new BufferedWriter(new FileWriter("user.txt"));// bw = new BufferedWriter(new FileWriter(file));// 为了保证数据是追加写入,必须加truebw = new BufferedWriter(new FileWriter(file, true));bw.write(user.getUsername() + "=" + user.getPassword());bw.newLine();bw.flush();} catch (IOException e) {System.out.println("用户注册失败");// e.printStackTrace();} finally {if (bw != null) {try {bw.close();} catch (IOException e) {System.out.println("用户注册释放资源失败");// e.printStackTrace();}}}}
}
操作基本数据类型的流
- 可以读写基本数据类型的数据
- 数据输入流:DataInputStream
- DataInputStream(InputStream in)
- 数据输出流:DataOutputStream
- DataOutputStream(OutputStream out)
- 数据输入流:DataInputStream
public class DataStreamDemo {public static void main(String[] args) throws IOException {// 写// write();// 读read();}private static void read() throws IOException {// DataInputStream(InputStream in)// 创建数据输入流对象DataInputStream dis = new DataInputStream(new FileInputStream("dos.txt"));// 读数据byte b = dis.readByte();short s = dis.readShort();int i = dis.readInt();long l = dis.readLong();float f = dis.readFloat();double d = dis.readDouble();char c = dis.readChar();boolean bb = dis.readBoolean();// 释放资源dis.close();System.out.println(b);System.out.println(s);System.out.println(i);System.out.println(l);System.out.println(f);System.out.println(d);System.out.println(c);System.out.println(bb);}private static void write() throws IOException {// DataOutputStream(OutputStream out)// 创建数据输出流对象DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt"));// 写数据了dos.writeByte(10);dos.writeShort(100);dos.writeInt(1000);dos.writeLong(10000);dos.writeFloat(12.34F);dos.writeDouble(12.56);dos.writeChar('a');dos.writeBoolean(true);// 释放资源dos.close();}
}
内存操作流
- 内存操作流一般用于处理临时信息,因为临时信息不需要保存,使用后就可以删除。
使用完毕后,不需要close()方法,关闭流。
操作字节数组
- ByteArrayInputStream
- ByteArrayOutputStream
- 操作字符数组
- CharArrayReader
- CharArrayWrite
- 操作字符串
- StringReader
- StringWriter
public class ByteArrayStreamDemo {public static void main(String[] args) throws IOException {// 写数据// ByteArrayOutputStream()ByteArrayOutputStream baos = new ByteArrayOutputStream();// 写数据for (int x = 0; x < 10; x++) {baos.write(("hello" + x).getBytes());}// 释放资源// 通过查看源码我们知道这里什么都没做,所以根本不需要close()// baos.close();// public byte[] toByteArray()byte[] bys = baos.toByteArray();// 读数据// ByteArrayInputStream(byte[] buf)ByteArrayInputStream bais = new ByteArrayInputStream(bys);int by = 0;while ((by = bais.read()) != -1) {System.out.print((char) by);}// bais.close();}
}
流的基本分类
- 基本流:就是能够直接读写文件的
- 高级流:在基本流基础上提供了一些其他的功能
打印流
打印流概述
- 字节流打印流 PrintStream
- 字符打印流 PrintWriter
打印流特点
- 只有写数据的,没有读取数据。只能操作目的地,不能操作数据源。
- 可以操作任意类型的数据。
- 如果启动了自动刷新,能够自动刷新。
- 可以直接操作文本文件的
- 可以操作任意类型的数据
- print()
- println()
- 这个时候不仅仅自动刷新了,还实现了数据的换行。
哪些流对象是可以直接操作文本文件的呢?
- FileInputStream
- FileOutputStream
- FileReader
- FileWriter
- PrintStream
- PrintWriter
具体方法:看API,查流对象的构造方法,如果同时有File类型和String类型的参数,一般来说就是可以直接操作文件的。
基本功能
public class PrintWriterDemo {public static void main(String[] args) throws IOException {// 作为Writer的子类使用PrintWriter pw = new PrintWriter("pw.txt");pw.write("hello");pw.write("world");pw.write("java");pw.close();}
}
- 带自动刷新、换行的高级功能
/** 1:可以操作任意类型的数据。* print()* println()* 2:启动自动刷新* PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);* 还是应该调用println()的方法才可以* 这个时候不仅仅自动刷新了,还实现了数据的换行。* * println()* 其实等价于于:* bw.write();* bw.newLine(); * bw.flush();*/
public class PrintWriterDemo2 {public static void main(String[] args) throws IOException {// 创建打印流对象// PrintWriter pw = new PrintWriter("pw2.txt");PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);// write()是搞不定的,怎么办呢?// 我们就应该看看它的新方法// pw.print(true);// pw.print(100);// pw.print("hello");pw.println("hello");pw.println(true);pw.println(100);pw.close();}
}
- 打印流复制文本文件
/** 需求:DataStreamDemo.java复制到Copy.java中* 数据源:* DataStreamDemo.java -- 读取数据 -- FileReader -- BufferedReader* 目的地:* Copy.java -- 写出数据 -- FileWriter -- BufferedWriter -- PrintWriter*/
public class CopyFileDemo {public static void main(String[] args) throws IOException {// 以前的版本// 封装数据源// BufferedReader br = new BufferedReader(new FileReader(// "DataStreamDemo.java"));// // 封装目的地// BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));//// String line = null;// while ((line = br.readLine()) != null) {// bw.write(line);// bw.newLine();// bw.flush();// }//// bw.close();// br.close();// 打印流的改进版// 封装数据源BufferedReader br = new BufferedReader(new FileReader("DataStreamDemo.java"));// 封装目的地PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);String line = null;while((line=br.readLine())!=null){pw.println(line);}pw.close();br.close();}
}
标准输入输出流(重点掌握)
(1)System类中的两个成员变量:
- public static final InputStream
in
* “标准”输入流* - public static final PrintStream
out
* “标准”输出流* - InputStream is = System.in;
- PrintStream ps = System.out;
- 它们各代表了系统标准的输入和输出设备。
- 默认输入设备是键盘,输出设备是显示器。
- System.in的类型是
InputStream
。 - System.out的类型是
PrintStream
是OutputStream
的子类FilterOutputStream
的子类.
- public static final InputStream
(2)三种键盘录入方式
- A:main方法的args接收参数
- B:System.in通过BufferedReader进行包装
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- C:Scanner
- Scanner sc = new Scanner(System.in);
(3)输出语句的原理和如何使用字符流输出数据
- A:原理
- System.out.println(“helloworld”);
- PrintStream ps = System.out;
- ps.println(“helloworld”);
- B:把System.out用字符缓冲流包装一下使用
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
- A:原理
/** 标准输入输出流* System类中的两个成员变量:* public static final InputStream in “标准”输入流。* public static final PrintStream out “标准”输出流。* * InputStream is = System.in;* PrintStream ps = System.out;*/
public class SystemOutDemo {public static void main(String[] args) {// 有这里的讲解我们就知道了,这个输出语句其本质是IO流操作,把数据输出到控制台。System.out.println("helloworld");// 获取标准输出流对象PrintStream ps = System.out;ps.println("helloworld");ps.println();// ps.print();//这个方法不存在// System.out.println();// System.out.print();}
}
- 用System.in实现键盘录入数据。用BufferedReader改进。
/** System.in 标准输入流。是从键盘获取数据的* * 键盘录入数据:* A:main方法的args接收参数。* java HelloWorld hello world java* B:Scanner(JDK5以后的)* Scanner sc = new Scanner(System.in);* String s = sc.nextLine();* int x = sc.nextInt()* C:通过字符缓冲流包装标准输入流实现* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));*/
public class SystemInDemo {public static void main(String[] args) throws IOException {// //获取标准输入流// InputStream is = System.in;// //我要一次获取一行行不行呢?// //行。// //怎么实现呢?// //要想实现,首先你得知道一次读取一行数据的方法是哪个呢?// //readLine()// //而这个方法在哪个类中呢?// //BufferedReader// //所以,你这次应该创建BufferedReader的对象,但是底层还是的使用标准输入流// // BufferedReader br = new BufferedReader(is);// //按照我们的推想,现在应该可以了,但是却报错了// //原因是:字符缓冲流只能针对字符流操作,而你现在是字节流,所以不能使用?// //那么,我还就想使用了,请大家给我一个解决方案?// //把字节流转换为字符流,然后在通过字符缓冲流操作// InputStreamReader isr = new InputStreamReader(is);// BufferedReader br= new BufferedReader(isr);BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入一个字符串:");String line = br.readLine();System.out.println("你输入的字符串是:" + line);System.out.println("请输入一个整数:");// int i = Integer.parseInt(br.readLine());line = br.readLine();int i = Integer.parseInt(line);System.out.println("你输入的整数是:" + i);}
}
- 用System.out实现控制台输出数据。用BufferedWriter改进。
/** 转换流的应用。*/
public class SystemOutDemo2 {public static void main(String[] args) throws IOException {// 获取标准输入流// // PrintStream ps = System.out;// // OutputStream os = ps;// OutputStream os = System.out; // 多态// // 我能不能按照刚才使用标准输入流的方式一样把数据输出到控制台呢?// OutputStreamWriter osw = new OutputStreamWriter(os);// BufferedWriter bw = new BufferedWriter(osw);BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));bw.write("hello");bw.newLine();// bw.flush();bw.write("world");bw.newLine();// bw.flush();bw.write("java");bw.newLine();bw.flush();bw.close();}
}
随机访问流
RandomAccessFile概述
- RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。支持对随机访问文件的读取和写入。
访问模式
- “r” 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
- “rw” 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
- “rws” 打开以便读取和写入,对于 “rw”,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
- “rwd” 打开以便读取和写入,对于 “rw”,还要求对文件内容的每个更新都同步写入到底层存储设备。
一般使用
rw模式
。案例演示
public static void main(String[] args) throws IOException {write();read();
}private static void read() throws IOException {RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");int i = raf.readInt();System.out.println(i);System.out.println("当前指针位置:" + raf.getFilePointer());char ch = raf.readChar();System.out.println(ch);System.out.println("当前指针位置:" + raf.getFilePointer());String s = raf.readUTF();System.out.println(s);System.out.println("当前指针位置:" + raf.getFilePointer());System.out.println("读取数据完毕");System.out.println("当前指针位置:" + raf.getFilePointer());raf.seek(0);// 返回数据开头int ii = raf.readInt();System.out.println(ii);
}private static void write() throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");raf.writeInt(100); // 写入int类型raf.writeChar('爱');// 写入字符raf.writeUTF("中国你好");// 写入字符串raf.close();System.out.println("写入成功");
}
合并流
SequenceInputStream概述
- SequenceInputStream类可以将多个输入流串流在一起,合并为一个输入流,因此,该流也被称为合并流。
SequenceInputStream的构造方法
- SequenceInputStream(InputStream s1, InputStream s2)
- SequenceInputStream(Enumeration
/** 以前的操作:* a.txt -- b.txt* c.txt -- d.txt* * 现在想要:* a.txt+b.txt -- c.txt*/
public class SequenceInputStreamDemo {public static void main(String[] args) throws IOException {// SequenceInputStream(InputStream s1, InputStream s2)// 需求:把ByteArrayStreamDemo.java和DataStreamDemo.java的内容复制到Copy.java中InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");InputStream s2 = new FileInputStream("DataStreamDemo.java");SequenceInputStream sis = new SequenceInputStream(s1, s2);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Copy.java"));// 如何写读写呢,其实很简单,你就按照以前怎么读写,现在还是怎么读写byte[] bys = new byte[1024];int len = 0;while ((len = sis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();sis.close();}
}
- 方式二:
public class SequenceInputStreamDemo2 {public static void main(String[] args) throws IOException {// 需求:把下面的三个文件的内容复制到Copy.java中// ByteArrayStreamDemo.java,CopyFileDemo.java,DataStreamDemo.java// SequenceInputStream(Enumeration e)// 通过简单的回顾我们知道了Enumeration是Vector中的一个方法的返回值类型。// Enumeration<E> elements()Vector<InputStream> v = new Vector<InputStream>();InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");InputStream s2 = new FileInputStream("CopyFileDemo.java");InputStream s3 = new FileInputStream("DataStreamDemo.java");v.add(s1);v.add(s2);v.add(s3);Enumeration<InputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Copy.java"));// 如何写读写呢,其实很简单,你就按照以前怎么读写,现在还是怎么读写byte[] bys = new byte[1024];int len = 0;while ((len = sis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();sis.close();}
}
序列化流
序列化流概述
对象序列化是将对象状态转换为可保持或传输的过程。一般的格式是与平台无关的二进制流,可以将这种二进制流持久保存在磁盘上,也可以通过网络将这种二进制流传输到另一个网络结点。
序列化流:可以把
对象
写入文本文件或者在网络中传输- ObjectOutputStream
- 反序列化流:把这种
二进制流数据
还原成对象
。- ObjectInputStream
- 序列化操作问题
- 为什么要实现序列化?
- 如何实现序列化?
- 让被序列化的对象所属类实现序列化接口。
- 该接口是一个标记接口。没有功能需要实现。
- 序列化数据后,再次修改类文件,读取数据会出问题,如何解决呢?
- 在类文件中,给出一个固定的序列化id值。而且,这样也可以解决黄色警告线问题
- 使用transient关键字声明不需要序列化的成员变量
public class ObjectStreamDemo {public static void main(String[] args) throws IOException,ClassNotFoundException {// 由于我们要对对象进行序列化,所以我们先自定义一个类// 序列化数据其实就是把对象写到文本文件// write();read();}private static void read() throws IOException, ClassNotFoundException {// 创建反序列化对象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));// 还原对象Object obj = ois.readObject();// 释放资源ois.close();// 输出对象System.out.println(obj);}private static void write() throws IOException {// 创建序列化流对象ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));// 创建对象Person p = new Person("林青霞", 27);// public final void writeObject(Object obj)oos.writeObject(p);// 释放资源oos.close();}
}
- person类
/** NotSerializableException:未序列化异常* * 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。* 该接口居然没有任何方法,类似于这种没有方法的接口被称为标记接口。* * java.io.InvalidClassException: * cn.itcast_07.Person; local class incompatible: * stream classdesc serialVersionUID = -2071565876962058344, * local class serialVersionUID = -8345153069362641443* * 为什么会有问题呢?* Person类实现了序列化接口,那么它本身也应该有一个标记值。* 这个标记值假设是100。* 开始的时候:* Person.class -- id=100* wirte数据: oos.txt -- id=100* read数据: oos.txt -- id=100 * * 现在:* Person.class -- id=200* wirte数据: oos.txt -- id=100* read数据: oos.txt -- id=100* 我们在实际开发中,可能还需要使用以前写过的数据,不能重新写入。怎么办呢?* 回想一下原因是因为它们的id值不匹配。* 每次修改java文件的内容的时候,class文件的id值都会发生改变。* 而读取文件的时候,会和class文件中的id值进行匹配。所以,就会出问题。* 但是呢,如果我有办法,让这个id值在java文件中是一个固定的值,这样,你修改文件的时候,这个id值还会发生改变吗?* 不会。现在的关键是我如何能够知道这个id值如何表示的呢?* 不用担心,你不用记住,也没关系,点击鼠标即可。* 你难道没有看到黄色警告线吗?* * 我们要知道的是:* 看到类实现了序列化接口的时候,要想解决黄色警告线问题,就可以自动产生一个序列化id值。* 而且产生这个值以后,我们对类进行任何改动,它读取以前的数据是没有问题的。* * 注意:* 我一个类中可能有很多的成员变量,有些我不想进行序列化。请问该怎么办呢?* 使用transient关键字声明不需要序列化的成员变量*/
public class Person implements Serializable {private static final long serialVersionUID = -2071565876962058344L;private String name;// private int age;private transient int age;// int age;public Person() {super();}public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}
}
Properties集合
Properties概述
(1)是一个
集合类
,Hashtable的子类,是一个可以和IO流相结合使用的集合类。可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。(2)特有功能
- public Object setProperty(String key,String value):添加元素
- public String getProperty(String key):获取元素
- public Set
<String>
stringPropertyNames():获取所有的键的集合
(3)和IO流结合的方法
- 把
键值对
形式的文本文件内容加载到集合中- public void load(Reader reader)
- public void load(InputStream inStream)
- 把集合中的数据存储到文本文件中
- public void store(Writer writer,String comments)
- public void store(OutputStream out,String comments)
- 把
Properties作为Map集合的使用
/** 是Hashtable的子类,说明是一个Map集合。*/
public class PropertiesDemo {public static void main(String[] args) {// 作为Map集合的使用// 下面这种用法是错误的,一定要看API,如果没有<>,就说明该类不是一个泛型类,在使用的时候就不能加泛型// Properties<String, String> prop = new Properties<String, String>();Properties prop = new Properties();// 添加元素prop.put("it002", "hello");prop.put("it001", "world");prop.put("it003", "java");// System.out.println("prop:" + prop);// 遍历集合Set<Object> set = prop.keySet();for (Object key : set) {Object value = prop.get(key);System.out.println(key + "---" + value);}}
}
Properties的特殊功能
- public Object setProperty(String key,String value):添加元素
- public String getProperty(String key):获取元素
- public Set
<String>
stringPropertyNames():获取所有的键的集合
public class PropertiesDemo2 {public static void main(String[] args) {// 创建集合对象Properties prop = new Properties();// 添加元素prop.setProperty("张三", "30");prop.setProperty("李四", "40");prop.setProperty("王五", "50");// public Set<String> stringPropertyNames():获取所有的键的集合Set<String> set = prop.stringPropertyNames();for (String key : set) {String value = prop.getProperty(key);System.out.println(key + "---" + value);}}
}/** class Hashtalbe<K,V> { public V put(K key,V value) { ... } }* * class Properties extends Hashtable { public V setProperty(String key,String* value) { return put(key,value); } }*/
Properties和IO流的结合使用
- public void load(Reader reader):把文件中的数据读取到集合中
- public void store(Writer writer,String comments):把集合中的数据存储到文件
/** 这里的集合必须是Properties集合:* public void load(Reader reader):把文件中的数据读取到集合中* public void store(Writer writer,String comments):把集合中的数据存储到文件*/
public class PropertiesDemo3 {public static void main(String[] args) throws IOException {// myLoad();myStore();}private static void myStore() throws IOException {// 创建集合对象Properties prop = new Properties();prop.setProperty("林青霞", "27");prop.setProperty("武鑫", "30");prop.setProperty("刘晓曲", "18");//public void store(Writer writer,String comments):把集合中的数据存储到文件Writer w = new FileWriter("name.txt");prop.store(w, "helloworld");w.close();}private static void myLoad() throws IOException {Properties prop = new Properties();// public void load(Reader reader):把文件中的数据读取到集合中// 注意:这个文件的数据必须是键值对形式Reader r = new FileReader("prop.txt");prop.load(r);r.close();System.out.println("prop:" + prop);}
}
- 练习1:我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”
/** 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。* 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”* * 分析:* A:把文件中的数据加载到集合中* B:遍历集合,获取得到每一个键* C:判断键是否有为"lisi"的,如果有就修改其值为"100"* D:把集合中的数据重新存储到文件中*/
public class PropertiesTest {public static void main(String[] args) throws IOException {// 把文件中的数据加载到集合中Properties prop = new Properties();Reader r = new FileReader("user.txt");prop.load(r);r.close();// 遍历集合,获取得到每一个键Set<String> set = prop.stringPropertyNames();for (String key : set) {// 判断键是否有为"lisi"的,如果有就修改其值为"100"if ("lisi".equals(key)) {prop.setProperty(key, "100");break;}}// 把集合中的数据重新存储到文件中Writer w = new FileWriter("user.txt");prop.store(w, null);w.close();}
}
- 练习2:我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
/** 我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。*/
public class PropertiesTest2 {public static void main(String[] args) throws IOException {// 读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"// 创建一个文件// File file = new File("count.txt");// if (!file.exists()) {// file.createNewFile();// }// 把数据加载到集合中Properties prop = new Properties();Reader r = new FileReader("count.txt");prop.load(r);r.close();// 我自己的程序,我当然知道里面的键是谁String value = prop.getProperty("count");int number = Integer.parseInt(value);if (number > 5) {System.out.println("游戏试玩已结束,请付费。");System.exit(0);} else {number++;prop.setProperty("count", String.valueOf(number));Writer w = new FileWriter("count.txt");prop.store(w, null);w.close();GuessNumber.start();}}
}
NIO包下的IO流
(1) JDK4出现NIO。新IO和传统的IO有相同的目的,都是用于进行输入输出的,但新IO使用了不同的方式来处理输入输出,采用内存映射文件的方式,将文件或者文件的一段区域映射到内存中,就可以像访问内存一样的来访问文件了,这种方式效率比旧IO要高很多,但是目前好多地方我们看到的还是旧IO的引用,所以我们仍以旧IO为主,知道NIO即可。
(2)JDK7的NIO的使用
- Path:与平台无关的路径。
- Paths:包含了返回Path的静态方法。
- public static Path get(URI uri):根据给定的URI来确定文件路径。
- Files:提供了常见的功能
- 复制文本文件:
public static long copy(Path source, OutputStream out)
- 把集合中的数据写到文本文件:
public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options)
- 复制文本文件:
案例1:复制文件
Files.copy(Paths.get("Demo.java"), newFileOutputStream("Copy.Java"));
案例2:把集合中的数据写到文件
public class NIODemo {public static void main(String[] args) throws IOException {// public static long copy(Path source,OutputStream out)// Files.copy(Paths.get("ByteArrayStreamDemo.java"), new// FileOutputStream(// "Copy.java"));ArrayList<String> array = new ArrayList<String>();array.add("hello");array.add("world");array.add("java");Files.write(Paths.get("array.txt"), array, Charset.forName("GBK"));}
}
No_16_0324 Java基础学习第二十三天相关推荐
- java基础学习 --- 第二节 变量、数据类型和运算符
java基础学习 - 第二节 变量.数据类型和运算符 一.变量 1.变量就是会发生改变的数据,程序运行时,这些数据以变量的形式被保存到计算机的内存中.如图 2.使用变量的步骤: 声明变量 - ...
- Java基础学习第二讲:Java数据类型,变量及常量
Java数据类型,变量及常量 一.Java数据类型 1.什么是数据类型? 数据类型在计算机语言里面,是对内存位置的一个抽象表达方式,可以理解为针对内存的一种抽象的表达方式. 2.为什么要用到数据类型? ...
- Java基础学习第二十讲:Java序列化
Java序列化 一.序列化和反序列化 序列化:指堆内存中的java对象数据,通过某种方式把对存储到磁盘文件中,或者传递给其他网络节点(网络传输).这个过程称为序列化,通常是指将数据结构或对象转化成二进 ...
- Java中大数据数组,Java基础学习笔记之数组详解
摘要:这篇Java开发技术栏目下的"Java基础学习笔记之数组详解",介绍的技术点是"java基础学习笔记.基础学习笔记.Java基础.数组详解.学习笔记.Java&qu ...
- 尚学堂JAVA基础学习笔记_2/2
尚学堂JAVA基础学习笔记_2/2 文章目录 尚学堂JAVA基础学习笔记_2/2 写在前面 第10章 IO技术 1. IO入门 2. IO的API 3. 装饰流 4. IO实战 5. CommonsI ...
- Java基础学习(9)
Java基础学习 一.常用方法(API) 1.1 Math 1.2 System 1.3 Runtime 1. 4 .object 1.5 Objects 1.6 BigInteger BigInte ...
- Java基础学习(12)
Java基础学习 一.不可变集合 二.Stream流 2.1 Stream流数据添加 2.2 Stream流的中间方法 2.3 Stream终结方法 三. 方法引用 3.1 方法引用的基本概念 3.2 ...
- java基础学习-4
Java基础学习-4 static 注意事项 重新认识main方法 继承 小练习 子类到底能继承父类中的哪些内容(第126集)*** 继承过程中成员变量和成员方法的访问特点 小练习 小总结 构造方法的 ...
- java基础学习-6
Java基础学习-6 双列集合 Map的常见API Map的遍历方式 HashMap 小练习 LinkedHashMap TreeMap TreeMap--小练习1 TreeMap--小练习2 Tre ...
最新文章
- ReactNative ES6简介 及基本语法第一篇
- wxWidgets:wxCustomBackgroundWindow<W> 类模板用法
- 浪潮存储双活方案:新疆道路运输管理局的大数据应用不再是梦
- Linux中文档去掉windows文本的多余的回车符(^M)
- netcore开源框架_Mono和.NET Core 从比翼双飞到合体
- 如何处理分析Flink作业反压的问题?
- ABViewer免费汉化下载注册地址图形查看器教程功能介绍
- 计算机课堂热身游戏,电脑课我们常玩的13个小游戏
- Fiddler4抓包工具使用教程
- SpringBoot简单实现上传图片到七牛云
- 小飞鱼通达二开 致远OA配置(写数据到通达OA第三方系统)接口(图文)
- mysql count函数 null_MySql中COUNT(XXX OR NULL)
- STL初步——集合Set
- 测试工具:adb+perfdog+charles+tidevice+Monkey
- 尚硅谷-SpringCloud
- 王者荣耀服务器维护费用,王者荣耀服务器全线崩溃!事后只补偿100铭文!网友:卸载了...
- 数据分析01 - 规范化方法
- NLP十大Baseline论文简述(一) - Word2vec
- Kali入侵Windows主机
- 企业订单管理软件|移讯云订货宝系统功能介绍