返回列表 回复 发帖

Java IO学习笔记

文件对象的生成和文件的创建
  1. /*
  2. * ProcessFileName.java
  3. *
  4. * Created on 2006年8月22日, 下午3:10
  5. *
  6. * 文件对象的生成和文件的创建
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. public class GenerateFile
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         File dirObject = new File("d:\\mydir");
  15.         File fileObject1 = new File("oneFirst.txt");
  16.         File fileObject2 = new File("d:\\mydir", "firstFile.txt");
  17.         System.out.println(fileObject2);
  18.         try
  19.         {
  20.             dirObject.mkdir();
  21.         }catch(SecurityException e)
  22.         {
  23.             e.printStackTrace();
  24.         }
  25.         try
  26.         {
  27.             fileObject2.createNewFile();
  28.             fileObject1.createNewFile();
  29.         }catch(IOException e)
  30.         {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34. }
复制代码
文件名的处理
5 `, b( W# ~: P2 r
  1. /*
  2. * ProcessFileName.java
  3. *
  4. * Created on 2006年8月22日, 下午3:29
  5. *
  6. * 文件名的处理
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. /*
  11. * 文件名的处理
  12. * String getName(); 获得文件的名称,不包含文件所在的路径。
  13. * String getPath(); 获得文件的路径。
  14. * String getAbsolutePath(); 获得文件的绝对路径。
  15. * String getParent(); 获得文件的上一级目录的名称。
  16. * String renameTo(File newName); 按参数中给定的完整路径更改当前的文件名。
  17. * int compareTo(File pathName); 按照字典顺序比较两个文件对象的路径。
  18. * boolean isAbsolute(); 测试文件对象的路径是不是绝对路径。
  19. */
  20. public class ProcesserFileName
  21. {   
  22.     public static void main(String[] args)
  23.     {
  24.         File fileObject1 = new File("d:\\mydir\\firstFile.txt");
  25.         File fileObject2 = new File("d:\\firstFile.txt");
  26.         boolean pathAbsolute = fileObject1.isAbsolute();
  27.         System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
  28.         System.out.println("There are some information of fileObject1's file name:");
  29.         System.out.println("fileObject1: " + fileObject1);  
  30.         System.out.println("fileObject2: " + fileObject2);
  31.         System.out.println("file name: " + fileObject1.getName());
  32.         System.out.println("file path: " + fileObject1.getPath());
  33.         System.out.println("file absolute path: " + fileObject1.getAbsolutePath());
  34.         System.out.println("file's parent directory: " + fileObject1.getParent());
  35.         System.out.println("file's absoulte path: " + pathAbsolute);
  36.         int sameName = fileObject1.compareTo(fileObject2);
  37.         System.out.println("fileObject1 compare to fileObject2: " + sameName);
  38.         fileObject1.renameTo(fileObject2);
  39.         System.out.println("file's new name: " + fileObject1.getName());
  40.     }
  41. }
复制代码
测试和设置文件属性
  1. /*
  2. * SetterFileAttribute.java
  3. *
  4. * Created on 2006年8月22日, 下午3:51
  5. *
  6. * 测试和设置文件属性
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. public class SetterFileAttribute
  11. {
  12.     /*
  13.      * File类中提供的有关文件属性测试方面的方法有以下几种:
  14.      * boolean exists(); 测试当前文件对象指示的文件是否存在。
  15.      * boolean isFile(); 测试当前文件对象是不是文件。
  16.      * boolean isDirectory(); 测试当前文件对象是不是目录。
  17.      * boolean canRead(); 测试当前文件对象是否可读。
  18.      * boolean canWrite(); 测试当前文件对象是否可写。
  19.      * boolean setReadOnly(); 将当前文件对象设置为只读。
  20.      * long length(); 获得当前文件对象的长度。
  21.      */
  22.     public static void main(String[] args)
  23.     {
  24.         File dirObject = new File("d:\\mydir");
  25.         File fileObject = new File("d:\\mydir\\firstFile.txt");
  26.         try
  27.         {
  28.             dirObject.mkdir();
  29.             fileObject.createNewFile();
  30.         }catch(IOException e)
  31.         {
  32.             e.printStackTrace();
  33.         }
  34.         System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
  35.         System.out.println("there are some information about property of file object:");
  36.         System.out.println("file object : " + fileObject);
  37.         System.out.println("file exist? " + fileObject.exists());
  38.         System.out.println("Is a file? " + fileObject.isFile());
  39.         System.out.println("Is a directory?" + fileObject.isDirectory());
  40.         System.out.println("Can read this file? " + fileObject.canRead());
  41.         System.out.println("Can write this fie? " + fileObject.canWrite());
  42.         long fileLen = fileObject.length();
  43.         System.out.println("file length: " +fileLen);
  44.         boolean fileRead = fileObject.setReadOnly();
  45.         System.out.println(fileRead);
  46.         System.out.println("Can read this file? " + fileObject.canRead());
  47.         System.out.println("Can write this fie? " + fileObject.canWrite());
  48.         System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
  49.     }
  50. }
