经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Java-IO
来源:cnblogs  作者:为你编程  时间:2018/10/9 10:16:04  对本文有异议
  1. 1 public class Member {
  2. 2 private String name;
  3. 3 private int age;
  4. 4 public Member() {
  5. 5 }
  6. 6 public Member(String name, int age) {
  7. 7 this.name = name;
  8. 8 this.age = age;
  9. 9 }
  10. 10 public void setName(String name){
  11. 11 this.name = name;
  12. 12 }
  13. 13 public void setAge(int age) {
  14. 14 this.age = age;
  15. 15 }
  16. 16 public String getName() {
  17. 17 return name;
  18. 18 }
  19. 19 public int getAge() {
  20. 20 return age;
  21. 21 }
  22. 22 }
  1. 1 import java.io.DataInputStream;
  2. 2 import java.io.DataOutputStream;
  3. 3 import java.io.FileInputStream;
  4. 4 import java.io.FileOutputStream;
  5. 5 import java.io.IOException;
  6. 6
  7. 7 /**
  8. 8 * DataOutputStream
  9. 9 */
  10. 10 public class DataStreamTest {
  11. 11 public static void main(String[] args) {
  12. 12 Member[] members = { new Member("Justin", 90),
  13. 13 new Member("momor", 95),
  14. 14 new Member("Bush", 88) };
  15. 15 try {
  16. 16 DataOutputStream dos = new DataOutputStream(new FileOutputStream("E:/member.txt"));
  17. 17 for (Member member : members) {
  18. 18 // 写入UTF字符串
  19. 19 dos.writeUTF(member.getName());
  20. 20 // 写入int数据
  21. 21 dos.writeInt(member.getAge());
  22. 22 }
  23. 23 dos.flush();// 将流中的数据刷新到目的地
  24. 24 dos.close();// 关闭流
  25. 25
  26. 26 DataInputStream dis = new DataInputStream(new FileInputStream("E:/member.txt"));
  27. 27 // 读出数据并还原为对象
  28. 28 for (int i = 0; i < members.length; i++) {
  29. 29 String name = dis.readUTF();// 读出UTF字符串
  30. 30 int score = dis.readInt();// 读出int数据
  31. 31 members[i] = new Member(name, score);
  32. 32 }
  33. 33 dis.close();// 关闭流
  34. 34
  35. 35 // 显示还原后的数据
  36. 36 for (Member member : members) {
  37. 37 System.out.printf("%s\t%d%n", member.getName(), member.getAge());
  38. 38 }
  39. 39 } catch (IOException e) {
  40. 40 e.printStackTrace();
  41. 41 }
  42. 42 }
  43. 43 }
  1. 1 import java.io.File;
  2. 2 import java.io.FileInputStream;
  3. 3 import java.io.IOException;
  4. 4 import java.io.InputStream;
  5. 5
  6. 6 /**
  7. 7 * FileInputStream 字节输入流
  8. 8 */
  9. 9 public class FileInputStreamTest {
  10. 10 public static void main(String[] args) {
  11. 11 InputStream in = null;//文件输入流
  12. 12 try{
  13. 13 in = new FileInputStream(new File("E:/fileInputStreamTest.txt"));
  14. 14 /*
  15. 15 * FileInputStream是有缓冲区的,所以用完之后必须关闭,否则可能导致内存占满,数据丢失。
  16. 16 */
  17. 17 int i = 0;//接收读取的内容
  18. 18 //streamReader.read()从文件读取一个字节,并当做int类型返回,如果遇到文件末尾,则返回-1
  19. 19 while((i = in.read())!=-1) {
  20. 20 System.out.print((char)i);//打印读取的内容
  21. 21 }
  22. 22 }catch (final IOException e) {
  23. 23 e.printStackTrace();
  24. 24 }finally{
  25. 25 try{
  26. 26 in.close(); //关闭流
  27. 27 }catch (IOException e) {
  28. 28 e.printStackTrace();
  29. 29 }
  30. 30 }
  31. 31 }
  32. 32 }
  1. 1 import java.io.FileInputStream;
  2. 2 import java.io.FileOutputStream;
  3. 3 import java.io.IOException;
  4. 4
  5. 5 /**
  6. 6 * 文件拷贝
  7. 7 */
  8. 8 public class FileCopy {
  9. 9 public static void main(String[] args) {
  10. 10 byte[] b=new byte[512]; //一次取出的字节数大小,缓冲区大小
  11. 11 int i=0;
  12. 12 FileInputStream input=null;
  13. 13 FileOutputStream out =null;
  14. 14 try {
  15. 15 input=new FileInputStream("E:/fileInputStreamTest.txt");
  16. 16 out=new FileOutputStream("E:/copy.txt",true); //如果文件不存在会自动创建,true表示写在文件末尾
  17. 17
  18. 18 while ((i=input.read(b))!=-1) { //i的目的在于防止最后一次读取的字节小于b长度,否则会自动被填充0
  19. 19 //使用输出流写入读取的内容到指定文件
  20. 20 out.write(b, 0, i);
  21. 21 }
  22. 22 } catch (final IOException e) {
  23. 23 e.printStackTrace();
  24. 24 }finally{
  25. 25 try {
  26. 26 input.close();
  27. 27 out.close();
  28. 28 } catch (IOException e) {
  29. 29 e.printStackTrace();
  30. 30 }
  31. 31 }
  32. 32 }
  33. 33 }
  1. 1 import java.io.BufferedReader;
  2. 2 import java.io.BufferedWriter;
  3. 3 import java.io.File;
  4. 4 import java.io.FileReader;
  5. 5 import java.io.FileWriter;
  6. 6 import java.io.IOException;
  7. 7
  8. 8 /**
  9. 9 * BufferedReader、BufferedWriter
  10. 10 */
  11. 11 public class FileWriterTest {
  12. 12 public static void main(String[] args) throws IOException{
  13. 13 BufferedReader br = new BufferedReader(new FileReader(new File("E:/printTest.txt")));
  14. 14 BufferedWriter bw = new BufferedWriter(new FileWriter(new File("E:/writeCopy.txt")));
  15. 15
  16. 16 String str = null;
  17. 17 try {
  18. 18 while((str = br.readLine())!=null) { //一次读一行
  19. 19 bw.write(str);
  20. 20 }
  21. 21 } catch (IOException e) {
  22. 22 e.printStackTrace();
  23. 23 }finally {
  24. 24 br.close();
  25. 25 bw.close();
  26. 26 }
  27. 27 }
  28. 28 }
  1. 1 import java.io.Serializable;
  2. 2
  3. 3 public class Student implements Serializable{
  4. 4
  5. 5 private static final long serialVersionUID = 1L;
  6. 6 private String name;
  7. 7 private int age;
  8. 8 public Student() {}
  9. 9 public String getName() {
  10. 10 return name;
  11. 11 }
  12. 12 public void setName(String name) {
  13. 13 this.name = name;
  14. 14 }
  15. 15 public int getAge() {
  16. 16 return age;
  17. 17 }
  18. 18 public void setAge(int age) {
  19. 19 this.age = age;
  20. 20 }
  21. 21 public Student(String name, int age) {
  22. 22 this.name = name;
  23. 23 this.age = age;
  24. 24 }
  25. 25 @Override
  26. 26 public String toString() {
  27. 27 return "Student [name=" + name + ", age=" + age + "]";
  28. 28 }
  29. 29 }
  1. 1 import java.io.FileInputStream;
  2. 2 import java.io.FileOutputStream;
  3. 3 import java.io.IOException;
  4. 4 import java.io.ObjectInputStream;
  5. 5 import java.io.ObjectOutputStream;
  6. 6 import java.util.ArrayList;
  7. 7 import java.util.List;
  8. 8
  9. 9 /**
  10. 10 * ObjectOutputStream
  11. 11 */
  12. 12 public class ObjectStream {
  13. 13 public static void main(String[] args) {
  14. 14 // 对象输入输出流
  15. 15 ObjectOutputStream ow = null;
  16. 16 ObjectInputStream or = null;
  17. 17 List<Student> stuList = new ArrayList<Student>();
  18. 18 Object obj = null;
  19. 19 try {
  20. 20 // 先将对象输出到文件中
  21. 21 ow = new ObjectOutputStream(new FileOutputStream("E:/student.txt"));
  22. 22 ow.writeObject(new Student("gg", 22));
  23. 23 ow.writeObject(new Student("tt", 18));
  24. 24 ow.writeObject(new Student("rr", 17));
  25. 25 ow.writeObject(null);//添加一个null 是为了后续判断是否读取到末尾
  26. 26 // 然后从该文件读取对象
  27. 27 or = new ObjectInputStream(new FileInputStream("E:/student.txt"));
  28. 28
  29. 29 while((obj = or.readObject())!=null) {
  30. 30 stuList.add((Student) obj);// 将读取的对象强转成Student对象
  31. 31 }
  32. 32 for (Student student : stuList) {
  33. 33 System.out.println(student.getName() + "-" + student.getAge());
  34. 34 }
  35. 35 } catch (IOException e) {
  36. 36 e.printStackTrace();
  37. 37 } catch (ClassNotFoundException e) {
  38. 38 e.printStackTrace();
  39. 39 } finally {
  40. 40 try {
  41. 41 ow.close();
  42. 42 or.close();//关闭流
  43. 43 } catch (IOException e) {
  44. 44 e.printStackTrace();
  45. 45 }
  46. 46 }
  47. 47 }
  48. 48 }
  1. 1 import java.io.FileReader;
  2. 2 import java.io.IOException;
  3. 3 import java.io.PrintWriter;
  4. 4
  5. 5 /**
  6. 6 * PrintWriter
  7. 7 */
  8. 8 public class PrintTest {
  9. 9 public static void main(String[] args) {
  10. 10 FileReader reader=null; //读取字符文件的流
  11. 11 PrintWriter writer=null; //写字符到控制台的流
  12. 12 char[] b=new char[512]; //一次取出的字节数大小,缓冲区大小
  13. 13 int i=0;
  14. 14 try {
  15. 15 reader=new FileReader("E:/fileInputStreamTest.txt");
  16. 16 //writer=new PrintWriter(System.out); //输出到控制台
  17. 17 writer=new PrintWriter("E:/printTest.txt"); //输出指定文件,没有自动创建
  18. 18
  19. 19 while ((i = reader.read(b))!=-1) {
  20. 20 writer.write(b, 0, i);
  21. 21 }
  22. 22 } catch (IOException e) {
  23. 23 e.printStackTrace();
  24. 24 }finally{
  25. 25 try {
  26. 26 reader.close();
  27. 27 } catch (IOException e) {
  28. 28 e.printStackTrace();
  29. 29 }
  30. 30 writer.close();
  31. 31 }
  32. 32 }
  33. 33 }
  1. 1 import java.io.ByteArrayInputStream;
  2. 2 import java.io.IOException;
  3. 3 import java.io.PushbackInputStream;
  4. 4 /**
  5. 5 * 回退流:PushbackInputStream类继承了FilterInputStream类是iputStream类的修饰者。
  6. 6 * 提供可以将数据插入到输入流前端的能力(当然也可以做其他操作)。
  7. 7 * 简而言之PushbackInputStream类的作用就是能够在读取缓冲区的时候提前知道下一个字节是什么,
  8. 8 * 其实质是读取到下一个字符后回退的做法,这之间可以进行很多操作,这有点向你把读取缓冲区的过程当成一个数组的遍历,
  9. 9 * 遍历到某个字符的时候可以进行的操作,
  10. 10 * 当然,如果要插入,能够插入的最大字节数是与推回缓冲区的大小相关的,插入字符肯定不能大于缓冲区吧!
  11. 11 */
  12. 12 public class PushBackInputStreamTest {
  13. 13 public static void main(String[] args) throws IOException {
  14. 14 String str = "hello,rollenholt";
  15. 15 PushbackInputStream push = null; // 声明回退流对象
  16. 16 ByteArrayInputStream bat = null; // 声明字节数组流对象
  17. 17 bat = new ByteArrayInputStream(str.getBytes());
  18. 18 push = new PushbackInputStream(bat); // 创建回退流对象,将拆解的字节数组流传入
  19. 19 int temp = 0;
  20. 20 while ((temp = push.read()) != -1) { // push.read()逐字节读取存放在temp中,如果读取完成返回-1
  21. 21 if (temp == ',') { // 判断读取的是否是逗号
  22. 22 push.unread(temp); //回到temp的位置
  23. 23 temp = push.read(); //接着读取字节
  24. 24 System.out.print("(回退" + (char) temp + ") "); // 输出回退的字符
  25. 25 } else {
  26. 26 System.out.print((char) temp); // 否则输出字符
  27. 27 }
  28. 28 }
  29. 29 }
  30. 30 }
  1. 1 import java.io.BufferedOutputStream;
  2. 2 import java.io.FileInputStream;
  3. 3 import java.io.FileNotFoundException;
  4. 4 import java.io.FileOutputStream;
  5. 5 import java.io.IOException;
  6. 6 import java.io.InputStream;
  7. 7 import java.io.SequenceInputStream;
  8. 8 import java.util.Enumeration;
  9. 9 import java.util.Vector;
  10. 10
  11. 11 /**
  12. 12 * SequenceInputStream
  13. 13 */
  14. 14 public class SequenceInputStreamTest {
  15. 15 /**
  16. 16 * SequenceInputStream合并流,
  17. 17 * 将与之相连接的流集组合成一个输入流并从第一个输入流开始读取,
  18. 18 * 直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
  19. 19 * 合并流的作用是将多个源合并合一个源。可接收枚举类所封闭的多个字节流对象。
  20. 20 */
  21. 21 public static void main(String[] args) {
  22. 22 doSequence();
  23. 23 }
  24. 24 private static void doSequence() {
  25. 25 // 创建一个合并流的对象
  26. 26 SequenceInputStream sis = null;
  27. 27 // 创建输出流。
  28. 28 BufferedOutputStream bos = null;
  29. 29 try {
  30. 30 // 构建流集合。
  31. 31 Vector<InputStream> vector = new Vector<InputStream>();
  32. 32 vector.addElement(new FileInputStream("E:/text1.txt"));
  33. 33 vector.addElement(new FileInputStream("E:/text2.txt"));
  34. 34 vector.addElement(new FileInputStream("E:/text3.txt"));
  35. 35 Enumeration<InputStream> e = vector.elements();
  36. 36
  37. 37 sis = new SequenceInputStream(e); //将多个输入流合并到合并流
  38. 38 bos = new BufferedOutputStream(new FileOutputStream("E:/text4.txt",true));//true追加末尾
  39. 39 // 读写数据
  40. 40 byte[] buf = new byte[1024];
  41. 41 int len = 0;
  42. 42 while ((len = sis.read(buf)) != -1) {
  43. 43 bos.write(buf, 0, len);
  44. 44 bos.flush();
  45. 45 }
  46. 46 } catch (FileNotFoundException e1) {
  47. 47 e1.printStackTrace();
  48. 48 } catch (IOException e1) {
  49. 49 e1.printStackTrace();
  50. 50 } finally {
  51. 51 try {
  52. 52 if (sis != null)
  53. 53 sis.close();
  54. 54 } catch (IOException e) {
  55. 55 e.printStackTrace();
  56. 56 }
  57. 57 try {
  58. 58 if (bos != null)
  59. 59 bos.close();
  60. 60 } catch (IOException e) {
  61. 61 e.printStackTrace();
  62. 62 }
  63. 63 }
  64. 64 }
  65. 65 }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号