java文件读写和properties文件的读取

1、路径分为绝对路径和相对路径。
绝对路径是指真实的路径,相对路径是相对于项目的名字所在的路径的,比如当前项目为
testfile,其下有个文件夹resource,resource下有个文件aa.txt,则txt的相对路径为resource/aa.txt
创建文件对象是使用下面的格式File file = new File("resource/aa.txt");


2、判断文件是否存在
        File file = new File("resource/aa.txt");
  if(!file.exists())
  {
      return false;
  }
  
3、读取properties文件
方法1:文件在类路径下,比如bb.properties在src/testfile/bb.properties下,则读取该文件方法如下
InputStream in = TestClassPathProperties.class.getClassLoader()
    .getResourceAsStream("testfile/bb.properties");
  
  Properties pro = new Properties();
  
  try
  {
   pro.load(in);
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


方法2:文件不在类路径下,比如bb.properties在与src同级的config目录下,那么读取该文件的方法如下
Properties pro1 = new Properties();
  
  File file = new File("config/bb.properties");
  try
  {
   InputStream ins = new FileInputStream(file.getAbsoluteFile());
   pro1.load(ins);
   
   
  }
  catch (Exception e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

4、读写文件常用的类为FileInputStream和FileReader,前面是字节流类,后面是字符类。


5、读写字节流的使用方法如下:
读字节流
File file = new File("src/testfile/bb.txt");
  
  // 用于缓冲字节流,一般大小为1K
  byte[] bytes = new byte[1024];
  int n;
  FileInputStream fis = null;
  try
  {
   fis = new FileInputStream(file);
   
   // 文件结束时返回值为-1
   while((n = fis.read(bytes)) != -1)
   {
    // 将第0到n个字节解码为新的字符串
    System.out.println(new String(bytes,0,n));
   }
   
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  finally
  {
   try
   {
    // 关闭字节流
    fis.close();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }


写字节流
File fileWrite = new File("D:\\test.txt");
  String str = "aaaa写文件";
  FileOutputStream fos = null;
  
  try
  {
   fos = new FileOutputStream(fileWrite);
   fos.write(str.getBytes());
  }
  catch (Exception e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally
  {
   try
   {
    fos.close();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }  

利用输入字节流和输出字节流拷贝文件

  File fileIn = new File("config/I18n.png");
  File fileOut = new File("config/custom/I18n.png");
  FileInputStream fis = null;
  FileOutputStream fos = null;
  
  try {
   fis = new FileInputStream(fileIn);
   fos = new FileOutputStream(fileOut);
   byte[] by = new byte[1024];
   
   while(fis.read(by) != -1)
   {
    fos.write(by);
   }
  }
  catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally
  {
   try
   {
    fos.close();
    fis.close();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }

 }

 

6、字符的读,FileReader
      字符的写,FileWriter
   演示从一个文件拷贝到另一个文件
     FileReader fr = null;
     FileWriter fw = null;
     
     try
     {
      fr = new FileReader("D:\\a.txt");
      fw = new FileWriter("D:\\java\\cc.txt");
      //读取到缓冲区
      char[] buffer = new char[1024];
      int n = 0;
      while ((n = fr.read(buffer)) != -1)
      {
       // 读取时指定其实字符和结束字符,因为有时1024长度的缓冲区不一定是满的
       // 防止不满时,出现乱码情况
       fw.write(buffer, 0, n);
      }
     }
     catch(Exception e)
     {
      e.printStackTrace();
     }
     finally
     {
      try
      {
          fr.close();
    fw.close();
   }
      catch (IOException e)
      {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
  
7、一行一行的读取:BufferReader
     一行一行的写入:BufferWriter
  
     FileReader fr = null;
  FileWriter fw = null;
  BufferedReader br = null;
  BufferedWriter bw = null;
  String str = "";
  
  try
  {
   fr = new FileReader("D:\\a.txt");
   br = new BufferedReader(fr);
   
   fw = new FileWriter("D:\\fw.txt");
   bw = new BufferedWriter(fw);
   while ((str = br.readLine()) != null)
   {
    bw.write(str + "\r\n");
   }
  }
  catch (Exception e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally
  {
   try
   {
       bw.close();
    fw.close();
    br.close();
    fr.close();   
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }

 8、参考文章

http://353588249-qq-com.iteye.com/blog/780343

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值