复制代码
文件操作方法
  1. /*
  2. * FileOperation.java
  3. *
  4. * Created on 2006年8月22日, 下午4:25
  5. *
  6. * 文件操作方法
  7. */

  8. package study.iostudy;
  9. import java.io.*;
  10. /*
  11. * 有关文件操作方面的方法有如下几种:
  12. * boolean createNewFile(); 根据当前的文件对象创建一个新的文件。
  13. * boolean mkdir(); 根据当前的文件对象生成一目录,也就是指定路径下的文件夹。
  14. * boolean mkdirs(); 也是根据当前的文件对象生成一个目录,
  15. *                   不同的地方在于该方法即使创建目录失败,
  16. *                   也会成功参数中指定的所有父目录。
  17. * boolean delete(); 删除当前的文件。
  18. * void deleteOnExit(); 当前Java虚拟机终止时删除当前的文件。
  19. * String list(); 列出当前目录下的文件。
  20. */
  21. public class FileOperation
  22. {
  23.     public static void main(String[] args)
  24.     {
  25.         File fileObject = new File("d:\\mydir", "firstFile.txt");
  26.         File dirObject1 = new File("d:\\mydir\\01");
  27.         File dirObject2 = new File("d:\\mydir\\02");
  28.         File dirObject3 = new File("d:\\mydir");
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  30.         System.out.println("file object: " + fileObject);
  31.         System.out.println("dir object 1: " + dirObject1);
  32.         System.out.println("dir object 2" + dirObject2);
  33.         try
  34.         {
  35.             dirObject1.mkdir();
  36.             dirObject2.mkdirs();
  37.         }catch(SecurityException e)
  38.         {
  39.             e.printStackTrace();
  40.         }
  41.         try
  42.         {
  43.             fileObject.createNewFile();
  44.         }catch(IOException e)
  45.         {
  46.             e.printStackTrace();
  47.         }
  48.         String[] files = dirObject2.list();
  49.         for (int i = 0; i < files.length; i++)
  50.         {
  51.             System.out.println("list files in myhdir: " + files[i]);
  52.         }
  53.         System.out.println("dir object 1 exist? " + dirObject1.exists());
  54.         System.out.println("dir object 2 exist? " + dirObject2.exists());
  55.         boolean dir1Del = dirObject1.delete();
  56.         dirObject2.deleteOnExit();
  57.         System.out.println("dir object 1 exist? " + dirObject1.exists());
  58.         System.out.println("dir object 2 exist? " + dirObject2.exists());
  59.     }
  60. }
