zoukankan      html  css  js  c++  java
  • 字节流 

    // 字节流(以stream结尾的),要注意的一点是字节流可以转成是字符流,但是字符流转换成字节流没有
    			//什么意义了,以下步骤就可以做到写入一个流,接着读出在一个新的文件,把以前的文件的内容放在新建的那个文件
    
            File file = new File("F:\1117\ceshi.txt");
    			
    	File fileout = new File("F:\1117\ceshi_new_reader.txt");
    
        
            InputStream in = null;  //一开始定义的是一个没有对象的输入流,输出流
    			OutputStream out = null;
    			int result  = -1;   //判断是否几经读到内容的结尾
    			try {
    				in = new FileInputStream(file);  //把文件传入
    				out = new FileOutputStream(fileout);
    				while ((result = in.read()) != -1) {  //字节流一个字节一个字节的读取,根据read方法
    					System.out.println((char)result);
    					out.write(result);
    				}
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    				
    			} catch (IOException e) {
    				e.printStackTrace();
    				
    			}finally {  //一定会执行的语句
    				try {
    					in.close();
    					out.flush();
    					out.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    					
    				}
    			}
    

    字符流

    //字符流,字符流(以Reader, Writer结尾的),一个字符占两个字节,以一个字符为一个单位
    
            File file = new File("F:\1117\ceshi.txt");
    			
    			File fileout = new File("F:\1117\ceshi_new_reader.txt");
    
    
            try {
    				Reader r = new FileReader(file);
    				Writer w = new FileWriter(fileout);
    				int result = -1;
    				while((result = r.read()) != -1) {
    					System.out.println((char)result);
    					w.write(result);
    					
    				}
    					r.close();
    					w.flush();  //在使用流的时候的一个良好的习惯, 先flush, 再close
    					w.close();
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    				
    			} catch (IOException e) {
    				e.printStackTrace();
    				
    			}
    

    文件类:
        java.io.File

    用递归的方法列出目录下所有的内容

    public static void main(String[] args) {
    		File file = new File("f:\1117");
    		list(file);
    
    	}
    	public static void list(File file ) {
    		if (file.isDirectory()) {  //判断是否为目录
    			File[] files = file.listFiles(); //获取当前这个路径下面所有的文件和文件夹,目录下面还有子目录
    			for (File f : files) {  //遍历,取出File数组里的
    				if (f.isDirectory()) {  
    					System.out.println(f.getName());  //目录里面还有文件
    					list(f);  //递归,自身调用自身,
    				}else {  //不是目录的话,就直接打印文件名
    					System.out.println(f.getName());
    				}
    			}
    		}else {  //不是目录,直接打印文件名
    			System.out.println(file.getName());
    		}
    	}
    

    递归的思想求阶乘

    	public static int fact( int n) {  //递归求阶乘
    		if (n==1) {
    			return 1;
    		}else  {
    			return n*fact(n - 1);
    		}
    	}
    

    处理流  Buffered

    public static void main(String[] args) {
    		File file = new File("F:\1117\ceshi.txt");
    		try {
    			Reader r = new FileReader(file);
    			BufferedReader br = new BufferedReader(r);//给Reader套一个管道,相当于处理流
    			
    				Writer w = new FileWriter("F:\1117\ceshi_new.txt",true); //加true是追加内容
    				BufferedWriter bw = new BufferedWriter(w);
    				String str = null;
    				while ((str = br.readLine()) != null) {  //每次读一行
    					bw.write(str);
    					bw.newLine();  //换行
    				}
    				br.close();  //处理流关闭,里面的字符流也相应关闭
    				bw.flush();
    				bw.close();
    				
    				
    		} catch (FileNotFoundException e) {  //捕获异常
    			
    			e.printStackTrace();
    		} catch (IOException e) {
    				
    				e.printStackTrace();
    			}
    		
    
    	}
    
  • 相关阅读:
    HDU 5744
    HDU 5815
    POJ 1269
    HDU 5742
    HDU 4609
    fzu 1150 Farmer Bill's Problem
    fzu 1002 HangOver
    fzu 1001 Duplicate Pair
    fzu 1150 Farmer Bill's Problem
    fzu 1182 Argus 优先队列
  • 原文地址:https://www.cnblogs.com/zuo72/p/7967201.html
Copyright © 2011-2022 走看看