JAVA中File的相关操作

  1. Creation of a file if it does not exist; Deletion of a file if it already exists.
    package com.han;
    
    import java.io.File;
    
    /**
     * Creation of a file if it does not exist;
     * Deletion of a file if it already exists.
     * @author han
     *
     */
    public class FileTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		File file=new File("/home/han/Documents","word.txt");
    		if(file.exists()){
    			System.out.println(file.getPath());
    			file.delete();
    			System.out.println("文件已删除");
    		}else{
    			try{
    				file.createNewFile();
    				System.out.println("文件已创建");
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    
    	}
    
    }
    


  2. Get a specified file properties, including its name, length, and existence or if it is hidden.
    package com.han;
    
    import java.io.File;
    
    /**
     * Get a specified file properties, 
     * including its name, length, and existence or if it is hidden.
     * @author han
     *
     */
    public class FileTest2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		File file=new File("/home/han/Documents","word.txt");
    		if(file.exists()){
    			String name=file.getName();
    			long length=file.length();
    			boolean hidden=file.isHidden(); //if the file is hidden or not.
    			System.out.println("文件名称: "+name);
    			System.out.println("文件长度: "+length);
    			System.out.println("该文件是隐藏文件吗? "+hidden);
    		}else{
    			try{
    				System.out.println("该文件不存在");
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    


  3. write a string content to a text file using a FileInputStream and FileOutputStream
    package com.han;
    
    import java.io.*;
    
    /**
     * write a string content to a text file 
     * using a FileInputStream and FileOutputStream
     * @author han
     *
     */
    public class FileTest3 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		File file=new File("/home/han/Documents","word.txt");
    		try{
    			FileOutputStream out=new FileOutputStream(file); //创建输出流
    			byte[] content_to_output="我有一只小毛驴,我从来也不骑。".getBytes();
    			out.write(content_to_output);
    			out.close(); //关闭输出流
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		
    		try{
    			FileInputStream in=new FileInputStream(file); //创建输入流
    			byte[] content_to_input=new byte[1024];
    			int len=in.read(content_to_input);
    			in.close(); //关闭输入流
    			System.out.println(new String(content_to_input,0,len));
    			System.out.println("文件长度为: "+len);
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    


  4. Rename a file.
    package com.han;
    
    import java.io.*;
    
    /**
     * Rename a file.
     * @author han
     *
     */
    public class FileTest4 {
    	public static void main(String[] args) {
    		/*Use to renameTo() method of the File class to complete the rename job*/
    		File file=new File("/home/han/Documents","word.txt");
    		File file_renamed=new File("/home/han/Documents","word_renamed.txt");
    		try{
    			FileOutputStream out=new FileOutputStream(file); //创建输出流
    			byte[] content_to_output="我有一只小毛驴,我从来也不骑。".getBytes();
    			out.write(content_to_output);
    			out.close(); //关闭输出流
    			file.renameTo(file_renamed);
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		/*Use the stream to copy a file to another*/
    		try{
    			FileInputStream in=new FileInputStream(file_renamed); //创建输入流
    			File file_copy=new File("/home/han/Documents","word_copy.txt");
    			FileOutputStream out_copy=new FileOutputStream(file_copy); //创建副本输出流
    			/*int input_byte=in.read(); 
    			while(input_byte!=-1){
    				out_copy.write(input_byte);
    				input_byte=in.read();
    			}*/
    			int input_byte=0;
    			while((input_byte=in.read())!=-1){ //(input_byte=in.read())!=-1 这种形式要会使用
    				out_copy.write(input_byte);
    			}
    			out_copy.close(); //关闭副本输出流
    			in.close(); //关闭输入流
    			System.out.println("副本文件名称为: "+file_copy.getName());
    			System.out.println("副本文件长度为: "+file_copy.length());
    			System.out.println("副本文件是隐藏文件吗? "+file_copy.isHidden());
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    


  5. write a string content to a specified text file. and then read it to the default command screen.

    package com.han;
    
    import java.io.*;
    
    /**
     * write a string content to a specified text file.
     * and then read it to the default command screen.
     * <p>
     * It used the FileReader and FileWriter, 
     * moreover, the BufferedReader and BufferedWriter.
     * @author han
     *
     */
    public class FileTest5 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String[] content={"好久不见","最近好吗","常联系"};
    		File file=new File("/home/han/Documents","word.txt");
    		try {
    			FileWriter out=new FileWriter(file);
    			BufferedWriter bw=new BufferedWriter(out);
    			for (int i=0;i<content.length;i++){
    				bw.write(content[i]);
    				bw.newLine();
    			}
    			bw.close();
    			out.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			FileReader in=new FileReader(file);
    			BufferedReader br=new BufferedReader(in);
    			String s=null;
    			int i=0;
    			while((s=br.readLine())!=null){
    				System.out.println("第"+(i+1)+"行: "+s);
    				i++;
    			}
    			br.close();
    			in.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    }
    

    It used the FileReader and FileWriter, moreover, the BufferedReader and BufferedWriter.


posted on 2012-01-02 18:30  java课程设计例子  阅读(233)  评论(0编辑  收藏  举报