复制代码
找出一个目录下所有的文件
  1. /*
  2. * SearchFile.java
  3. *
  4. * Created on 2006年8月22日, 下午4:45
  5. *
  6. * 找出一个目录下所有的文件
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. public class SearchFile
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         File dirObject = new File("D:\\aa");
  15.         Filter1 filterObj1 = new Filter1("HTML");
  16.         Filter2 filterObj2 = new Filter2("Applet");
  17.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  18.         System.out.println("list HTML files in directory: " + dirObject);
  19.         String[] filesObj1 = dirObject.list(filterObj1);
  20.         for (int i = 0; i < filesObj1.length; i++)
  21.         {
  22.             File fileObject = new File(dirObject, filesObj1[i]);
  23.             System.out.println(((fileObject.isFile())
  24.                 ? "HTML file: " : "sub directory: ") + fileObject);
  25.         }
  26.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  27.         String[] filesObj2 = dirObject.list(filterObj2);
  28.         for (int i = 0; i < filesObj2.length; i++)
  29.         {
  30.             File fileObject = new File(dirObject, filesObj2[i]);
  31.             System.out.println(((fileObject.isFile())
  32.                 ? "htm file: " : "sub directory: ") + fileObject);
  33.         }
  34.     }
  35. }

  36. class Filter1 implements FilenameFilter
  37. {
  38.    String fileExtent;
  39.     Filter1(String extentObj)
  40.     {
  41.         fileExtent = extentObj;
  42.     }

  43.     public boolean accept(File dir, String name)
  44.     {
  45.         return name.endsWith("." + fileExtent);
  46.     }
  47. }

  48. class Filter2 implements FilenameFilter
  49. {
  50.     String fileName;
  51.     Filter2(String fileName)
  52.     {
  53.         this.fileName = fileName;
  54.     }

  55.     public boolean accept(File dir, String name)
  56.     {
  57.         return name.startsWith(fileName + ".");
  58.     }
  59. }
复制代码
文件内容的拷贝(任意文件)忽悠,忽悠社区,忽悠论坛.- U- e4 l  r/ m' G2 G  D1 }
  1. /*
  2. * CopyFileContent.java
  3. *
  4. * Created on 2006年8月22日, 下午5:37
  5. *
  6. * 文件内容的拷贝(任意文件)
  7. */

  8. package study.iostudy;
  9. import java.io.*;
  10. public class CopyFileContent
  11. {
  12.     static void copyContent(FileInputStream inObj, FileOutputStream outObj)
  13.    {
  14.         int copyLen;
  15.         byte[] copyBuf = new byte[1024];
  16.         try
  17.         {
  18.             while ((copyLen = inObj.read(copyBuf, 0, 1024)) != -1)
  19.             {
  20.                 String copyStr = new String(copyBuf);
  21.                 System.out.println(copyStr);
  22.                 outObj.write(copyBuf, 0, copyLen);
  23.             }
  24.         }catch(IOException e)
  25.         {
  26.             System.out.println("error: " + e);
  27.         }
  28.     }
  29.    
  30.     public static void main(String[] args)
  31.     {
  32.         String secondFileName = "d:\\mydir\\secondFile.wmv";
  33.         String thirdFileName = "d:\\mydir\\thirdFile.wmv";
  34.         File fileObject = new File(thirdFileName);
  35.         FileInputStream inStream;
  36.         FileOutputStream outStream;
  37.         try
  38.         {
  39.             fileObject.createNewFile();
  40.             inStream = new FileInputStream(secondFileName);
  41.             outStream = new FileOutputStream(thirdFileName);
  42.             copyContent(inStream, outStream);
  43.         }catch(FileNotFoundException e)
  44.         {
  45.             e.printStackTrace();
  46.         }catch(IOException e)
  47.         {
  48.             e.printStackTrace();
  49.         }
  50.       
  51.     }
  52. }
