1.文件常见方法
boolean flag=f.exists();         //文件是否存在
    flag=f.isFile();          //是否是文件
    flag=f.isDirectory();      //是否是目录
    str=f.getPath();        //获得文件的相对路径
    str=f.getAbsolutePath();   //获得文件的绝对路径
    str=f.getName();          //获得文件名
    flag=f.delete();         //删除文件
    flag=f.createNewFile();    //创建文件
    long=f.length(); //返回文件长度
  注意:File不能操作文件内容

   

public static void main(String[] args) {

  //File f=new File("文件路径")
  //"\\"表示转义\
   //"/"表示/
    File f=new File("e:/a.txt");
    System.out.println(f);
  
  //文件是否存在
  boolean flag=f.exists();
  System.out.println(flag);
  
  //是否是文件
   flag=f.isFile();
   System.out.println(flag);
  
   //是否是目录
   flag=f.isDirectory();
   System.out.println(flag);

  //获取文件相对路径
    String path=f.getPath();
      System.out.println(path);
 
   //获取文件的绝对路径
     path=f.getAbsolutePath();
     System.out.println(path);

  //获取名字
     String name=f.getName();
     System.out.println(name);
  
     //删除文件或者目录
     flag=f.delete();
     System.out.println(flag);

//创建(检查异常)
    try {
      flag=f.createNewFile();
     System.out.println(flag);
    } catch (IOException e) {
     e.printStackTrace();
   }

   //长度
    System.out.println(f.length());
}

 

2.InputStream/OutputStream
  文件:FileInputStream/FileOutputStream
  2.1 FileInputStream(输入流)
    数据从文件到java代码中。
    int read(); //读取一个字节
    int read(byte[]); //读取一串字节
    long avaliable; //文件长度
  2.2 FileInputStream(字节文件输入流)
    new FileInputStream(File);
    new FileInputStream("文件路径+文件名");

   

