• Java文件与io——复制文件和转换流


    字节流与字符流的区别

    在所有的流操作里,字节永远是最基础的。任何基于字节的操作都是正确的。无论是文本文件还是二进制的文件。

    如果确认流里面只有可打印的字符,包括英文的和各种国家的文字,也包括中文,那么可以考虑字符流。由于编码不同,多字节的字符可能占用多个字节。比如GBK的汉字就占用2个字节,而UTF-8的汉字就占用3个字节。所以,字符流是根据指定的编码,将1个或多个字节转化为java里面的unicode的字符,然后进行操作。字符操作一般使用Writer,Reader等,字节操作一般都是InputStream,OutputStream以及各种包装类,比如BufferedInputStream和BufferedOutputStream等。

    总结:如果你确认你要处理的流是可打印的字符,那么使用字符流会看上去简单一点。如果不确认,那么用字节流总是不会错的。

    指定一个盘符下的文件,把该文件复制到指定的目录下。

    public class CopyFileDemo {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args)  {
            // TODO Auto-generated method stub
            File target=new File("H:\123.txt");
            String dest="G:\test";
            copyFile(target,dest);
            System.out.println("文件复制成功");
        }
        /**
         * 
         * @param target 源文件,要复制的文件
         * @param dest  目标文件
         */
        public static void copyFile(File target,String dest){
            String filename=target.getName();
            //System.out.println(filename);
            File destFile=new File(dest+filename);
            try {
                //先进行输入才能进行输出,代码书序不能变
                InputStream in=new FileInputStream(target);
                OutputStream out=new FileOutputStream(destFile);
                byte[] bytes=new byte[1024];
                int len=-1;
                
                while((len=in.read(bytes))!=-1){
                    out.write(bytes, 0, len);
                }
                out.close();
                in.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    字节字符转换流,可以将一个字节流转换为字符流,也可以将一个字符流转换为字节流

    OutputStreamWriter:可以将输出的字符流转换为字节流的输出形式

    InputStreamReader:将输入的字节流转换为字符流输入形式

    public class ChangeStreamDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String info=reader(System.in);
            System.out.println(info);
        }
    
        public static String reader(InputStream in){
            BufferedReader r=new BufferedReader(new InputStreamReader(in));
            try {
                return r.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    
    }
  • 相关阅读:
    Lambda Expression in C#
    DirectorySearcher LDAP
    摘录 LDAP
    The return types for the following stored procedures could not be detected
    无法调试存储过程,无法启动T-SQL调试
    根据窗口句柄显示窗体
    还原数据库时提示空间不足
    xml格式发送
    ../ 上一级目录
    匿名函数 invoke
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/5117513.html
Copyright © 2020-2023  润新知