• 编译原理 >实验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();
    	}
    }
    	
            
    

      

  • 相关阅读:
    非递归遍历二叉树Java
    【滴滴实习】滴滴2023届产研秋招储备实习生正在火热招募中🔥
    c++从office word的xml源文本文件中提取空行后的首个段落
    自建脚本安装docker
    银河麒麟安装软件失败,可使用命令行安装
    Fiddler+Proxifier抓pc应用包(c/s架构)
    11. Spring高级AOP概念
    10. Spring高级IOC的深入剖析
    12. Spring高级注解驱动AOP开发入门
    9. Spring高级注解驱动开发入门
  • 原文地址:https://www.cnblogs.com/suiyun/p/2690320.html
Copyright © 2020-2023  润新知