工作后,使用的技术随着项目的变化而变化,时而C#,时而Java,当然还有其他一些零碎的技术。总体而言,C#的使用时间要更长一些,其次是 Java。我本身对语言没有什么倾向性,能干活的语言,就是好语言。而且从面向对象的角度来看,我觉得C#和Java对我来说,没什么区别。
这篇文章主要回顾Java中和I/O操作相关的内容,I/O也是编程语言的一个基础特性,Java中的I/O分为两种类型,一种是顺序读取,一种是随机读取。
我们先来看顺序读取,有两种方式可以进行顺序读取,一种是InputStream/OutputStream,它是针对字节进行操作的输入输出流;另外一种是Reader/Writer,它是针对字符进行操作的输入输出流。
下面我们画出InputStream的结构
- FileInputStream:操作文件,经常和BufferedInputStream一起使用
- PipedInputStream:可用于线程间通信
- ObjectInputStream:可用于对象序列化
- ByteArrayInputStream:用于处理字节数组的输入
- LineNumberInputStream:可输出当前行数,并且可以在程序中进行修改
下面是OutputStream的结构
- PrintStream:提供了类似print和println的接口去输出数据
下面我们来看如何使用Stream的方式来操作输入输出
- 使用BufferedOutputStream复制文件
- public static void copyFilebyBufferedOutputStream(File file)throws IOException
- {
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- try
- {
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- fos = new FileOutputStream(file.getName() + ".bak");
- bos = new BufferedOutputStream(fos);
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)
- {
- bos.write(buffer, 0, bytesRead);
- }
- bos.flush();
- }
- catch(Exception ex)
- {
- System.out.println("Error occurs during copying " + file.getAbsoluteFile());
- }
- finally
- {
- if (fis != null) fis.close();
- if (bis != null) bis.close();
- if (fos != null) fos.close();
- if (bos != null) bos.close();
- }
- }
- 使用FileOutputStream复制文件
- public static void copyFileByFileOutputStream(File file) throws IOException
- {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- try
- {
- fis = new FileInputStream(file);
- fos = new FileOutputStream(file.getName() + ".bak");
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- while((bytesRead = fis.read(buffer,0,buffer.length)) != -1)
- {
- fos.write(buffer, 0, bytesRead);
- }
- fos.flush();
- }
- catch(Exception ex)
- {
- System.out.println("Error occurs during copying " + file.getAbsoluteFile());
- }
- finally
- {
- if (fis != null) fis.close();
- if (fos != null) fos.close();
- }
- }
- 使用BufferedInputStream读取文件
- public static byte[] readFileByBufferedInputStream(File file) throws Exception
- {
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- try
- {
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)
- {
- output.write(buffer, 0, bytesRead);
- }
- }
- catch(Exception ex)
- {
- System.out.println("Error occurs during reading " + file.getAbsoluteFile());
- }
- finally
- {
- if (fis != null) fis.close();
- if (bis != null) bis.close();
- if (output != null) output.close();
- }
- return output.toByteArray();
- }
- 使用FileInputStream读取文件信息
- public static byte[] readFileByFileInputStream(File file) throws IOException
- {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- FileInputStream fis = null;
- try
- {
- fis = new FileInputStream(file);
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1)
- {
- output.write(buffer, 0, bytesRead);
- }
- }
- catch(Exception ex)
- {
- System.out.println("Error occurs during reading " + file.getAbsoluteFile());
- }
- finally
- {
- if (fis !=null) fis.close();
- if (output !=null) output.close();
- }
- return output.toByteArray();
- }
1 2 下一页>>查看全文
内容导航 第 1 页:Java回顾之I/O(1) | 第 2 页:Java回顾之I/O(2) |
原文:Java回顾之I/O(1) 返回开发首页