zoukankan      html  css  js  c++  java
  • 编译原理 >实验1

    import java.io.*;
    
    public class IOlianxi1 {
    	public static void main(String[] args) throws IOException {
    		System.out.println("the path: "+System.getProperty("user.dir"));
    		CreateFile("C:/123.txt");
    		
        	         readFile("C:/AbsPath.txt");
    		 writeFile2("C:/RelPath.txt","abcdfdfdfdfddde");
    		 readFile("C:/AbsPath.txt");
    		 copy();
    	}
    	
    	public static boolean CreateFile(String destFileName) throws IOException{
    		File file = new File(destFileName);
    		if(file.exists()){
    			System.out.println("Fail! Here it is!");
    			return false;
    		}
    		file.createNewFile();
    		System.out.println("success!");
    		return true;
    	}
    	
    	
    	public static void readFile(String fileName)throws IOException{
    		File file = new File(fileName);
    		BufferedReader reader = new BufferedReader(new FileReader(file));
    		String br=null;
    		while((br=reader.readLine())!=null){
    			System.out.println(br);
    		}
    		reader.close();
    		
    	}
    	public static void writeFile2(String filePath,String str)throws IOException{
    	PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));  
    	pw.print(str);
    	pw.close();
    	}
    	
    	
    	public void writeFile(String filePath,StringBuffer text)throws IOException{
    		   BufferedWriter rw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
    		   rw.write(new String(text));
    		   rw.close();	  
    	}
    		 
    	
    	public static void copy_1() throws IOException
    	{
    		FileReader fr=new FileReader("FileReaderDemo2.java");
    		
    		FileWriter fw=new FileWriter("FileReaderDemo2.txt");
    				
    		int ch=0;
    		while((ch=fr.read())!=-1)//循环读取,每次读取一个字符,到达文件尾则返回-1
    		{
    			fw.write(ch);//取从第一个到第num个字符,避免有数据不足3个字符的情况
    		}
    		fw.close();
    		fr.close();
    	}
    	public static void copy() throws IOException
    	{
    		FileReader fr=new FileReader("C:/AbsPath.txt");
    		
    		FileWriter fw=new FileWriter("C:/out.txt");
    		
    		char[] buf=new char[1024];
    		
    		int num=0;
    		while((num=fr.read(buf))!=-1)
    		{
    			fw.write(buf,0,num);
    		}
    		fr.close();
    		fw.close();
    	}
    }
    	
            
    

      

  • 相关阅读:
    react-native window下创建Hello(解决创建一路的坑)
    vue2.0 监听滚动 锚点定位
    vue-awesome-swiper 轮播图使用
    vue和react区别
    vuex 管理状态
    vue 解决axios 跨域问题
    判断一个对象是否为空? js
    微信小程序中的自定义组件(components)
    深入理解ES6箭头函数中的this
    vue中组件的data为什么是一个函数
  • 原文地址:https://www.cnblogs.com/suiyun/p/2690320.html
Copyright © 2011-2022 走看看