java 的IO流--文件流---缓冲流---转换流--打印流---数据流---数组流---对象流--序列化
集合相关辨析题
集合和数组的比较
数组:
1.长度不可变
2.数组中可以存放基本数据类和引用数据类型
3.数组是有序的,不唯一
4.数据结构不同
5,数组中能调用的api不多
Vector 和ArrayList的联系和区别
1.实现原理相同,功能相同,都是长度 可变的数组结构,很多情况下可以互用
区别:
1.Vector 是早期JDK接口,Arraylist 是代替Vector的新接口
2.Vector 线程安全,效率底下Arraylist 重速度轻安全,线程非安全
3.扩容Vector 默认增长1倍Arraylist 增长50%
4.遍历方式
HashMap 和Hashtable的联系和区别
1.hashtable不允许null值,HashMap允许null值
2.hashtable继承Dictionary, HashMap实现了Map
3.hashtable 线程安全,hashMap线程不安全
IO流概述
数据源:datasource
流的分类:
流的方向:
输入流:数据源到程序(InputStream、Reader读进来)
输出流:程序到目的地(OutPutStream、Writer写出去)
处理数据单元:
字节流:按照字节读取数据(InputStream、OutPutStream)
字符流:按照字符读取数据(Reader、Writer)
功能不同
节点流:可以从数据源或者目的地读写数据
处理流(包装流),不直接连接到数据源或者目的地,是其他流进行封装,目的主要是简化操作和提高性能
节点流处于IO操作第一线,所有操作必须通过他们进行,
处理流可以其他流进行处理(提高效率)
字节流(读写)(任意格式文件)
public static void main(String[] args) { try { InputStream is=new FileInputStream("D:/配置文件/test.txt"); OutputStream os=new FileOutputStream("D:/配置文件/demo.txt"); //准备一个字节数组,用于作为中转站 byte[] b =new byte[1024]; //默认每次读取的长度为文件大小的字节数,文件大小字节数超多1024,则每次默认1024 int len ; //开始读取 len= is.read(b); while(len!=-1){ System.out.println("----------------------"+len); //将读取到的信息写入目的地读一点写一点 os.write(b,0,len); //继续去读 len=is.read(b); os.flush(); } //关闭资源,先开后关 os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } }
字符流(读写)(只能用于操作字符文件 txt、 java等)
/* * 字节流: * FileInputStream * FileOutputStream * 字符流: * FileReader * FileWriter */ public class TestCopy { public static void main(String[] args) throws IOException { Reader reader=new FileReader("D:/配置文件/test.txt"); int c; while((c=reader.read())!=-1){ System.out.print((char)c); } reader.close(); } }
实现文件夹的复制
* * 实现文件夹复制: * 1.如果是文件,要进行文件复制 * 2.如果是文件夹,在目标位置进行创建同名的文件夹 * 3.子文件继续复制,子文件夹继续创建 * * 字节流:FileInputStream ,FileOutputStream * 递归方案 */ public class TestFloderCopy { public static void main(String[] args) { copyDir(new File("D:/配置文件/test.txt"),new File("D:/")); } /* * 实现文件夹的拷贝 */ private static void copyDir(File src,File dest){ if(src==null){ System.out.println("未找到此文件") return; } if(src.isFile()){ copyFile(src,dest); }else if(src.isDirectory()){ //是文件夹,需要在dest下创建同名文件夹 File dir=new File(dest,src.getName()); //创建目录 dir.mkdir(); //获取src 下所有的子文件信息 File[] files = src.listFiles(); for (File file : files) { copyDir(file, dir); } } } /* * 实现文件的拷贝 * srcFile目标文件 * destDir */ private static void copyFile(File srcFile, File destDir) { InputStream input=null; OutputStream outPut=null; //创建输入输出流 try { //input=new FileInputStream(srcFile.getAbsolutePath()); // outPut=new FileOutputStream(new File(destDir,srcFile.getName())); //使用缓冲流包装一下 input=new BufferedInputSteam(new FileInputStream(srcFile.getAbsolutePath())); outPut=new BufferedOutputSteam(new FileOutputStream(new File(destDir,srcFile.getName()))); byte[] b=new byte[1024]; //记录每次中转的长度 int len; while((len=input.read())!=-1){ outPut.write(b, 0, len); outPut.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(outPut!=null){ try { outPut.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(input!=null){ try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
缓冲字节流
** * 缓冲流: * 字节流:BufferedInputSteam,BufferedoutputSteam, * 字符流:BufferedReader,BufferedWriter * reader.readline()//读一行 * writer.write()//写 * writer.newLine()//读下一行//换行操作 * * @author lenovo * */ public class TestBuffered { public static void main(String[] args) throws IOException { BufferedInputStream input=new BufferedInputStream(new FileInputStream("D:/"));//默认8192 BufferedOutputStream output=new BufferedOutputStream(new FileOutputStream("")); copy1(input, output); copy2(input, output); } /* * 准备中转站 * 字节数组 */ private static void copy2(BufferedInputStream input, BufferedOutputStream output) throws IOException { //中转站 byte[] b=new byte[1024]; //每次读取长度 int len; while((len=input.read())!=-1){ output.write(b, 0, len); } output.close(); input.close(); } /* * 每次操作一个字节 */ private static void copy1(BufferedInputStream input, BufferedOutputStream output) throws IOException { int b; while((b=input.read())!=-1){ output.write(b); } output.close(); input.close(); } }
转换流
* * 从键盘连续输入字符串,将输入的字符串写到一个文件中,直到输入exit为止 * * 1.不能使用Scanner如何操作 * System.in:InputStream, 输入流 * System.out:PrintStream 输出流 * * 2.字节流能不能转换成字符流? * 通过转换流可以实现 * InputStreamReader:将字节输入流转换为字符输入流 * BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); * OutPutStreamWriter:将字节输出流转换成字符输出流 * * 4.转换流在创建对象时,可以指定编码格式:GBK,gbk2312,ISO-8859-1,utf-8 */ public class Test { public static void main(String[] args) throws IOException { //创建扫描器 //Scanner sc=new Scanner(System.in); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一串字符;exit退出输入"); BufferedWriter writer=new BufferedWriter(new FileWriter("D:/test.txt")); String info; while(!(info=reader.readLine()).equals("exit")){ writer.write(info); writer.newLine(); writer.flush(); } writer.close(); reader.close(); } }
打印流
printStream ,PrintReader
只有输出流,没有输入流
可以非常方便的将任意数据类型转换成String写到目的地
/* * printStream ,PrintWriter 只有输出流,没有输入流 可以非常方便的将任意数据类型转换成String写到目的地 */ public class TestPrintStream { public static void main(String[] args) throws IOException { //testPs(); testPw(); } /* * 测试字符打印流---文本文件的复制 */ private static void testPw() throws IOException { //输入流 BufferedReader br=new BufferedReader(new FileReader("D:/print.txt")); //输出流 //printWriter,第二个参数是一个boolean,表示自动刷新 PrintWriter pw =new PrintWriter(new BufferedWriter(new FileWriter("E:/print.txt")),true); String line; while((line=br.readLine())!=null){ pw.println(line); } pw.close(); br.close(); } /* * 测试字节打印流 */ private static void testPs() throws FileNotFoundException { PrintStream ps=new PrintStream(new File("D:/print.txt")); ps.println(123); ps.println('a'); ps.println(true); ps.println(new Date()); ps.close(); } }
数据流(打印流会将数据全部转换成字符串,不能保留数据类型)
对象流(数据不能保存自己定义的对象数据类型)
保存的对象在使用对象流存储时要实现序列化接口
先存储,后读
读的顺序要与存的一致
读和数据流不同不需要创建对象
/*打印流 * PrintStream 和PrintWriter * 可以将任何数据转换成字符串写到文件中, * 1.数据类型无法保存 * 2.只有输出流没有输入流 * * 数据流:DataInputStream ,DataOutputStream:用来直接读写基本数据类型和String类型 * 对象流:ObjectInputStream,ObjectOutputStream:用来直接读写任何数据类型 * * 只有字节流,没有字符流 * 只能先写,然后再读 * */ public class TestDataStream { public static void main(String[] args) throws IOException { //writeData(); readData(); } /* * 使用数据流将文件中数据读出来 */ private static void readData() throws IOException { FileInputStream fis = new FileInputStream("D:/data.txt"); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); //读要按写的顺序读 System.out.println(dis.readBoolean()); System.out.println(dis.readChar()); System.out.println(dis.readDouble()); System.out.println(dis.readUTF()); dis.close(); } /* * 使用数据流将基本数据类型和字符串写入一个文件中 */ private static void writeData() throws IOException { //文件输出流对文件进行写的操作:节点流 FileOutputStream output=new FileOutputStream("D:/data.txt"); //处理流:提高效率 BufferedOutputStream bos=new BufferedOutputStream(output); //处理流:简化操作 DataOutputStream dos = new DataOutputStream(bos); dos.writeBoolean(false); dos.writeChar('a'); dos.writeDouble(3.15); dos.writeUTF("你好数据流"); dos.close(); } }
序列化(对象流存储对象数据,写时,因为内存中不能永久化保存,会报序列化错误,所以要对存储对象进行序列化标记Serializable,)
标记接口没有实际内容,表示只要实现化这个接口的类就能进行序列化操作
SerializableUID:用于表示本地对象和获取对象是不是同一个类型
transient:临时的,不参与序列化
数组流(为了将其他数据类型转换成字节数组)
字节数组流:ByteArrayInputStream,ByteArrayOutputStream
字符数组流:CharArrayReader,CharArrayWriter
字符串流:StringReader, StringWriter
String —>byte[]:str.getBytes();
其他类型没有特定的方法转换为字节数组可以使用数组流转换
节点:
- 点赞 2
- 收藏
- 分享
- 文章举报
- JAVA序列化和反序列化 对象<=>IO流 对象<=>字节数组
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
- Java基础——IO流(字节流、字符流、文件与文件夹的拷贝、缓冲流、转换流、基本数据类型流、序列化和反序列化)
- JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
- JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流
- JAVA基础 day23 IO流的其他类 (对象的序列化 管道流 RandomAccessFile类(随机读取文件) DateStream(操作基本数据类型的流) 操作数组的流 )字符编码问题
- IO流-数据流、对象流(序列化与反序列化)、打印流
- JAVA基础学习(二十二)--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
- Java学习笔记之输入输出流(二) 缓冲流、随机流、数组流、数据流、对象流
- Java笔记(7)-输入、输出流、File类、InputStream、Reader、文件字节流、文件字符流、缓冲流、随机流、数据流、对象流、序列化和对象克隆、Scanner解析文件、Console流
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- Jedis存储Java对象--Java序列化为byte数组方式
- JAVA IO流(对象的序列化)
- java对象转换为字节数组
- Java集合对象和数组对象的转换
- 18、Java入门—Java IO流之对象的序列化和反序列化
- 今天无意中发现JavaBean类基本都要求实现了Serializable接口,以前只是知道序列化以后,可以通过io流的方式将对象序列化和反序列化,进行存取,但不知道为什么需要序列化,今天总结一下
- 用Java 在 KSOAP中序列化复杂对象数组实体
- JSON-JSON字符串转换成JSON对象、JSON对象数组、java实体类以及保存到List列表中
- java 对象流、管道流、基本数据流、字节数组流