复制代码
文件随机访问www.huuoo.com1 Q: x5 t6 j) ]/ u
  1. /*
  2. * RandomFile.java
  3. *
  4. * Created on 2006年8月22日, 下午9:47
  5. *
  6. * 文件随机访问
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. /*
  11. * 文件随机访问的方法
  12. *
  13. * void seek(long pos); 将文件指针移动到参数指定的位置。
  14. * long getFilePointer(); 得到当前文件指针的位置。
  15. * int skipBytes(int n); 将文件指针向前移动参数指定的n个字节。
  16. * String readLine(); 从当前文件指定位置读取一行。
  17. *
  18. */
  19. public class RandomFile
  20. {
  21.     public static void main(String[] args)
  22.     {
  23.         String tempStr;
  24.         int fileLines = 0;
  25.         long pointerLast = 0;
  26.         try
  27.         {
  28.             RandomAccessFile inObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
  29.             while (inObj.readLine() != null)
  30.                 fileLines++;
  31.             for (int i =0; i < fileLines / 2; i++)
  32.             {
  33.                 inObj.seek(2 * i);
  34.                 tempStr = inObj.readLine();
  35.                 System.out.println(tempStr);
  36.             }
  37.             pointerLast = inObj.getFilePointer();
  38.         }catch(IOException e)
  39.         {
  40.             e.printStackTrace();
  41.         }
  42.         try
  43.         {
  44.             RandomAccessFile fileObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
  45.             String writeStr = new String("Insert a string!");
  46.             fileObj.seek(pointerLast);
  47.             fileObj.writeChars(writeStr);
  48.         }catch(IOException e)
  49.         {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53. }
复制代码
字符流处理
  1. /*
  2. * ProcesserCharacterStream.java
  3. *
  4. * Created on 2006年8月23日, 上午8:02
  5. *
  6. * 字符流处理
  7. *
  8. * java.io包中加入了专门用于字符流处理的类,这些类都是Reader和Writer类的子类,
  9. * Reader和Writer是两个抽象类,只提供了一系列用于字符流处理的接口,不能生成这
  10. * 两个类的实例。
  11. * java.io包中用于字符流处理的最基本的类是InputStreamReader和OutputStreamWriter,
  12. * 用来在字节流和字符流之间作为中介。
  13. *
  14. * 下面是InputStreamReader类和OutputStreamWriter类的常用方法:
  15. *
  16. * public InputStreamReader(InputStream in)
  17. * 根据当前平台缺省的编码规范,基于字节流in生成一个输入字符流。
  18. * public InputStreamReader(InputStream in, String sysCode)throws UnSupportedEncodingException
  19. * 按照参数sysCode指定的编码规范,基于字节流in构造输入字符流,如果不支持参数sysCode中指定的编码规范,就会产生异常。
  20. * public OutputStreamWriter(OutputStream out)
  21. * 根据当前平台缺省的编码规范,基于字节流out生成一个输入字符流。
  22. * public OutputStreamWriter(OutputStream out, String sysCode) throws UnsupportedEncodingException
  23. * 按照参数sysCode指定的编码规范,基于字节流out构造输入字符流,如果不支持参数sysCode中指定的编码规范,就会产生异常。
  24. * public String getEncoding()
  25. * 获得当前字符流使用的编码方式。
  26. * public void close() throws IOException
  27. * 用于关闭流。
  28. * public int read() throws IOException
  29. * 用于读取一个字符。
  30. * public int read(char[] cbuf, int off, int len)
  31. * 用于读取len个字符到数组cbuf的索引off处。
  32. * public void write(char[] cbuf, int off, int len) throws IOException
  33. * 将字符数组cbuf中从索引off处开始的len个字符写入输出流。
  34. * public void write(int c) throws IOException
  35. * 将单个字符写入输入流。
  36. * public void write(String str, int off, int len) throws IOException
  37. * 将字符串str中从索引off位置开始的ltn个字符写入输出流。
  38. *
  39. * 此外,为了提高字符流处理的效率,在Java语言中,引入了BufferedReader和BufferWriter类,这两个类对字符流进行块处理。
  40. * 两个类的常用方法如下:
  41. * public BufferedReader(Reader in)
  42. * 用于基于普通字符输入流in生成相应的缓冲流。
  43. * public BufferedReader(Reader in, int bufSize)
  44. * 用于基于普通字符输入流in生成相应的缓冲流,缓冲区大小为参数bufSize指定。
  45. * public BufferedWriter(Writer out)
  46. * 用于基于普通字符输入流out生成相应的缓冲流。
  47. * public BufferedWriter(Writer out, int bufSize)
  48. * 用于基于普通字符输入流out生在相应缓冲流,缓冲流大小为参数bufSize指定。
  49. * public String readLine() throws IOException
  50. * 用于从输入流中读取一行字符。
  51. * public void newLine() throws IOException
  52. * 用于向字符输入流中写入一行结束标记,值得注意的是,该标记不是简单的换行符"\n",而是系统定义的属性line.separator。
  53. */
  54. package study.iostudy;
  55. import java.io.*;
  56. public class ProcesserCharacterStream
  57. {
  58.     public static void main(String[] args)
  59.             throws FileNotFoundException, IOException
  60.     {
  61.         String lineStr;
  62.         FileInputStream fileInStream;
  63.         InputStreamReader inputReader;
  64.         BufferedReader bufReader;
  65.         FileOutputStream fileOutStream;
  66.         OutputStreamWriter outputWriter;
  67.         BufferedWriter bufWriter;
  68.         fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
  69.         inputReader = new InputStreamReader(fileInStream);
  70.         bufReader = new BufferedReader(inputReader);
  71.         System.out.println("------------------------------------------------");
  72.         System.out.println("There are file content before modify:");
  73.         while ((lineStr = bufReader.readLine()) != null)
  74.             System.out.println(lineStr);
  75.         bufReader.close();
  76.         inputReader.close();
  77.         fileInStream.close();
  78.         fileOutStream = new FileOutputStream("d:\\mydir\\secondFile.txt");
  79.         outputWriter = new OutputStreamWriter(fileOutStream);
  80.         bufWriter = new BufferedWriter(outputWriter);
  81.         String newStr = new String("Modify the file ! \r\nThis is a nice thing. \r\nWe can write anything.");
  82.         bufWriter.write(newStr, 0, newStr.length());
  83.         System.out.println(newStr);
  84.         bufWriter.close();
  85.         outputWriter.close();
  86.         fileOutStream.close();
  87.         fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
  88.         inputReader = new InputStreamReader(fileInStream);
  89.         bufReader = new BufferedReader(inputReader);
  90.         System.out.println("------------------------------------------------");
  91.         System.out.println("There are file content after modify:");
  92.         while ((lineStr = bufReader.readLine()) != null)
  93.             System.out.println(lineStr);
  94.         bufReader.close();
  95.         inputReader.close();
  96.         fileInStream.close();
  97.     }
  98. }
复制代码
接收键盘输入数据
  1. /*
  2. * OutputKeyPress.java
  3. *
  4. * Created on 2006年8月23日, 上午9:27
  5. *
  6. * 接收键盘输入数据
  7. */
  8. package study.iostudy;
  9. import java.io.*;
  10. public class OutputKeyPress
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         System.out.println("This is a example about acceptance of keyboard.");
  15.         String tempStr = "0";
  16.         try
  17.         {
  18.             InputStreamReader inputReader;
  19.             BufferedReader bufReader;
  20.             inputReader = new InputStreamReader(System.in);
  21.             bufReader = new BufferedReader(inputReader);
  22.             tempStr = bufReader.readLine();
  23.             System.out.println("Input num is: " + tempStr);
  24.         }catch(IOException e)
  25.         {
  26.             e.printStackTrace();
  27.         }
  28.         int n = Integer.parseInt(tempStr);
  29.         int nultiNum = 1;
  30.         for (int i =1; i <= n; i++)
  31.         {
  32.             nultiNum *= i;
  33.         }
  34.         System.out.println("multiply of input number is: " + nultiNum);
  35.     }   
  36. }
