IO

来源:互联网 发布:手机淘宝怎么求购 编辑:程序博客网 时间:2024/06/03 22:06

一、File

  1.File:

       mkdir():创建一个目录,并且父目录必须存在。
       mkdirs():创建一个目录,如果父目录不存在就一并创建。
  2. FileInputStream,FileOutputStream 字节流
  3. FileReader() FileWriter() 处理文本文件的字符流,可以实现文本文件的复制,对于非文本文件(视频文件,图片文件等)只能使用字节流!
@Testpublic void test1() throws IOException {File file = new File("D:\\workspace\\Java\\test.txt");if (file.exists()) {System.out.println("文件名称:" + file.getName());System.out.println(file.getName() + (file.isDirectory() ? "是一个目录。" : "不是一个目录"));System.out.println(file.getName() + (file.isFile() ? "是一个文件。" : "不是一个文件"));System.out.println(file.getName() + (file.isHidden() ? "是一个隐藏文件。" : "不是一个隐藏文件"));System.out.println("最后一次修改日期:" + new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date(file.lastModified())));System.out.println("文件大小:" + new BigDecimal(file.length() / (double) 1024 / 1024).divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP).doubleValue() + "M");System.out.println("字节:" + file.length());System.out.println(file.getAbsolutePath());System.out.println(file.getCanonicalPath());System.out.println(file.getPath());}}

二、缓冲流

   缓冲流操作:
          BufferedInputStream
          BufferedOutputStream    (flush())
          BufferedReader
          BufferedWriter            (flush())
      可以提升文件操作的效率


 

@Testpublic void testFileCopy(){long start = System.currentTimeMillis();String src = "C:\\Users\\ASUS\\Desktop\\1.mp4";String dest = "C:\\Users\\ASUS\\Desktop\\2.mp4";copyFile(src, dest);long end = System.currentTimeMillis();System.out.println("花费的时间是:"+(double)(end - start)/1000);}//实现文件的复制public void copyFile(String src,String dest){File file1 = new File(src);File file2 = new File(dest);BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);byte[] b = new byte[1024];int len;while((len = bis.read(b)) != -1){bos.write(b, 0, len);bos.flush();}}catch (IOException e) {e.printStackTrace();}finally{if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}if (bos != null) {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}}


三、转换流

   转换流:InputStreamReader   OutputStreamWriter
     编码:字符串 --> 字节数组
     解码:字节数组 --> 字符串

@Testpublic void test() {BufferedReader br = null;BufferedWriter bw = null;try {// 编码File file = new File("集合类.txt");FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis, "GBK");br = new BufferedReader(isr);// 解码File file2 = new File("集合类4.txt");FileOutputStream fos = new FileOutputStream(file2);OutputStreamWriter osw = new OutputStreamWriter(fos);bw = new BufferedWriter(osw);String str = null;while ((str = br.readLine()) != null) {// System.out.println(str); //输出内容bw.write(str);bw.newLine();bw.flush();}} catch (IOException e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}}


原创粉丝点击