字节流:用来处理二进制文件
字符流:用来处理文本数据
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();}}
}
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();}}
}
写入的方式:
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 {}}
}
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();}}}
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)进行读写文件,比如说文件。直接跟数据源相连。
对某一个节点流进行包装,可以封装任意一个节点流,使处理读写更加方便。
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();}
}
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();}
}
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();}}
}
下一篇:Linux的目录结构