大部分程序都需要输入/输出处理,把这些不同类型的输入、输出源抽象为流(Stream),流根据输入输出分为输入流和输出流,输入或输出的数据则称为数据流(Data Stream),不管是输入还是输出流都要先打开然后最后操作,而且更重要的一点就是在操作完后要将流关闭,否则会占用资源。
流根据处理的不同也可以分为字节流,字符流。
字节流中最常用到的有文件流,过滤流,对象流,标准流,内存流等,基类是InputStream和OutputStream。
文件流:FileInputStream 和FileOutputStream。
//读文件->文件输入流
FileInputStream fis = null;
//写文件->文件输出流
FileOutputStream fos = null;
try
{
fis = new FileInputStream(new File("输入的文件"));
fos = new FileOutputStream(new File("输出的文件"));
// 写法一
int iChar = 0;
while((iChar = fis.read()) != -1)
{
fos.write(iChar);
}
// //写法二
// byte[] arrb = new byte[1024];
// int len = 0;
// while((len = fis.read(arrb)) > 0)
// {
// fos.write(arrb, 0, len);
// }
fos.flush();
System.out.println("文件读写完毕");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fis = null;
}
if(fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fos = null;
}
}
}
内存流:ByteArrayInputStream和ByteArrayOutputStream
String str = "abcd";
byte[] arr = str.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(arr);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i = 0;
while((i = bis.read()) != -1)
{
bos.write(i);
}
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(new File("src/a.txt"));
bos.writeTo(fos);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fos = null;
}
}
}
</pre><p></p><p></p> 过滤流中有缓冲流:BufferedInputStream和BufferedOutputStream<p> </p><pre name="code" class="java">//读文件->文件输入流
FileInputStream fis = null;
//写文件->文件输出流
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
fis = new FileInputStream(new File("src/2 Java异常处理机制.ppt"));
fos = new FileOutputStream(new File("src/a.ppt"));
//将文件的字节输入流 转换成了 带有缓冲的字节输入流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] arrb = new byte[1024];
int len = 0;
while((len = bis.read(arrb)) > 0)
{
bos.write(arrb, 0, len);
}
bos.flush();
System.out.println("文件读写完毕");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(bis != null)
{
try
{
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
bis = null;
}
if(bos != null)
{
try
{
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
bos = null;
}
if(fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fis = null;
}
if(fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fos = null;
}
}
}
标准流:System.in 和System.out
System.out.println("请输入内容,然后点击回车:");
int len = 0;
try
{
byte[] arr = new byte[1024];
while( (len = System.in.read(arr)) != 13)
{
System.out.println(new String(arr, 0, len));
}
System.out.println("输入结束");
}
catch (IOException e)
{
e.printStackTrace();
}
}
对象流:ObjectOutputStream和ObjectinputStream
字符流中最常用到的有文件流,过滤流,管道流等,基类是reader()和writer()。
文件流:FileReader和FileWriter
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
fr = new FileReader(new File("src/a.txt"));
fw = new FileWriter(new File("src/2.txt"));
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char[] arr = new char[1024];
int len = 0;
String strLine = null;
while( (strLine = br.readLine()) != null)
{
bw.write(strLine);
}
bw.flush();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
br = null;
}
if(bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
bw = null;
}
if(fr != null)
{
try
{
fr.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fr = null;
}
if(fw != null)
{
try
{
fw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fw = null;
}
}
}
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
fis = new FileInputStream(new File("src/tnsnames.ora"));
fos = new FileOutputStream(new File("src/2.ora"));
//先将 字节输入流 fis -> Reader -> BufferedReader
br = new BufferedReader(
new InputStreamReader(fis));
bw = new BufferedWriter(
new OutputStreamWriter(fos));
String strLine = null;
while((strLine = br.readLine()) != null)
{
// \r\n:表示换行符
bw.write(strLine + "\r\n");
}
bw.flush();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
br = null;
}
//....
}
}
对字符数组:CharArrayReader和CharArrayWriter
String str = "你好";
char[] arr = str.toCharArray();
CharArrayReader car = new CharArrayReader(arr);
CharArrayWriter caw = new CharArrayWriter();
FileWriter fw = null;
char[] arr2 = new char[1024];
int len = 0;
try
{
while((len = car.read(arr2)) > 0)
{
caw.write(arr2, 0, len);
}
fw = new FileWriter(new File("src/3.ora"));
caw.writeTo(fw);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(fw != null)
{
try
{
fw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
fw = null;
}
}
}