public static void main(String[] args) {
//文件内容
File f=new File("src/a.txt");
try {
//英文和中午翻译
//码表
InputStream is=new FileInputStream(f);
int b=is.read(); //1:1的unicode编码
System.out.println((char)b);
b=is.read();
System.out.println((char)b);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

 

  2.3 FileOutputStream(输出流)
    数据从java代码中,写到文件或者其他介质中。
    void write(字节); //写入一个字节
    void write(byte[]); //写入字节数组
  2.4 FileOutputStream
    new FileOutputStream(File);
    new FileOutputStream("文件路径+文件名");
    new FileOutputStream("文件路径+文件名",true);
  注意:a.boolean:表示是否向文件末尾追加,如果是true,表示追加,false表示不追加(也就是覆盖),默认值为false
     b.创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件。

   

public static void main(String[] args) throws Exception{
File f=new File("src/a.txt");
OutputStream os=new FileOutputStream(f);
os.write(97);
os.close();
}

 

3.Reader/Writer(字符流)

  3.1 FileReader

    int b=fr.read(); //读取一个字符
    int length=fr.read(char[]); //读取字符数组
  3.2 FileWriter
    fw.write(char); //写一个字符
    fw.write(char[]); //写字符数组
  3.3BufferedReader(字符输入缓冲流)
    BufferedReader br=new BufferedReader(new FileReader("文件路径"));
    String str=br.readLine(); //读取一行字符
  3.4BufferedWriter(字符输出缓冲流)
    BufferedWriter bw=new BufferedWriter(new FileWriter("文件路径"));
    bw.write(字符串);

  注意:a.能够用文本编辑器打开的文件,不乱码就是字符文件
     b.能够用文本编辑器打开乱码的,就是字节文件

public class TestChar {
public static void main(String[] args) throws Exception {
// read();
write();
}

/**
* 字符输出流Writer
* @throws IOException
*/
private static void write() throws IOException {
Writer w=new FileWriter("src/a.txt");
char[] cs={'钓','鱼','岛','是','中','国','的'};
w.write(cs);
w.close();
}

/**
* 字符输入流Reader
* @throws Exception
*/
private static void read() throws Exception{
Reader r=new FileReader("src/d.txt");
// int b=r.read();
// System.out.println((char)b);

char[] chars=new char[1024];
int length=r.read(chars);
System.out.println(Arrays.toString(chars));
}
}

 

public class TestBuffer {
public static void main(String[] args) throws Exception {
buReader();
buWriter();

}

private static void buWriter() throws Exception {
BufferedWriter bw=new BufferedWriter(new FileWriter("src/a.txt"));
bw.write("我是中国人");
//刷新缓存
bw.flush();
//默认执行flush(),关闭管道
bw.close();
}

private static void buReader() throws Exception {
BufferedReader br=new BufferedReader(new FileReader("src/d.txt"));
//读取一行记录
// String str=br.readLine();
// str=br.readLine();
// System.out.println(str);

String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
}

}

 

I/O的方法、输入流和输出流的更多相关文章

  1. java中的IO流(输入流与输出流)概述与总结

    Java中IO流,输入输出流概述与总结 总结的很粗糙,以后时间富裕了好好修改一下. 1:Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都 ...

  2. C++输入流和输出流、缓冲区

    一.C++输入流和输出流 输入和输出的概念是相对程序而言的. 键盘输入数据到程序叫标准输入,程序数据输出到显示器叫标准输出,标准输入和标准输出统称为标准I/O,文件的输入和输出叫文件I/O. cout ...

  3. 牛客网Java刷题知识点之输入流、输出流、字节流、字符流、字节流的抽象基类(InputStream、OutputStream)、字符流的抽象基类(Reader、Writer)、FileWriter、FileReader

    不多说,直接上干货! IO流用来处理设备之间的数据传输. java对数据的操作是通过流的方式. java用于操作流的对象都在IO包中. IO流按操作数据分为两种:字节流和字符流. IO流按流向分为:输 ...

  4. [Java开发之路](8)输入流和输出流

    1. Java流的分类 按流向分: 输入流: 能够从当中读入一个字节序列的对象称作输入流. 输出流: 能够向当中写入一个字节序列的对象称作输出流. 这些字节序列的来源地和目的地能够是文件,并且通常都是 ...

  5. Java的IO流以及输入流与输出流的异同

    一:流的基本概念:           Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.J ...

  6. Java:字节流和字符流(输入流和输出流)

    本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...

  7. 转载:Java:字节流和字符流(输入流和输出流)

    本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...

  8. java基础49 IO流技术(对象输入流/对象输出流)

    1.对象输入输出流 对象注意作用是用于写对象信息与读取对象信息 1.对象输出流:ObjectOutputStream    2.对象输入流:ObjectInputStream 2.对象输入输出流的步骤 ...

  9. Java基础:浅谈数据输入流/数据输出流《DataInputstream类与DataOutputstream类》

     一.理论概述 数据输入/输出流(DataInputStream类与DataOutputStream类) 允许应用程序以与机器无关的方式从底层输入流中读取基本Java数据类型. 说白了就是,当读取一个 ...

随机推荐

  1. (转) latch 入门

    原链接:http://www.itpub.net/thread-1424719-1-1.html (入门1)一直想点文章关于Latch的,又一直没写,一是因为懒,二是一直觉得现在关于Latch的书那么 ...

  2. PHP 多字节字符串 函数

    参考资料 多字节字符编码方案和他们相关的问题相当复杂,超越了本文档的范围. 关于这些话题的更多信息请参考以下 URL 和其他资源. Unicode materials » http://www.uni ...

  3. Loadrunner场景设计篇——负载生成器

    1  简介 当执行一个场景时,Controller把场景中的每个用户配到负载生成器(Load generator). 所谓的负载生成器(Load Generator)就是执行Vuser脚本,运行Vus ...

  4. DB 数据同步到数据仓库的架构与实践

    背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的ODS数据有业务日志数据(Log)和业务DB数据( ...

  5. Kotlin学习记录2

    参考我的博客:http://www.isedwardtang.com/2017/09/03/kotlin-primer-2/

  6. nginx ip无法访问

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭firewall: systemctl stop firewalld.service #停止f ...

  7. 【leetcode刷题笔记】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. cisco笔记

    交换机 show cdp neighbors 显示邻居信息 路由 show ip interface brief 显示接口ip

  9. centos7下安装ngnix1.8.1

    参考 http://www.linuxidc.com/Linux/2016-09/134907.htm 安装依赖 openssl zlib pcre gcc 下载安装包 [root@localhost ...

  10. fix LayerKit framework不能提交App Store

    - 问题: - 原因 x86_64, i386是ios模拟器用的architectures.发布时,不支持这两种.但是,默认编译出来的layerkit framework支持这两种编译器 - 解决办法 ...