复制代码
过滤流
' R4 H4 D- q* u6 i/ q! r) a5 L忽悠社区是综合性社区网站,将最新、最快、最专业的资讯、新闻,图片,视频奉献给所有爱好者。
  1. /*
  2. * FilterStream.java
  3. *
  4. * Created on 2006年8月23日, 上午9:40
  5. *
  6. * 过滤流
  7. *
  8. * 过滤流在读/写数据的同时可以对数据进行处理,并提供了同步机制,
  9. * 这样在同一时刻只有一个线程可以访问一个I/O流。在java.io包中,
  10. * FilterInputStream和FilterOutputStream类是所有过滤输入流和
  11. * 输出流的父类,它们是抽象类,本身不能生成任何实例,在这两上类
  12. * 之下,分别实现了几物特殊的过滤输入流和输出流,利用这些特殊输
  13. * 入流和输出流的实例可以进行流处理。
  14. *
  15. * 下面介绍几个过滤输入流和输出流的子类:
  16. *
  17. * BufferedInputStream 和 BufferedOutputStream
  18. * 这两个类实现了带缓冲的过滤流,将任意的输入流和输出流绑定到缓
  19. * 冲流上就会提高性能。
  20. * 对于BufferedInputStream类,当系统读取数据时,数据按块读入缓
  21. * 冲区,随后读操作直接访问缓冲区。使用BufferedOutputStream进行
  22. * 输出时,数据首先写入缓冲区,当缓冲区满时,缓冲区中的数据写入
  23. * 连接的输出流,BufferedOutputStream类提供的方法flush()可以强
  24. * 制将缓冲区的内容全部写入输出流。
  25. *
  26. * DataInputStream 和 DataOutputStream
  27. * 这两个类不仅能读写数据流,而且能读写各种各样的Java语言本身固
  28. * 有的数据类型,如int、float等。
  29. *
  30. * PushbackInputStream
  31. * PushbackInputStream类提供了方法将刚读过的字节退回到输入流中,
  32. * 后面的内容就可以用到该字节。
  33. */

  34. package study.iostudy;
  35. import java.io.*;
  36. public class FilterStream
  37. {
  38.     public static void main(String[] args)
  39.     {
  40.         try
  41.         {
  42.             FileInputStream inStream;
  43.             FileOutputStream outStream;
  44.             BufferedInputStream bufInObj;
  45.             BufferedOutputStream bufOutObj;
  46.             DataInputStream dataInObj;
  47.             PushbackInputStream pushObj;
  48.             byte[] tempBuf = new byte[1024];
  49.             int copyLen;
  50.             inStream = new FileInputStream("d:\\mydir\\secondFile.txt");
  51.             outStream = new FileOutputStream("d:\\mydir\\thirdFile.txt");
  52.             bufInObj = new BufferedInputStream(inStream);
  53.             bufOutObj = new BufferedOutputStream(outStream);
  54.             dataInObj = new DataInputStream(inStream);
  55.             System.out.println(dataInObj.readBoolean());
  56.             while ((copyLen = bufInObj.read(tempBuf, 0, 1024)) != -1)
  57.             {
  58.                 String copyStr = new String(tempBuf);
  59.                 System.out.println(copyStr);
  60.                 bufOutObj.write(tempBuf, 0, copyLen);
  61.                 bufOutObj.flush();
  62.             }
  63.             int pushData;
  64.             byte[] pushByte = {'o', 'k'};
  65.             pushObj = new PushbackInputStream(
  66.                     new FileInputStream("d:\\mydir\\thirdFile.txt"), 1000);
  67.             while ((pushData = pushObj.read()) != -1)
  68.             {
  69.                 if (Character.isLetter((char)pushData))
  70.                 {
  71.                     System.out.print((char)pushData);
  72.                 }
  73.                 else
  74.                 {
  75.                     System.out.println();
  76.                     pushObj.unread(pushByte);
  77.                 }
  78.             }
  79.         }catch(FileNotFoundException e)
  80.         {
  81.             System.out.println("File not found or persission denied.");
  82.         }catch(IOException e)
  83.         {
  84.             System.out.println("error:" + e);
  85.         }
  86.     }
  87.     /*
  88.      * 在上面的程序中,我们首先声明了FileInputStream类对象inStream和
  89.      * FileOutputStream类的对象outStream,接着声明了BufferInputStream
  90.      * 类对象bufObj、BufferedOutputStream类对象bufOutObj、
  91.      * DataInputStream类对象dataInObj以及PushbackInputStream类对象pushObj,
  92.      * 在try代码块中对上面这些对象进行初始化,程序的目的是通过BufferedInputStream
  93.      * 类对象bufInObj和BufferedOutputStream类对象bufOutObj将secondFile.txt
  94.      * 文件中内容输出到屏幕,并将该文件的内容写入thirdFile.txt文件中,值得注意的是,
  95.      * 将secondFile.txt文件中的内容输出之前,程序中使用
  96.      * "System.out.println(dataInObj.readBoolean());" 语句根据readBoolean()结果
  97.      * 输出了true,而secondFile.txt文件开始内容为“Modify”,和一个字符为M,
  98.      * 因此输出的文件内容没有“M”字符,thirdFile.txt文件中也比secondFile.txt
  99.      * 文件少第一个字符“M”。随后,通过PushbackInputStream类对象pushObj读取
  100.      * thirdFile.txt文件中的内容,输出读到的字符,当读到的不是字符,输出回车,将字符
  101.      * 数组pushByte写回到thirdFile.txt文件中,也就是“ok”写回文件中。
  102.      */

  103. }
