码迷,mamicode.com
首页 > 编程语言 > 详细

java中的IO 的示例

时间:2018-01-03 11:49:34      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:==   lin   图片   try   catch   post   port   leo   sts   

字符流

package jd_1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class jd_1 {

 /**
  * 字符流
  *
  * @param args
  */
 public static void main(String[] args) {
  // 创建BufferedReader用于读取文件
  BufferedReader reader = null;
  // 创建BufferedWriter用于写入文件
  BufferedWriter writer = null;
  // 创建FileReader用于保存读入的路径
  FileReader fis = null;
  try {
   fis = new FileReader("F:\\java IO\\简答copy\\source.txt");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // 创建FileWriter用于保存写入的路径
  FileWriter fw = null;
  try {
   fw = new FileWriter("F:\\java IO\\简答copy\\targetcopy.txt");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  reader = new BufferedReader(fis);
  writer = new BufferedWriter(fw);
  String line = null;
  // 读取的是字符串
  try {
   // while ((line = reader.readLine()) != null) {
   // try {
   // Thread.sleep(500);
   // } catch (InterruptedException e) {
   // // TODO Auto-generated catch block
   // e.printStackTrace();
   // }
   // writer.write(line);
   // }
   // 通过数组作为中转站
   char[] c = new char[1024];
   StringBuffer buffer = new StringBuffer();
   int legin = fis.read(c);
   while (legin != -1) {
    buffer.append(c);
    legin = fis.read();
    fw.write(c);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   System.err.println("copy成功");
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (writer != null) {
    try {
     writer.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}

字节流

public class copy {
 public static void main(String[] args) {
  // 复制前
  FileInputStream file = null;
  // 复制后
  FileOutputStream file1 = null;
  try {
   // 复制前的路径
   file = new FileInputStream("F:\\java IO\\新建文本文档.txt");
   // 复制后的路径
   file1 = new FileOutputStream("F:\\java IO\\copy.txt");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // 创建一个字符数组作为中转站
  byte[] Words = new byte[1024];
  // 记录数组的长度
  int len;
  try {
   while ((len = file.read(Words)) != -1) {
    file1.write(Words, 0, Words.length);
    System.out.println("copy成功");
   }
  } catch (IOException e) {

   e.printStackTrace();
  } finally {
   if (file != null) {
    try {
     file.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (file1 != null) {
    try {
     file1.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}

二进制流

package bdqn4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 二进制读取
 *
 * @author Administrator
 *
 */
public class Text {
 public static void main(String[] args) {
  // 创建流
  // 实现读写操作
  // 关闭流

  DataInputStream dis = null;
  DataOutputStream dos = null;

  FileInputStream fis = null;
  try {
   fis = new FileInputStream("F:\\java IO\\原图片.jpg");
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream("F:\\java IO\\copy图片\\copy.jpg");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  dis = new DataInputStream(fis);
  dos = new DataOutputStream(fos);
  int temp;
  try {
   while ((temp = dis.read()) != -1) {
    dos.write(temp);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   System.err.println("转移成功");
   if (dis != null) {
    try {
     dis.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (dos != null) {
    try {
     dos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}

判断IO的的各种属性及其方法

package bdqn1;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class text {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  // 实例化对象指点判断路径
  File file = new File("F:\\java IO\\新建文本文档.txt");
  // 判断.tet是否存在
  if (file.exists()) {
   System.out.println("当前文件存在");
   System.out.println("文件的完整路径" + file.getAbsolutePath());
   System.out.println("文件名" + file.getName());
   System.out.println("文件的相对路径" + file.getPath());
   System.out.println("文件的上一级目录" + file.getParent());
   System.out.println("文件的长度" + file.length());
   if (file.isDirectory()) {
    System.out.println("当前是文件夹");
   } else {
    System.out.println("当前是不是文件夹");
    System.err.println("请输入1删除");
    int number = input.nextInt();
    if (number == 1) {
     boolean bool = file.delete();
     if (bool) {
      System.out.println("删除成功");
     }
    }
   }
  } else {
   System.out.println("当前文件不存在");
   try {
    boolean bool = file.createNewFile();
    if (bool) {
     System.out.println("创建成功");
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

java中的IO 的示例

标签:==   lin   图片   try   catch   post   port   leo   sts   

原文地址:https://www.cnblogs.com/wh1520577322/p/8182258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!