IO流的分类
1、根据数据的流向:
输入流:用来读数据,如从外界设备读数据到内存中;
输出流:用来写数据,如从内存输出数据到外界存储设备;
2、根据数据流的格式:
字节流:一般用于声音或者秃瓢等二进制数据流,如InputStream OutputStream;
字符流:一般用于处理文本数据如(*.txt),比如InputStreamReader OutputStreamWriter;
3、根据数据流的构造函数区分包装流和原始流:
包装流:在实例化流的时候需要传入另一个流作为自己构造函数的参数,如
java.io.Reader reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8");
BufferedReader reader = new BufferedReader(new FileReader(inputFileName));
原始流:在实例化流的时候不需要传入另一个流作为自己构造函数的参数,如
InputStream is= new FileInputStream(inputFile);
1、按字节来读写文件(以字节来读写文件,一般用于读写二进制文件如图片、声音、影音等等)
1 /** 2 * 字节读写文件 3 * @param inputFileName 4 * @param outPutFileName 5 */ 6 public static void readerAndWriteFileByBytes(String inputFileName,String outPutFileName){ 7 File inputFile = new File(inputFileName); 8 File outputFile = new File(outPutFileName); 9 InputStream is = null; 10 OutputStream os = null; 11 try { 12 is = new FileInputStream(inputFile); 13 os = new FileOutputStream(outputFile); 14 byte[] tempBytes = new byte[100]; 15 int byteread = 0; 16 while((byteread = is.read(tempBytes))!=-1){ 17 os.write(tempBytes, 0, byteread); 18 } 19 is.close(); 20 os.close(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 System.out.println("字节读写文件完成"); 25 }
2、按字符来读写文件(以字符来读取文件,一般用于读写文本或数字类的文件)
1 /** 2 * 按字符读取文件并写出到另一个文件(一次读取一个字符) 3 * @param inputFileName 4 * @param outPutFileName 5 */ 6 private static void readAndWriteFileByOneChar(String inputFileName,String outPutFileName) { 7 File inputFile = new File(inputFileName); 8 File outputFile = new File(outPutFileName); 9 Reader reader = null; 10 Writer writer = null; 11 try { 12 reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8"); 13 writer = new OutputStreamWriter(new FileOutputStream(outputFile),"utf-8"); 14 int tempChar; 15 while((tempChar=reader.read())!=-1){ 16 writer.write(tempChar); 17 } 18 reader.close(); 19 writer.close(); 20 System.out.println("按单个字符读写文件结束。。。"); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 } 25 /** 26 * 按字符读取文件并写出到另一个文件(一次读取多个字符) 27 * @param inputFileName 28 * @param outPutFileName 29 */ 30 private static void readAndWriteFileByChars(String inputFileName,String outPutFileName) { 31 File inputFile = new File(inputFileName); 32 File outputFile = new File(outPutFileName); 33 Reader reader = null; 34 Writer writer = null; 35 try { 36 reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8"); 37 writer = new OutputStreamWriter(new FileOutputStream(outputFile),"utf-8"); 38 int tempChar; 39 // 一次读多个字符 40 char[] tempchars = new char[30]; 41 while((tempChar=reader.read(tempchars))!=-1){ 42 if(tempChar == tempchars.length && (tempchars[tempchars.length - 1] != '\r')){ 43 writer.write(tempChar); 44 }else{ 45 for(int i=0;i<tempchars.length;i++){ 46 if (tempchars[i] == '\r') { 47 continue; 48 } else { 49 writer.write(tempchars[i]); 50 } 51 } 52 } 53 } 54 reader.close(); 55 writer.close(); 56 System.out.println("按多个字符读写文件结束。。。"); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 } 60 }
3、按行来读取文件(以行为单位读取文件,一般用于读取面向行的格式化文件)
1 /** 2 * 读取一个文件写入到另一个文件 3 * @param inputFileName 读取文件的路径 4 * @param outputFileName 写出文件的路径 5 */ 6 public static void readAndWriteFileByLine(String inputFileName,String outputFileName){ 7 BufferedReader reader = null; 8 BufferedWriter writer = null; 9 try { 10 writer = new BufferedWriter(new FileWriter(outputFileName)); 11 reader = new BufferedReader(new FileReader(inputFileName)); 12 String tempStr =null; 13 while((tempStr=reader.readLine())!=null){ 14 writer.append(tempStr); 15 writer.newLine(); //换行 16 writer.flush(); //及时清除流的缓冲区,万一文件过大就无法写入了 17 } 18 } catch (IOException e) { 19 e.printStackTrace(); 20 }finally{ 21 if(reader!=null || writer!=null){ 22 try { 23 reader.close(); 24 writer.close(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 System.out.println("文件写入成功"); 30 } 31 }