复制代码
顺序输入流
  1. /*
  2. * SequenceStream.java
  3. *
  4. * Created on 2006年8月23日, 上午10:55
  5. *
  6. * 顺序输入流
  7. *
  8. * java.io包中提供了SequenceInputStream类,用于将多个输入流顺序连接起来,
  9. * 使它们看起来就像一个较长的流。
  10. */
  11. package study.iostudy;
  12. import java.io.*;
  13. public class SequenceStream
  14. {
  15.     public static void main(String[] args)
  16.     {
  17.         FileInputStream fileStream1, fileStream2;
  18.         try
  19.         {
  20.             String allStr;
  21.             fileStream1 = new FileInputStream("d:\\mydir\\secondFile.txt");
  22.             fileStream2 = new FileInputStream("d:\\mydir\\thirdFile.txt");
  23.             SequenceInputStream seqStream = new SequenceInputStream(
  24.                     fileStream1, fileStream2);
  25.             BufferedInputStream bufObj = new BufferedInputStream(seqStream);
  26.             byte[] bufByte = new byte[1024];
  27.             while (bufObj.read(bufByte, 0, 1024) != -1)
  28.             {
  29.                 String tempStr = new String(bufByte);
  30.                 System.out.println(tempStr);
  31.             }
  32.         }catch(FileNotFoundException e)
  33.         {
  34.             System.out.println("File not found or no permission.");
  35.         }catch(IOException e)
  36.         {
  37.             System.out.println("error:" + e);
  38.         }
  39.     }
  40. }
