java文件流

 //读取二进制文件
 public static void ReadBinaryFile()
 {
  System.out.println("请输入你要读的文件路径:");
  Scanner in = new Scanner(System.in);
     String path = in.next();
  try {
   FileInputStream inputFile = new FileInputStream(path);
   DataInputStream input = new DataInputStream(inputFile);
   FileOutputStream outFile = new FileOutputStream("d://first.class");
   DataOutputStream output = new DataOutputStream(outFile);
   int size = input.available();
   byte[] me = new byte[size];
   input.read(me);
   output.write(me);
   output.close();
   outFile.close();
   input.close();
   inputFile.close();
   System.out.println("文件复制成功");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void BufferWriter()
 {
//  使用缓冲区存数据 此种效率高
  System.out.println("请输入你要写入的文件路径:");
  Scanner in = new Scanner(System.in);
     String path = in.next();
     try {
   FileWriter fw = new FileWriter(path);
   BufferedWriter bw = new BufferedWriter(fw);
   System.out.println("请输入你要写进文件的内容");
   //bw.write(in.next());
   //bw.newLine();
   bw.append(in.next());//添加数据
   bw.close();
   fw.close();
   System.out.println("文件写入成功");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void BufferRead()
 {
  //使用缓冲区存数据 此种效率高
  System.out.println("请输入你要读的文件路径:");
  Scanner in = new Scanner(System.in);
     String path = in.next();
  try {
   FileReader fr = new FileReader(path);
   BufferedReader br = new BufferedReader(fr);
   String str = null;
    while((str=br.readLine())!=null)
    {
     System.out.println(str);
    }
   br.close();
   fr.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  };
 }
 
 public void WriteFile()
 {
  System.out.println("请输入你要写入的文件路径:");
  Scanner in = new Scanner(System.in);
     String path = in.next();
     try {
      //写入流
   OutputStream file = new FileOutputStream(path);
   System.out.println("请输入要写入地文件内容:");
   String str = in.next();
   byte[] words = str.getBytes();
   file.write(words);
   file.close();
   System.out.println("写入成功");
   } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void ReadFile()
 {
  System.out.println("请输入你要读取的文件路径:");
  Scanner in = new Scanner(System.in);
     String path = in.next();
     try {
//      读入流
   InputStream file = new FileInputStream(path);
   int size;
    size = file.available();
   System.out.println("文件可读取的最大数:"+size);
   byte[] text = new byte[size];
    file.read(text,0,size);
    String s = new String(text);
    System.out.println(s);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 } 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值