-------
android培训、
java培训、期待与您交流! ----------
------- android培训、 java培训、期待与您交流! ----------
1.IO流
IO流用来处理设备之间的数据传输。
java对数据的操作是通过流的方式,用于操作流的对象都在IO包中。
流按操作数据分类:字节流(媒体数据)和字符流(文本数据)
按流向分:输入流,输出流
字符流的由来:
中国文字:(GB2312扩容后成了GBK)
全球文字:UTF-8
Unicode 表中每个文字用用2个字节表示,UTF-8中,文字可能用1/2/3个字节表示。
使用字符流时可以自己选择文字编码。
字节流:InputStream OutputStream
字符流:Reader Writer
注:四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
(1)Writer
FileWriter close方法,关闭流资源之前会调用一个flush方法,将内部缓冲的数据刷到目的文件中。
close后,同一个流对象不能再调用write方法。
流对象使用完后,一定要关闭。(一般在finally中进行流的关闭操作)
Windows系统中的换行 "\r\n" Linux系统中的换行 "\n"
练习代码:
/*
*续写文件
*/
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args)
{ FileWriter fw = null;
try{
//第二个参数设置为true,表示不覆盖已有文件,并在已有文件的末尾续写数据 .
fw = new FileWriter("a.txt",true);//如果文件不存在,会创建
fw.write("\r\n我是续写进去的");
}catch (IOException e){
System.out.println(e.toString());
}finally{
try{
if(fw!=null){
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
*续写文件
*/
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args)
{ FileWriter fw = null;
try{
//第二个参数设置为true,表示不覆盖已有文件,并在已有文件的末尾续写数据 .
fw = new FileWriter("a.txt",true);//如果文件不存在,会创建
fw.write("\r\n我是续写进去的");
}catch (IOException e){
System.out.println(e.toString());
}finally{
try{
if(fw!=null){
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
(2)Reader
Reader 没有flush方法
字符流都有默认编码,默认编码为系统编码,中文Windows一般为GBK
读取文件不存在会报FileNotFoundException该异常是IOException的子类
read()方法一次读一个字符,且自动往下读,返回类型为int,读到文件末尾返回-1
练习代码:
/**
* 文件的读取
*/
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args)
{ FileReader fr = null;
try
{
fr = new FileReader("a.txt");
char[] buf = new char[2048]; //一般为1024的整数倍,一个字符两个字节,2048 就是一次读4K的数据
int len = 0;
while((len=fr.read(buf))!=-1){//等于-1的时候说明读到文件末尾了
System.out.print(new String(buf,0,len));
}
}
catch (IOException e)
{
System.out.println(e.toString());
}finally{
if(fr!=null){
try{
fr.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
* 文件的读取
*/
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args)
{ FileReader fr = null;
try
{
fr = new FileReader("a.txt");
char[] buf = new char[2048]; //一般为1024的整数倍,一个字符两个字节,2048 就是一次读4K的数据
int len = 0;
while((len=fr.read(buf))!=-1){//等于-1的时候说明读到文件末尾了
System.out.print(new String(buf,0,len));
}
}
catch (IOException e)
{
System.out.println(e.toString());
}finally{
if(fr!=null){
try{
fr.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
}
(3)IO异常
IO异常的处理方式有两种,一种是直接抛出,一种是捕获
1.项目代码中一般都要对该异常进行捕获,且需要在finally中对IO流进行关闭,关闭IO流的操作同样需要进行IOException捕获。
2.为避免在finally中操作IO流时出现未定义变量的编译错误,需在try{}catch{}前声明IO流对象
练习代码:
/*
*续写文件
*/
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args)
{ FileWriter fw = null;
try{
//第二个参数设置为true,表示不覆盖已有文件,并在已有文件的末尾续写数据
fw = new FileWriter("a.txt",true);
fw.write("\r\n我是是续写进去的");
}catch (IOException e){
System.out.println(e.toString());
}finally{
try{
if(fw!=null){
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
*续写文件
*/
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args)
{ FileWriter fw = null;
try{
//第二个参数设置为true,表示不覆盖已有文件,并在已有文件的末尾续写数据
fw = new FileWriter("a.txt",true);
fw.write("\r\n我是是续写进去的");
}catch (IOException e){
System.out.println(e.toString());
}finally{
try{
if(fw!=null){
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
注:window 系统下 换行为 "\r\n" Linux 系统下为 "\n"
/*
*拷贝图片
*/
import java.io.*;
class CopyPic
{
public static void main(String args[]){
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("d:\\1.jpg");
fos = new FileOutputStream("d:\\copyPic.jpg");
byte[] buf = new byte[2048];
int len = 0;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("文件复制失败");
}finally{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取流关闭失败");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入流关闭失败");
}
}
}
}
*拷贝图片
*/
import java.io.*;
class CopyPic
{
public static void main(String args[]){
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("d:\\1.jpg");
fos = new FileOutputStream("d:\\copyPic.jpg");
byte[] buf = new byte[2048];
int len = 0;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("文件复制失败");
}finally{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取流关闭失败");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入流关闭失败");
}
}
}
}
------- android培训、 java培训、期待与您交流! ----------