java 檔案與流

來源:互聯網
上載者:User

java 檔案與流的總結

一 java 讀寫檔案

 Java的IO操作都是基於流進行操作的,為了提高讀寫效率一般需要進行緩衝。

    簡單的樣本程式如下:
    
    /**
 * 讀出file1中的內容,寫入file2中
 *
 */

BufferedReader reader=new BufferedReader(new FileReader(file1));
    BufferedWriter writer=new BufferedWriter(new FileWriter(file2));
    
    String str=null;
     str=reader.readLine();
    while(str!=null)
    {
     writer.write(str+"/r/n");
     
     str=reader.readLine();
    }
    reader.close();
    writer.close(); 

完整的樣本:

import java.io.*;
public class FileReading {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub 

  System.out.println("input the direction's path:");
  BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
  
  try
  {
   String path=reader.readLine();
   String fileName;
   makeDir(path);
   System.out.println("input the filename:");
   fileName=reader.readLine();
   File file=makeFile(path,fileName);
   writeFile(file);

System.out.println("input the file name or the direction:");
   BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));   
   File f=new File(buff.readLine().toString());
   listFile(f);
  }
  catch(Exception ex)
  {
   System.err.println(ex.getMessage());
  }
  
 }
 
 public static void makeDir(String path)//建立目錄,---檔案夾
 {
  try
  {
   File file=new File(path);   
   if(!file.exists())
    file.mkdir();   
    
  }
  catch(Exception ex)
  {
   System.err.println(ex.getMessage());
  }
  
 }
 
 public static File makeFile(String path,String fileName)//建立檔案
 {
  File f=new File(path,fileName);
  try
  {   
   if(!f.exists())
    f.createNewFile();   
  }
  catch(Exception ex)
  {
   System.err.println(ex.getMessage());
  }
  finally
  {
   return f;
  }
 }
 
 public static void writeFile(File file)//將一個檔案中的內容寫入另一個檔案
 {
  try
  {
   if(!file.exists())
    file.createNewFile();
   else
   {
    System.out.println("input the file:");
    BufferedReader buff=new BufferedReader(new InputStreamReader(System.in)); 
    String pathname=buff.readLine();
    File f=new File(pathname);
    
    BufferedReader reader=new BufferedReader(new FileReader(f));
    BufferedWriter writer=new BufferedWriter(new FileWriter(file));
    
    String str=null;
     str=reader.readLine();
    while(str!=null)
    {
     writer.write(str+"/r/n");
     
     str=reader.readLine();
    }
    reader.close();
    writer.close();
   }
  }
   catch(Exception ex)
   {
    System.err.println(ex.getMessage());
   }

  
 }

public static void listFile(File file)//列出所有目錄及檔案
 {  
  File[] list= file.listFiles();
  int filecout=0;
  int dircout=0;
  for(File f:list)
  {
   if(f.isDirectory())
   {
    System.out.println("this is direction "+(++dircout)+" :"+f.getName());
   }
   else if(f.isFile())
   {
    System.out.println("This is file:"+(++filecout)+" :"+f.getName());
   }
  }
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.