InputStream & Reader


  • InputStream(字节流),如下是InputStream的层次结构:

public class audioInputStream {
public static void playWAV(){
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("SourceFile/1.wav"));
byte[] samples = getSamples(stream); //将音频转化为字节数组
InputStream in = new ByteArrayInputStream(samples);
play(in,stream.getFormat()); //播放音频文件
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
private static byte[] getSamples(AudioInputStream stream){
int length = (int) (stream.getFrameLength()*stream.getFormat().getFrameSize());
byte[] samples = new byte[length];
DataInputStream in = new DataInputStream(stream);
try {
in.readFully(samples);
System.out.println(length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return samples;
}
private static void play(InputStream stream, AudioFormat format){
int bufferSize = format.getFrameSize()* Math.round(format.getSampleRate()/10);
byte[] buffer = new byte[bufferSize];
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufferSize);
line.start();
int numBytesRead = 0;
while(numBytesRead != -1){
numBytesRead = stream.read(buffer, 0, buffer.length);
if(numBytesRead != -1){
line.write(buffer, 0, numBytesRead);
//System.out.println(numBytesRead);
}
}
line.drain();
line.close();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  • ByteArrayInputStream:流的来源并不一定是文件,也可以是内存中的一块空间,例如一个字节数组。ByteArrayInputStream就是将字节数组当作流输入来源的类。
    • new ByteArrayInputStream(byte[] buf, int offset, int length)
    • new ByteArrayInputStream(byte[] buf)
  • FileInputStream:从文件系统或者终端获取输入信息,构造函数如下:
    • new FileInputStream(File file)
    • new FileInputStream(FileDescriptor fdObj)
    • new FileInputStream(String name)
try {
FileInputStream fis = new FileInputStream("SourceFile/employee");
try {
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  • Reader(字符输入流),其层次结构如下:

  • BufferedReader:字符读入,默认拥有8192字符的缓冲区,当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取。

    • 构造方法有两个,size表示设置缓冲区大小,默认为8192:

      • new BufferedReader(Reader in)
      • new BufferedReader(Reader in, int size)
    • //System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换,
      //然后再使用BufferedReader为其增加缓冲功能。
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String content = null;
      try {
      while(!(content = br.readLine()).equals("quit")){
      System.out.println(content);
      }
      br.close();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
  • CharArrayReader:从字符数组中读取信息
    • 构造方法有两个:

      • new CharArrayReader(char[] buf)
      • new CharArrayReader(char[] buf, int offset, int length)
    • 相关说明见CharArrayReader类链接。
  • InputStreamReader:将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码。
    • 构造方法:

      • new InputStreamReader(InputStream in)
      • new InputStreamReader(InputStream in, Charset cs)
      • new InputStreamReader(InputStream in, CharsetDecoder dec)
      • new InputStreamReader(InputStream in, String charsetName)
    • 相关说明见InputStreamReader类链接。
  • StringReader:读入String字符串。
    • 构造方法

      • new StringReader(String str)
    • 相关代码
    • StringReader sr = new StringReader("dsfasdfasdfasd");
      char[] chars = new char[5]; //每次读取5个字符
      int length = 0;
      try {
      while((length = sr.read(chars)) != -1){
      String strRead = new String(chars, 0, length).toUpperCase();
      System.out.println(strRead);
      }
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

Java中读文件操作的更多相关文章

  1. Java中的文件操作(一)RandomAccessFile

    今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...

  2. Java中的文件操作

    在使用计算机编程中,常常会用到对于文件的操作,以下是我对于Java中文件的相关内容学习之后的一个总结和在学习过程中遇到的一些问题. 一.什么是文件 对于文件进行操作,首先我们要知道什么是文件.在此之前 ...

  3. java的读文件操作

    java读取文件内容,可以作如下理解: 首先获得一个文件句柄,File file = new File():file即为文件句柄.两人之间联通电话网络了,就可以开始打电话了. 通过这条线路读取甲方的信 ...

  4. 关于文件的INode与Java中的文件操作接口

    本文由作者周梁伟授权网易云社区发布. 近日做的项目中涉及到多进程共同读写多个文件的问题,文件名和最后修改时间都是可能会被频繁修改的,因而识别文件的唯一性会产生相当的麻烦,于是专门再学习了一下文件系统对 ...

  5. Java中写文件操作

    OutputStream 和 Writer OutputStream类(直接操作byte数组) 该类是字节输出流的抽象类,定义了输出流的各种操作方法.如下图是OutputStream的层次结构: By ...

  6. 3,Java中的文件IO流

    1,File类 ··· 概念:File对象可以表示一个文件或目录.可以对其进行增删改查. ··· 常用方法:     File f = new File(".");     判断是 ...

  7. Java中的IO操作和缓冲区

    目录 Java中的IO操作和缓冲区 一.简述 二.IO流的介绍 什么是流 输入输出流的作用范围 三.Java中的字节流和字符流 字节流 字符流 二者的联系 1.InputStreamReader 2. ...

  8. 第32课 Qt中的文件操作

    1. Qt的中IO操作 (1)Qt中IO操作的处理方式 ①Qt通过统一的接口简化了文件和外部设备的操作方式 ②Qt中的文件被看作一种特殊的外部设备 ③Qt中的文件操作与外部设备的操作相同 (2)IO操 ...

  9. 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作

    原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...

随机推荐

  1. 八大排序算法Java

    目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...

  2. nodejs学习之events

    在node里许多对象都发出事件:一个net.Server对象每次一个连接到来,都发出一个事件,一个fs.readStream对象在文件打开时放出一个事件.所有能放出事件的对象都是event.Event ...

  3. Django数据导入

    从网上下载的一些数据,excel表格,xml文件,txt文件等有时候我们想把它导入数据库,应该如何操作呢? 以下操作符合 Django版本为 1.6 ,兼顾 Django 1.7, Django 1. ...

  4. Struts 2的拦截器(Interceptor)总结

     什么是Struts 2拦截器? 从软件构架上来说,拦截器是实现了面向方面编程的组件.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象 ...

  5. Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |

    Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...

  6. JPEG文件结构

    JPEG文件由八个部分组成,每个部分的标记字节为两个,首字节固定为:0xFF,当然,准许在其前面再填充多个0xFF,以最后一个为准.下面为各部分的名称和第二个标记字节的数值,用ultraedit的16 ...

  7. Hadoop学习笔记—16.Pig框架学习

    一.关于Pig:别以为猪不能干活 1.1 Pig的简介 Pig是一个基于Hadoop的大规模数据分析平台,它提供的SQL-LIKE语言叫Pig Latin,该语言的编译器会把类SQL的数据分析请求转换 ...

  8. 慎重管理SQL Server服务的登录(启动)账户和密码

    今天是大年初三,先跟大家拜个年,祝大家新年快乐.今天处理了一个alwaysOn问题——辅助副本因为磁盘空间不足一直显示[未同步——可疑],在日志中可以看到数据库处于挂起状态,与主副本失去同步.原以为只 ...

  9. spring事务管理器设计思想(二)

    上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...

  10. HTTPS那些事(二)SSL证书(转载)

    原创地址:http://www.guokr.com/post/116169/   从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密 ...