字符流

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 的示例的更多相关文章

  1. Java中的IO流(六)

    上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...

  2. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  3. java中的IO操作总结

    一.InputStream重用技巧(利用ByteArrayOutputStream) 对同一个InputStream对象进行使用多次. 比如,客户端从服务器获取数据 ,利用HttpURLConnect ...

  4. java中的IO流

    Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...

  5. Java中的IO流总结

    Java中的IO流总结 1. 流的继承关系,以及字节流和字符流. 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Buffe ...

  6. Java 中的 IO 与 socket 编程 [ 复习 ]

    一.Unix IO 与 IPC Unix IO:Open-Read or Write-Close IPC:open socket - receive and send to socket - clos ...

  7. 深入理解Java中的IO

    深入理解Java中的IO 引言:     对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java >   本文的目录视图如下: ...

  8. Java中的IO流大体介绍

    由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...

  9. Java中的IO流,Input和Output的用法,字节流和字符流的区别

    Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...

随机推荐

  1. 嵌入式linux------ffmpeg移植 解码H264(am335x解码H264到yuv420并通过SDL显示)

    /* 编译命令:arm-linux-gcc -o show2642 264showyuv2.c -I/usr/local/ffmpeg_arm/include/ -L/usr/local/ffmpeg ...

  2. ArgumentError:Error #2004:某个参数无效

    1.错误描述 ArgumentError:Error #2004:某个参数无效 at flash display::Graphics/drawRect() at ZeroClipboard() 2.错 ...

  3. cookie的设置和获取

    // 创建cookiefunction setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeU ...

  4. BFS学习 Codeforces 301_div.2_Ice Cave

    C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. Linux之23个重要命令

    作为工作几年的Linux运维老司机,总结了Linux命令行的常用的一些用法,希望对您有所收获. 1. 搜索 在vi和vim中如果打开一个很大的文件,不容易找到对应的内容,可以使用自带的搜索关键字进行搜 ...

  6. CentOS 7离线安装MySQL 5.7

    系列文章首发平台为果冻想个人博客.果冻想,是一个原创技术文章分享网站.在这里果冻会分享他的技术心得,技术得失,技术人生.我在果冻想等待你,也希望你能和我分享你的技术得与失,期待. 前言 网上已经有那么 ...

  7. jsp学习笔记之:内置对象

    application对象: 设置一个名为name,值为val的应用内共享的数据 <% application.setAttribute("name",val); %> ...

  8. 几道很Interesting的偏序问题

    若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...

  9. Luogu3092:[USACO13NOV]No Change

    题面 传送门 Sol 状压一下\(k\),\(f[S]\)表示用过的硬币集合为\(S\)能买到的物品个数 # include <bits/stdc++.h> # define RG reg ...

  10. oracle 裸设备划分 --centos6.5

    主题思想:物理卷PV->卷组VG->逻辑卷LV(类型:raw)->添加表空间 操作过程 vg_orcl         8g 一:划分 二:创建裸设备 方法1:目前最常用的方法 #c ...