zoukankan      html  css  js  c++  java
  • Java学习笔记-File

    //文件操作

    //2015年4月30日15:40:21

    package com.alfredsun.first.main;
    
    import java.io.File;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			File file =new File("hello.txt");
    			if(file.exists())
    			{
    				//file
    				System.out.println(file.isFile());
    				//dire
    				System.out.println(file.isDirectory());
    			}
    			else
    			{
    				System.out.println("File Not Exist");
    				try {
    					file.createNewFile();
    					System.out.println("File has been created.");
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					System.out.println("FIle can not be creat.");
    					e.printStackTrace();
    				}
    				
    			}
    			
    		}
    
    }
    

      

    //文件夹创建1-mkdir

    //2015年5月2日20:42:28

    package com.alfredsun.first.main;
    
    import java.io.File;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			File folder =new File("My New Folder");
    			folder.mkdir();
    			System.out.println("Folder Create Success!");
    		}
    
    }
    

      

    //mkdirs

    //2015年5月2日20:49:50

    package com.alfredsun.first.main;
    
    import java.io.File;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			File folder =new File("My New Folder/one/two/three/main");
    			if(folder.mkdirs())		//mkdirs
    			{
    				System.out.println("Folder Create Success!");
    			}
    			else
    			{
    				if(folder.exists())
    				{
    					System.out.println("The Folder Exist");
    				}
    				else
    				{
    					System.out.println("Folder Can't been creat");
    				}
    			}
    		}
    
    }
    

      

    //文件夹的遍历

    /2015年5月2日21:33:12

    package com.alfredsun.first.main;
    
    import java.io.File;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    		printFiles(new File("."),1);
    			
    		}
    	public static void printFiles(File dir,int tab) 
    	{
    		 if (dir.isDirectory()) 
    		 {
    			File next[] =dir.listFiles();
    			for(int i=0;i<next.length;i++)
    			{
    				for(int j=0;j<tab;j++)
    				{
    					System.out.print("|---");
    				}
    				if(next[i].isFile())
    				{
    					System.out.println(next[i].getName());
    				}
    				else
    				{
    					printFiles(next[i],tab+1);
    				}
    			}
    		 }
    	}
    
    }
    

      

    |---.classpath
    |---.project
    |---|---|---org.eclipse.jdt.core.prefs
    |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---test.class
    |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---readme.txt
    |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---test.java
    

      

    //文件读取

    //2015年5月2日21:51:43

    package com.alfredsun.first.main;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			File file =new File("readme.txt");
    			if(file.exists())
    			{
    				System.err.println("File is Exist.");
    			}
    			try 
    			{
    				FileInputStream fis =new FileInputStream(file);
    				InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
    				BufferedReader br =new BufferedReader(isr);
    				
    				String line;
    				while((line = br.readLine())!=null)
    				{
    					System.out.println(line);
    				}
    				br.close();
    				isr.close();
    				fis.close();
    				
    			} 
    			catch (FileNotFoundException e)
    			{
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (UnsupportedEncodingException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			
    			}
    
    }
    

      

    //字节流读写数据,byte[]可能读取到半个汉字

    //2015年5月3日16:36:28

    package com.alfredsun.first.main;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			try {
    				FileInputStream fis =new FileInputStream("readme.txt");
    				byte[] input =new byte [20];
    				fis.read(input);
    				String inputString =new String(input);
    				System.out.println(inputString);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    		}
    }
    

      

    //文件流写入

    //2015年5月3日21:02:37

    package com.alfredsun.first.main;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			// TODO Auto-generated method stub
    			FileOutputStream fos;
    			try {
    				fos = new FileOutputStream("readme.txt");
    				String outString ="write 123456";
    				byte[] output =outString.getBytes("UTF-8");
    				fos.write(output);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (UnsupportedEncodingException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    
    		}
    }
    

      

    //使用输入流输出流复制文件

    //大文件惨不忍睹,150M大概要2分钟,不过文件无错误,不知道缓存有何用

    //2015年5月4日20:13:37

    package com.alfredsun.first.main;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			try {
    				FileInputStream fis= new FileInputStream("1.jpg");
    				FileOutputStream fos=new FileOutputStream("1.1.jpg");
    				byte input[] =new byte[50];			//每次读取字节 //有差异112-150 应该是操作系统的问题
    				while (fis.read(input)!=-1) {		//返回一个boolean表-完成与否
    					fos.write(input);
    					
    				}
    				
    				
    				
    				fis.close();
    				fos.close();
    				System.out.println("Done");
    				
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    }
    

      

    //带有缓存的输入输出流

    //速度飞快,考虑实际的文件大小来确定缓冲区和数组大小

    //2015年5月4日20:52:36

    package com.alfredsun.first.main;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class test {
    
    	public static void main(String[] args) 
    		{
    			try {
    				FileInputStream fis= new FileInputStream("1.jpg");
    				BufferedInputStream bis= new BufferedInputStream(fis,1000000);
    				FileOutputStream fos=new FileOutputStream("1.1.jpg");
    				BufferedOutputStream bos=new BufferedOutputStream(fos,1000000);
    				int count =0;
    				byte input[] =new byte[100000];			//每次读取字节 //有差异112-150 应该是操作系统的问题
    				while (bis.read(input)!=-1) {		//返回一个boolean表-完成与否
    					bos.write(input);
    					count++;
    				}
    				
    				
    				bis.close();
    				fis.close();
    				System.out.println("Read "+count +" Times");
    				bos.close();
    				fos.close();
    				System.out.println("Done");
    				
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    }
    

      

  • 相关阅读:
    jar 常用操作
    linux 加载新的磁盘(卷组)
    apache 代理配置
    spring boot datasource 参数设置
    svn 常用命令
    最详细的maven教程
    centos 用户组操作
    ubuntu命令行操作mysql常用操作
    Ruby-Clamp
    maven使用备忘
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4469076.html
Copyright © 2011-2022 走看看