Java IO Stream

ref: http://www.studytonight.com/java/java-io-stream.php

IO Stream

Java performs I/O through Streams. A stream is linked to physical layer by java I/O system to make input and

output operation in java. In general, a stream means continuous flow of data. Streams are clean way to deal with input/output without having every part of your code understand the physical

Java encapsulated Stream under java.io package. Java defined two types if stream:

1. Byte Stream:   handle input/output of byte

2. Character Stream:  handle input/output of characters. Character stream use Unicode and therefore can be internationalized

Byte Stream Classes

Byte Stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream

## These two abstract classes have several concrete classes that handle various devices

such as disk files, network connection etc.

Some important Byte stream classes:

stream class            description

---------------------------------------------------------------------------------------------------------------------------------------

BufferedInputStream        used for Buffered input Stream

BufferedOutputStream        used for Buffered output stream

DataInputStream          contains method for reading java standard datatype

DataOutputStream          an output stream that contain method for writting java standard data type

FileInputStream           input stream that reads from a file

FileOutputStream           output stream that write to a file

InputStream              Abstract class that describe stream input

OutputStream            Abstract class that describe stream output

PrintStream             output stream that contain print() and println() mehod

The above classes define several key methods. Two most important are:

read(): read byte of data

write(): write byte data

***************************************************************************

Character Stream Classes

character stream is also defined by using two abstract class at the top of hierarchy, that are Reader and writer

These two abstract classes have several  concrete classes that handle unicode character

stream class            description

---------------------------------------------------------------------------------------------------------------------------------------

BufferedReader            handles buffered input stream        

BufferedWriter             handles buffered output stream

InputStreamReader           input stream that translate byte to character

OutputStreamWriter          output stream that translate character to byte

FileReader                input stream reads from file 

FileWriter                 output stream that writes to file

Reader                         abstract class that define character stream input

Writer                 abstract class that define character stream output

PrintWriter                  output stream that contain print() and println() method

Reading Console Input

we use BufferedReader class obj to read inputs from keyboard

`` BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Reading Characters

read() method is used with BufferedReader object to read characters. As this function returns integer type

we need to use typecasting to convert it to char type

`` int read() throws IOException

class CharRead
{
 public static void main( String args[])
 {
  BufferedReader br = new Bufferedreader(new InputstreamReader(System.in));
  char c = (char)br.read();       //Reading character
 }
}

Reading Strings

readLine() function with BufferedReader class

`` String readLine() throw IOException

`` String line = br.readLine();

import java.io.*;
class MyInput
{
 public static void main(String[] args)
 {
  String text;
  InputStreamReader isr = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(isr);
  text = br.readLine();          //Reading String
  System.out.println(text);
 }
}

Program to read from a file using BufferedReader class

import java. Io *;
class ReadTest
{
 public static void main(String[] args)
 {
  try
  {
   File fl = new File("d:/myfile.txt");
   BufferedReader br = new BufferedReader(new FileReader(fl)) ;
   String str;
   while ((str=br.readLine())!=null)
   {
    System.out.println(str);
   }
   br.close();
   fl.close();
  }
  catch (IOException  e)
  { e.printStackTrace(); }
 }

Program to write to a File using FileWriter class

import java. Io *;
class WriteTest
{
 public static void main(String[] args)
 {
  try
  {
   File fl = new File("d:/myfile.txt");
   String str="Write this string to my file";
   FileWriter fw = new FileWriter(fl) ;
   fw.write(str);
   fw.close();
   fl.close();
  }
  catch (IOException  e)
  { e.printStackTrace(); }
 }
}
时间: 2024-10-10 22:11:26

Java IO Stream的相关文章

Java学习记录(补充八:Date类;Java流(Stream),文件(File)和IO)

Date类,Calendar类package Box1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; //Date类 public class DateTest { public static void main(String[] args) { Date

java.io.IOException: Attempted read from closed stream

代码如下,执行的时候提示“java.io.IOException: Attempted read from closed stream.” @Test public void test_temp(){ String url="http://ssov1.59iedu.com/login?TARGET=http://med.ihbedu.com:80/gateway/web/sso/auth&js&callback=loginThen&1470491151264&no

java.io.InvalidClassException local class incompatible: stream classdesc serialVersionUID

现象: java.io.InvalidClassException: com.engine.data.User; local class incompatible: stream classdesc serialVersionUID = -6012532569298149921, local class serialVersionUID = 6087477983556853561 解决方案: 将本地的序列化的类中的版本号(serialVersionUID )改成和远程中一样 从上列异常中可以看出

java.io.IOException: Stream closed 异常的原因和处理

java.io.IOException: Stream closed 多个线程索引同一个input  stream,当某一个thread在执行完之后,把这个inputstream关闭了:而此时正在从这个input  stream流中读取信息的线程就会抛出  java.io.IOException:  Stream  closed  异常. 终于找到这个异常的根源所在,原来是两个页面同时调用一个jsp,这个jsp中的内建对象out在执行out.close()时发生的异常,也就是当某一个thread

java.io.IOException: java.io.EOFException: Unexpected end of input stream错误

报错现象: Diagnostic Messages for this Task:Error: java.io.IOException: java.io.EOFException: Unexpected end of input stream at org.apache.hadoop.hive.io.HiveIOExceptionHandlerChain.handleRecordReaderNextException(HiveIOExceptionHandlerChain.java:121) at

Java流(Stream)、文件(File)和IO

Java流(Stream).文件(File)和IO Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列.输入流表示从一个源读取数据,输出流表示向一个目标写数据. Java为I/O提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中. 但本节讲述最基本的和流与I/O相关的功能.我们将通过一个个例子来学习这些功能. 读取控制台输入 Jav

Java笔记:Java 流(Stream)、文件(File)和IO

更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. 输入输出流 简介 一个流被定义为一个数据序列.输入流用于从源读取数据,输出流用于向目标写数据. 下图是一个描述输入流和输出流的类层次图: 在java.io包中操作文件内容的主要有两大类:字节流.字符流,两类都分为输入和输出操作. 在字节流中输出数据主要是使用

Java导出Excel,java.io.IOException: Stream is already closed

使用POI进行Excel导出的时候,后台报了这样一个错误: java.io.IOException: Stream is already closed 导出的代码如下: 查了半天,才发现问题出在第409行 out.close(); out这个输出流是不用自己手动关闭的,系统会自动替我们关闭,自己手动关闭的话还会引发问题. 实际上只有像下面这种使用new关键字创建的输入/输出流,才需要自己手动关闭 InputStream input = new FileInputStream(new File("

Java 流(Stream)、文件(File)和IO

Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列.输入流表示从一个源读取数据,输出流表示向一个目标写数据. Java为I/O提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中. 但本文讲述最基本的和流与I/O相关的功能.我们将通过一个个例子来学习这些功能. 读取控制台输入 Java的控制台输入由System.in完成. 为了获得一