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();
    	}
    }
    	
            
    

      

  • 相关阅读:
    大学那点破事
    我是计算机专业的学生
    acm 血泪教训
    汉诺塔问题(竟然还与Sierpiński三角形分形有关)
    证明:log(n!)与nlogn是等价无穷大
    priority_queue POJ 3253 Fence Repair
    插入排序之直接插入排序
    对Huffman编码的思考,熵
    Sudan Function
    给力小程序
  • 原文地址:https://www.cnblogs.com/suiyun/p/2690320.html
Copyright © 2011-2022 走看看