复制代码
对象串行化
  1. /*
  2. * SerializableObject.java
  3. *
  4. * Created on 2006年8月23日, 上午11:26
  5. *
  6. * 对象串行化
  7. *   对象通过写出描述自己状态的数值来记录自己,这个过程叫做对象串行化。对象的寿命通
  8. * 常是随着生成该对象的程序的终止而终止,在有些情况下,需要将对象的状态保存下来,然后
  9. * 在必要的时候将对象恢复,值得注意的是,如果变量是另一个对象的引用,则引用的对象也要
  10. * 串行化,串行化是一个递归的过程,可能会涉及到一个复杂树结构的串行化,比如包括原有对
  11. * 象,对象的对象等。
  12. *   在java.io包中,接口Serializable是实现对象串行化的工具,只有实现了Serializable
  13. * 的对象才可以被串行化。Serializable接口中没有任何的方法,当一个类声明实现Seriali-
  14. * zable接口时,只是表明该类遵循串行化协议,而不需要实现任何特殊的方法。
  15. *   在进行对象串行化时,需要注意将串行化的对象和输入、输出流联系起来,首先通过对
  16. * 象输出流将对象状态保存下来,然后通过对象输入流将对象状态恢复。
  17. */

  18. package study.iostudy;

  19. import java.io.*;

  20. class Book implements Serializable
  21. {
  22.     String isbn;
  23.     String name;
  24.     int page;
  25.     String type;
  26.     public Book(String isbn, String name, int page, String type)
  27.     {
  28.         this.isbn = isbn;
  29.         this.name = name;
  30.         this.page = page;
  31.         this.type = type;
  32.     }
  33. }

  34. public class SerializableObject implements Serializable
  35. {
  36.     public static void main(String[] args)
  37.             throws IOException, ClassNotFoundException
  38.     {
  39.         Book bookObj = new Book("7-02-016450-1", "Java", 218, "programming");
  40.         FileOutputStream fileOStream = new FileOutputStream("temp.ser");
  41.         ObjectOutputStream objOutStream = new ObjectOutputStream(fileOStream);
  42.         try
  43.         {
  44.             objOutStream.writeObject(bookObj);
  45.             objOutStream.close();
  46.         }catch(IOException e)
  47.         {
  48.             e.printStackTrace();
  49.         }
  50.         bookObj = null;
  51.         FileInputStream fileInStream = new FileInputStream("temp.ser");
  52.         ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
  53.         try
  54.         {
  55.             bookObj = (Book)objInStream.readObject();
  56.             objInStream.close();
  57.         }catch(IOException e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.         System.out.println("------------------------------------------------");
  62.         System.out.println("There are information about book:");
  63.         System.out.println("ISBN Number: " + bookObj.isbn);
  64.         System.out.println("Book Name: " + bookObj.name);
  65.         System.out.println("Book Page: " + bookObj.page);
  66.         System.out.println("Book Type: " + bookObj.type);
  67.     }
  68. }
复制代码

什么样的彩色数码短版印刷机适合您

提示: 作者被禁止或删除 内容自动屏蔽

vision

提示: 该帖被管理员或版主屏蔽

jeijls

提示: 该帖被管理员或版主屏蔽
返回列表
高级回复 | 发新话题
B Color Image Link Quote Code Smilies
换一个