java中IO流的操作
创始人
2025-05-30 03:22:55

对于java中io流的一些操作和类进行总结

io流的分类:

 字节流:用来处理二进制文件

字符流:用来处理文本数据

文件io流:

字节流:

InputStream的子类:

FileInputStream类:

读的方式:read

  • 一个字节的读取:
public class test_FileInputStream {public static void main(String[] args) {}@Testpublic void test_FileInputStream() throws IOException {String filepath="D:\\网盘文件\\hello.txt";FileInputStream fileInputStream=null;int read_data=0;//读取文件字节数try {fileInputStream=new FileInputStream(filepath);//read读取的字节数:如果读取失败的话就直接返回-1while ((read_data=fileInputStream.read())!=-1){System.out.print((char) read_data);}}catch (Exception e){e.printStackTrace();}finally {fileInputStream.close();}}
}
  • 使用byte数组来读取:
public class test_FileInputStream02 {public static void main(String[] args) {}@Testpublic void test_FileInputStream02() throws IOException {String filepath="d:\\网盘文件\\hello.txt";byte[] buf=new byte[10];FileInputStream fileInputStream=null;int read_len=0;//读取文件字节数try {fileInputStream=new FileInputStream(filepath);//按位数组读取元素:while ((read_len=fileInputStream.read(buf))!=-1) {System.out.print(new String(buf,0,read_len));}}catch (Exception e){e.printStackTrace();}finally {fileInputStream.close();}}
}

OutputStream子类:

FileOuputStream类:

写入的方式:

  •  写入文件内容:
public class test_FileOutputStream {public static void main(String[] args) {}@Testpublic void fileOutputStream() throws IOException {String filename="D:\\网盘文件\\hello.txt";FileOutputStream fileOutputStream=null;//写入文件的内容:String str="hello world";try {//写入文件使用追加的方式:fileOutputStream=new FileOutputStream(filename,true);fileOutputStream.write(str.getBytes());System.out.println("写入文件成功!");}catch (Exception e){e.printStackTrace();}finally {fileOutputStream.close();}}
}

文件拷贝:

public class test_拷贝文件 {public static void main(String[] args) {}@Testpublic void copyfile(){String srcfile="d:\\file.txt";String desfile="d:\\网盘文件\\file.txt";FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;int read_len=0;byte [] buf=new byte[1024];try {fileInputStream=new FileInputStream(srcfile);fileOutputStream=new FileOutputStream(desfile);while ((read_len=fileInputStream.read(buf))!=-1){//使用位数组的方式拷贝文件:fileOutputStream.write(buf,0,read_len);System.out.println("拷贝文件完成!");}}catch (Exception e){e.printStackTrace();}finally {}}
}

字符流:Reader 和Writer

Reader:实现的子类

FileReader类:

 public class test_FileReader {//Reader的实现子类;/***** 使用读取单个字符的方式* ***/@Testpublic void file_reader01() throws IOException {String filepath="d:\\file.txt";FileReader fileReader =null;int read_len=0;try {fileReader=new FileReader(filepath);System.out.print("读取字符为:");while ((read_len=fileReader.read())!=-1){System.out.print((char)read_len);}}catch(Exception e){e.printStackTrace();}finally {fileReader.close();}}/***** 使用字符数组的方式进行读取文件* **/@Testpublic void file_reader02() throws IOException {String filepath="d:\\file.txt";FileReader fileReader =null;//使用字符数组的方式进行读char [] chars=new char[20];int read_data=0;try{fileReader=new FileReader(filepath);while ((read_data=fileReader.read(chars))!=-1){System.out.print(new String(chars,0,read_data));}} catch (Exception e) {e.printStackTrace();} finally {fileReader.close();}}}

Writer:实现的子类

Filewriter类:

public class test_FileWriter {//Writer的实现子类@Testpublic void file_writer01() throws IOException {String filepath="d:\\file.txt";FileWriter fileWriter=null;//        char [] str={'a','b','c'};try{fileWriter=new FileWriter(filepath,true); //以追加的方式写入;
//            fileWriter.write(str); 以字符数组的方式写入fileWriter.write("你好java"); //以字符串的方式写入System.out.println("写入文件成功");}catch (Exception e){e.printStackTrace();}finally {fileWriter.close();}}}

节点流和处理流:

节点流:

从一个特定的数据源(比如是FileReader和FilerWriter)进行读写文件,比如说文件。直接跟数据源相连。

处理流:(BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream)继承了Reader和Writer类

对某一个节点流进行包装,可以封装任意一个节点流,使处理读写更加方便。

字符处理流:

BufferReader:(用来处理字符)

public class test_BufferReader {public static void main(String[] args) throws Exception{String filepath="d:\\file.txt";String line=null; //按行读取BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath));//readLine() 是按一行来读取的;// 当返回null 表示读取完毕;while ((line=bufferedReader.readLine()) !=null){System.out.println(line);}bufferedReader.close();}
}

BufferWriter:(用来处理字符)

public class test_BufferWriter {public static void main(String[] args) throws IOException {String filepath="d:\\file.txt";//new FileWriter(filepath,true) //以追加的方式:BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath,true));bufferedWriter.write("要努力变强啊!");bufferedWriter.newLine(); //进行换行操作;System.out.println("写入文件成功!");bufferedWriter.write("那是肯定的");//关闭流的操作:bufferedWriter.close();}
}

字节处理流(用来处理二进制数据):

BufferedInuputStream:(用来处理字节,只要给它传入一个InputStream的节点流)

BufferedOutputStream:(用来处理字节,只要给它传入一个OutputStream节点流)

public class test_BinputStream_BoutputStream {public static void main(String[] args) throws IOException {String srcpath="d:\\test.jpg";String despath="d:\\test01.jpg";int read_len=0;//读取数据的长度://字节处理流,就是将其他的节点字节处理进行包装,然后操作的BufferedInputStream bufferedInputStream = null;BufferedOutputStream bufferedOutputStream=null;byte[] buf=new byte[1024];//以字节数组的方式读取:try {//新建处理流的对象:bufferedInputStream=new BufferedInputStream(new FileInputStream(srcpath));bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(despath));//读取文件:while ((read_len=bufferedInputStream.read(buf))!=-1){//写入文件bufferedOutputStream.write(buf,0,read_len);}System.out.println("读取和写入二进制图片成功!");}catch (Exception e){e.printStackTrace();}finally {bufferedInputStream.close();bufferedOutputStream.close();}}
}

相关内容

热门资讯

长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
cad打印线条粗细设置 cad... 004-线型(下)打印样式设置和线型文件使用一、线宽设置方法制图规范里边的线宽要求,我们已经定义好,...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
阿西吧是什么意思 阿西吧相当于... 即使你没有受到过任何外语培训,你也懂四国语言。汉语:你好英语:Shit韩语:阿西吧(아,씨발! )日...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...