Java基础知识➣Stream整理(二)

概述

  在Java数据流用到的流包括(Stream)、文件(File流)和I/O流 ,利用该三个流操作数据的传输。

Java控制台输入输出流

 读取控制台使用数据流: BufferedReader和InputStreamReader

 输出控制台使用数据流: PrintStream 常用封装了System.out.println()、System.out.Write()

public static void ReadLine() 
    {
        try{
            System.out.println("请输入内容:");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String Result="";
            do{
                Result= br.readLine();
                System.out.println("接受的内容:"+Result);        
            }
            while(!Result.toUpperCase().equals("END"));
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

读写文件操作用到流

  读取文件使用流 FileInputStream、InputStream 、File、FileReader 常用属性 close()、finalize()、read()、available()

  写入文件使用流 FileOutputStream 、OutputStream、File、FileWrite 使用属性close()、finalize()、write()

  public static void FileOpeart()
   {        
        try{
            String MyWork="Hello Java ,你好啊!";
            byte[] bwiret=MyWork.getBytes();//{11,21,3,40,5};
            OutputStream os=new FileOutputStream("c://MyBoo.txt");
//            for(int x=0;x<bwiret.length;x++)
//            {
//                os.write(bwiret[x]);
//            }
            os.write(bwiret,0,bwiret.length);
            os.close();
            
            InputStream ReadOS=new FileInputStream("c://MyBoo.txt");
            int size=ReadOS.available();
            
            byte[] OutByte=new byte[size];
            ReadOS.read(OutByte, 0, size);
            ReadOS.close();
            String stm=new String(OutByte);
            System.out.println(stm);
            
        }
        catch(IOException ex)
        {
            System.out.println("Error"+ex.getMessage());
        }
    }

  可使用流OutputStreamWriter和OutputStreamWriter来读写文件

public static void FileOpeartWirte()
    {        
        try{
            String MyWork="Hello Java ,你好啊!";
            byte[] bwiret=MyWork.getBytes();//{11,21,3,40,5};
            OutputStream os=new FileOutputStream("c://MyBoo.txt");
            OutputStreamWriter WStream=new OutputStreamWriter(os,"utf-8");
            WStream.append("大家好!");
            WStream.append("
");
            //换行            
            WStream.append("English");
            WStream.close();
            os.close();            
            InputStream ReadOS=new FileInputStream("c://MyBoo.txt");
//            int size=ReadOS.available();
//            
//            byte[] OutByte=new byte[size];
//            ReadOS.read(OutByte, 0, size);
//            ReadOS.close();
//            String stm=new String(OutByte,"utf-8");
//            System.out.println(stm);
            InputStreamReader reader=new InputStreamReader(ReadOS,"utf-8");
            StringBuffer sb = new StringBuffer();
            while (reader.ready()) {
              sb.append((char) reader.read());
              // 转成char加到StringBuffer对象中
            }
            System.out.println(sb.toString());
            reader.close();
            // 关闭读取流            
            ReadOS.close();
            // 关闭输入流,释放系统资源                        
        }
        catch(IOException ex)
        {
            System.out.println("Error"+ex.getMessage());
        }
    }

Java中的目录的操作

  创建目录mkdir( )方法创建一个文件夹、kdirs()方法创建一个文件夹和它的所有父文件夹;

  读取目录:isDirectory() 判断是否目录, list() 方法,来提取它包含的文件和文件夹的列表;

public static void FileMKDir()
 {
        try{
        String Path="c:/tmp/user/java";
        File d=new File(Path);
        boolean res= d.mkdirs();
    
        String dirname="c:/tmp";
        File dt=new File(dirname);
        if(dt.isDirectory())
        {
            System.out.println("目录:"+dirname);
            String[] st=dt.list();
            for(String sr:st)
            {
                File ft=new File(dirname+"/"+sr);
                if(ft.isDirectory())
                {
                    System.out.println("目录"+dirname+"/"+sr);
                }
                else
                {
                    System.out.println("文件"+dirname+"/"+sr);                                        
                }
            }
        }    
    }
    catch(Exception ex)
    {
          System.out.println("Err:"+ex.getMessage());
    }
 }
原文地址:https://www.cnblogs.com/xibei666/p/6431358.html