• IO流之间的转换应用


    package com.laoxu.test.day01.streamDemo;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;

    /**
    * 通过IO流复制文件
    * @author Administrator
    *
    */
    public class InputAndOutPutStreamDemo {

    private static boolean IO_IS_SUCCESS = true;
    /**
    * 获取文件路径,判断是否存在该文件或者该文件路径是否正确
    * @param fileName
    * @return
    */
    private static File getFile(String fileName){
    File file = new File(fileName);
    if(file.exists()&&file.isFile())
    return file;
    return null;
    }
    private static File getOutputFile(String outfileName){
    return new File(outfileName);
    }
    /**
    * 获取文件的IO流
    * @param file
    */
    private static FileInputStream getInputStream(File file){
    if(file==null){
    System.out.println("getInputStream error");
    return null;
    }
    try {
    FileInputStream fis = new FileInputStream(file);
    return fis;
    } catch (Exception e) {
    e.printStackTrace();
    try {
    throw new FileNotFoundException(e.getMessage());
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    }
    return null;
    }
    /**
    * 通过传递的InputStream和OutputStream,将文件复制,输出
    * @param file
    * @param fis
    * @return
    * @throws IOException
    */
    private static boolean outputInfo(BufferedReader br,BufferedWriter bw) {
    if(br==null || bw==null){
    return !IO_IS_SUCCESS;
    }
    try {
    String str = "";
    while(true){
    str = br.readLine();
    if(str==null){
    break;
    }
    bw.write(str+" ");
    }
    closeStream(br);
    closeStream(bw);
    return IO_IS_SUCCESS;
    } catch (Exception e) {
    e.printStackTrace();
    return !IO_IS_SUCCESS;
    }

    }
    /**
    * 获取缓冲字符输入流
    */
    private static BufferedReader getBufferReader(FileInputStream fis){
    return new BufferedReader(new InputStreamReader(fis));
    }
    /**
    * 获取缓冲字符输出流
    */
    private static BufferedWriter getBufferWrite(FileOutputStream fos){
    return new BufferedWriter(new OutputStreamWriter(fos));
    }
    /**
    * 获取输出OutputStream流
    * @param file
    * @return
    */
    private static FileOutputStream getOutputStream(File file){
    if(file==null){
    return null;
    }
    try {
    FileOutputStream fos = new FileOutputStream(file);
    return fos;
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    try {
    throw new Exception(e.getMessage());
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    return null;
    }
    }
    /**
    * 关闭流
    * @param obj
    * @throws IOException
    */
    public static void closeStream(Object obj) throws IOException{
    if(obj!=null){
    if(obj instanceof FileInputStream){
    ((FileInputStream) obj).close();
    return ;
    }else if(obj instanceof FileOutputStream){
    ((FileOutputStream) obj).close();
    return ;
    }
    else if(obj instanceof BufferedReader){
    ((BufferedReader) obj).close();
    return ;
    }
    else if(obj instanceof BufferedWriter){
    ((BufferedWriter) obj).close();
    return ;
    }
    else if(obj instanceof InputStreamReader){
    ((InputStreamReader) obj).close();
    return ;
    }
    else if(obj instanceof OutputStreamWriter){
    ((OutputStreamWriter) obj).close();
    return ;
    }

    }
    return ;
    }
    /**
    * 整合方法
    * @param inputFile
    * @param outFile
    */
    public static boolean copyMethod(String inputFile,String outFile){
    return outputInfo(getBufferReader(getInputStream(getFile(inputFile))),getBufferWrite(getOutputStream(getOutputFile(outFile))));

    }


    /**
    * 测试方法
    * @param args
    */
    public static void main(String[] args) {
    String inputFile = "C:"+File.separator+"debug.ini";
    String outFile = "C:"+File.separator+"debug.txt";
    if(copyMethod(inputFile, outFile)){
    System.out.println("success");
    }else{
    System.out.println("error");
    }
    }


    }

  • 相关阅读:
    RE最全面的正则表达式----字符验证
    Ajax tips(my_jquery_function.js)
    Python 分布式执行测试用例
    Python black + flake8 自动化规范代码
    JavaScript学习笔记-ES5
    pytest-assume 测试用例某一断言失败继续执行后面的代码
    pytest常用命令行
    [pretty_errors] Prettifies Python exception output to make it legible
    requests-html
    Python 类 继承与重写
  • 原文地址:https://www.cnblogs.com/xuzhenmin/p/3291843.html
Copyright © 2020-2023  润新知