文章目录

  • 1. File类
  • 2. RandomAccessFile类
  • 3. 流类
    • 3.1 字节流
    • 3.2 字符流
    • 3.3 管道流
    • 3.4 ByteArrayInputStream、ByteArrayOutputStream
    • 3.5 System.in、System.out
    • 3.6 打印流 PrintStream
    • 3.7 DataInputStream、DataOutputStream
    • 3.8 合并流
    • 3.9 字节流与字符流的转换
    • 3.10 IO包类层次关系
  • 4. 字符编码
  • 5. 对象序列化

1. File类

File 类 是 java.io 包中唯一代表磁盘文件本身的对象

  • File(String dirPath) 构造生成 File 对象
import java.io.File;class FileDemo {public static void main(String[] args){File f = new File("file.txt");if(f.exists())f.delete();else{try{f.createNewFile();}catch(Exception e){System.out.println(e.getMessage());}}System.out.println("getName()获取文件名:"+f.getName());System.out.println("getPath()获取文件路径:"+f.getPath());System.out.println("getAbsolutePath()绝对路径:"+f.getAbsolutePath());System.out.println("getParent()父文件夹名:"+f.getParent());System.out.println("exists()文件存在吗?"+f.exists());System.out.println("canWrite()文件可写吗?"+f.canWrite());System.out.println("canRead()文件可读吗?"+f.canRead());System.out.println("isDirectory()是否是目录?"+f.isDirectory());System.out.println("isFile()是否是文件?"+f.isFile());System.out.println("isAbsolute()是否是绝对路径名称?"+f.isAbsolute());System.out.println("lastModified()最后修改时间:"+f.lastModified());System.out.println("length()文件长度-字节单位:"+f.length());}
}

输出:

getName()获取文件名:file.txt
getPath()获取文件路径:file.txt
getAbsolutePath()绝对路径:D:\gitcode\java_learning\file.txt
getParent()父文件夹名:null
exists()文件存在吗?true
canWrite()文件可写吗?true
canRead()文件可读吗?true
isDirectory()是否是目录?false
isFile()是否是文件?true
isAbsolute()是否是绝对路径名称?false
lastModified()最后修改时间:1614680366121
length()文件长度-字节单位:0

2. RandomAccessFile类

  • 随机跳转到文件的任意位置处读写数据,该类仅限于操作文件
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;class FileDemo {public static void main(String[] args){File f = new File("file.txt");if(f.exists())f.delete();else{try{f.createNewFile();}catch(Exception e){System.out.println(e.getMessage());}}System.out.println("name获取文件名:"+f.getName());System.out.println("getPath()获取文件路径:"+f.getPath());System.out.println("getAbsolutePath()绝对路径:"+f.getAbsolutePath());System.out.println("getParent()父文件夹名:"+f.getParent());System.out.println("exists()文件存在吗?"+f.exists());System.out.println("canWrite()文件可写吗?"+f.canWrite());System.out.println("canRead()文件可读吗?"+f.canRead());System.out.println("isDirectory()是否是目录?"+f.isDirectory());System.out.println("isFile()是否是文件?"+f.isFile());System.out.println("isAbsolute()是否是绝对路径名称?"+f.isAbsolute());System.out.println("lastModified()最后修改时间:"+f.lastModified());System.out.println("length()文件长度-字节单位:"+f.length());}
}class Employee1{String name;int age;final static int LEN = 8;public Employee1(String name, int age){if(name.length() > LEN){name = name.substring(0,8);}else {while(name.length() < LEN)name = name + " ";}this.name = name;this.age = age;}
}
class RandomFileDemo{public static void main(String[] args) throws Exception{Employee1 e1 = new Employee1("Michael___",18);Employee1 e2 = new Employee1("Ming",19);Employee1 e3 = new Employee1("ABC",20);RandomAccessFile ra = new RandomAccessFile("employee.txt","rw");ra.write(e1.name.getBytes());ra.writeInt(e1.age);ra.write(e2.name.getBytes());ra.writeInt(e2.age);ra.write(e3.name.getBytes());ra.writeInt(e3.age);ra.close();RandomAccessFile raf = new RandomAccessFile("employee.txt","r");int len = 8;raf.skipBytes(12);//跳过第一个员工信息,名字8字节,年龄4字节System.out.println("第2个员工信息:");String str = "";for(int i = 0; i < len; ++i)str = str+(char)raf.readByte();System.out.println("name: "+str);System.out.println("age: "+raf.readInt());System.out.println("第1个员工的信息:");raf.seek(0);//移动到开始位置str = "";for(int i = 0; i < len; ++i)str = str+(char)raf.readByte();System.out.println("name: "+str.trim());System.out.println("age: "+raf.readInt());System.out.println("第3个员工的信息:");raf.skipBytes(12); // 跳过第2个员工信息str = "";for(int i = 0; i < len; ++i)str = str+(char)raf.readByte();System.out.println("name: "+str.trim());System.out.println("age: "+raf.readInt());raf.close();}
}

输出:

第2个员工信息:
name: Ming
age: 19
第1个员工的信息:
name: Michael_
age: 18
第3个员工的信息:
name: ABC
age: 20进程已结束,退出代码为 0

3. 流类

  • InputStream、OutputStream 字节流(处理字节、二进制对象)
  • Reader、Writer 字符流(字符、字符串)

处理流程:

  • 使用 File 类找到文件
  • 通过 File 类对象实例化 流的子类
  • 进行字节、字符的读写操作
  • 关闭文件流

3.1 字节流


import java.io.*;class IoDemo {public static void main(String[] args){// 写文件File f = new File("file.txt");FileOutputStream out = null;try{out = new FileOutputStream(f);}catch (FileNotFoundException e){e.printStackTrace();}byte b[] = "Hello Michael!".getBytes();try{out.write(b);}catch (IOException e){e.printStackTrace();}try{out.close();}catch (IOException e){e.printStackTrace();}// 读文件FileInputStream in = null;try{in = new FileInputStream(f);}catch (FileNotFoundException e){e.printStackTrace();}byte b1[] = new byte[1024];//开辟空间接收文件读入进来int i = 0;try{i = in.read(b1);//返回读入数据的个数}catch(IOException e){e.printStackTrace();}try{in.close();}catch (IOException e){e.printStackTrace();}System.out.println(new String(b,0,i));// Hello Michael!}
}

3.2 字符流

class CharDemo {public static void main(String[] args){// 写文件File f = new File("file.txt");FileWriter out = null;try{out = new FileWriter(f);}catch (IOException e){e.printStackTrace();}String str= "Hello Michael!";try{out.write(str);}catch (IOException e){e.printStackTrace();}try{out.close();}catch (IOException e){e.printStackTrace();}// 读文件FileReader in = null;try{in = new FileReader(f);}catch (FileNotFoundException e){e.printStackTrace();}char c1[] = new char[1024];//开辟空间接收文件读入进来int i = 0;try{i = in.read(c1);//返回读入数据的个数}catch(IOException e){e.printStackTrace();}try{in.close();}catch (IOException e){e.printStackTrace();}System.out.println(new String(c1,0,i));// Hello Michael!}
}

3.3 管道流

  • 主要用于连接两个线程间的通信
  • PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;class Sender extends Thread{private PipedOutputStream out = new PipedOutputStream();public PipedOutputStream getOutputStream() {return out;}public void run(){String s = new String("hello, Michael!");try{out.write(s.getBytes());//写入,发送out.close();}catch(IOException e){System.out.println(e.getMessage());}}
}class Receiver extends Thread{private PipedInputStream in = new PipedInputStream();public PipedInputStream getInputStream(){return in;}public void run(){String s = null;byte buf[] = new byte[1024];try{int len = in.read(buf);s = new String(buf, 0, len);System.out.println("收到以下讯息:"+s);in.close();}catch(IOException e){System.out.println(e.getMessage());}}
}class PipedStreamDemo {public static void main(String[] args){try{Sender sender = new Sender();Receiver receiver = new Receiver();PipedOutputStream out = sender.getOutputStream();PipedInputStream in = receiver.getInputStream();out.connect(in); // 将输出发送到输入sender.start();receiver.start();}catch (IOException e){System.out.println(e.getMessage());}}
}
// 输出 : 收到以下讯息:hello, Michael!

3.4 ByteArrayInputStream、ByteArrayOutputStream

  • 如果程序要产生一些临时文件,可以采用虚拟文件方式实现(使用这两个类)
class ByteArrayDemo{public static void main(String[] args) throws Exception{String tmp = "abcdefg**A";byte[] src = tmp.getBytes(); // src 为转换前的内存块ByteArrayInputStream input = new ByteArrayInputStream(src);ByteArrayOutputStream output = new ByteArrayOutputStream();new ByteArrayDemo().transform(input, output);byte[] result = output.toByteArray(); // result为转换后的内存块System.out.println(new String(result));// ABCDEFG**A}public void transform(InputStream in, OutputStream out){int c = 0;try{while((c=in.read()) != -1)//没有读到流的结尾(-1){int C = (int) Character.toUpperCase((char)c);out.write(C);}}catch (IOException e){e.printStackTrace();}}
}

3.5 System.in、System.out

  • System.in 对应键盘,属于 InputStream
  • Sytem.out 对应显示器,属于 PrintStream

3.6 打印流 PrintStream

class SystemPrintDemo{public static void main(String[] args){PrintWriter out = new PrintWriter(System.out);out.print("hello Michael");out.println("hello Michael");out.close();}
}

输出:

hello Michaelhello Michael进程已结束,退出代码为 0
class FilePrint{public static void main(String[] args){PrintWriter out = null;File f = new File("file1.txt");try{out = new PrintWriter(new FileWriter(f));}catch (IOException e){e.printStackTrace();}out.print("Hello Michael!!!");out.close();}
}

3.7 DataInputStream、DataOutputStream

import java.io.*;class DataStreamDemo {public static void main(String[] args) throws Exception{// 将数据写入文件DataOutputStream out = new DataOutputStream(new FileOutputStream("order.txt"));double prices[] = {18.99, 9.22, 14.22, 5.22, 4.21};int units[] = {10, 10, 20, 39, 40};String [] name = {"T恤衫", "杯子", "洋娃娃", "大头针", "钥匙链"};for(int i = 0; i < prices.length; ++i){//写入价格out.writeDouble(prices[i]);out.writeChar('\t');//写入数目out.writeInt(units[i]);out.writeChar('\t');//写入产品名称,行尾换行out.writeChars(name[i]);out.writeChar('\n');}out.close();//将数据读出DataInputStream in = new DataInputStream(new FileInputStream("order.txt"));double price;int unit;StringBuffer tempName;double total = 0.0;try{ // 文本读完后会抛出 EOF 异常while(true){price = in.readDouble();in.readChar();//跳过tabunit = in.readInt();in.readChar();//跳过tabchar c;tempName = new StringBuffer();while((c=in.readChar()) != '\n')tempName.append(c);System.out.println("订单信息:" + "产品名称:" + tempName+ ", \t 数量:" + unit + ", \t 价格" + price);total += unit*price;}}catch (EOFException e){System.out.println("\n 共需要:" + total + "元");}in.close();}
}

输出:

3.8 合并流

  • SequenceInputStream 类,可以实现两个文件的合并
import java.io.*;class SequenceDemo {public static void main(String[] args) throws IOException {// 两个文件输入流FileInputStream in1 = null, in2 = null;// 序列流SequenceInputStream s = null;FileOutputStream out = null;try{File inputfile1 = new File("1.txt");File inputfile2 = new File("2.txt");FileWriter wt = new FileWriter(inputfile1);wt.write("the first file.\nhaha! \n");wt.close();wt = new FileWriter(inputfile2);wt.write("the second file..");wt.close();File outputfile = new File("12.txt");in1 = new FileInputStream(inputfile1);in2 = new FileInputStream(inputfile2);s = new SequenceInputStream(in1, in2); // 合并两个输入流out = new FileOutputStream(outputfile);int c;while((c=s.read()) != -1)out.write(c);in1.close();in2.close();s.close();out.close();System.out.println("合并完成!");}catch(IOException e){e.printStackTrace();}}
}


3.9 字节流与字符流的转换

InputstreamReader 用于将一个字节流中的字节解码成字符
OutputstreamWriter 用于将写入的字符编码成字节后写入一个字节流

为了效率最高,最好不要直接用这两个类来读写,而是如下方法:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;class BufferDemo {public static void main(String[] args){BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));String str = null;while(true){System.out.println("请输入数字:");try{str = buf.readLine();}catch(IOException e){e.printStackTrace();}int i = -1;try{i = Integer.parseInt(str);i++;System.out.println("+1 后的数字为:" + i);break;}catch(Exception e){System.out.println("输入内容不是整数,请重新输入!");}}}
}

输出:

请输入数字:
abc
输入内容不是整数,请重新输入!
请输入数字:
123
+1 后的数字为:124进程已结束,退出代码为 0

3.10 IO包类层次关系




4. 字符编码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;class EncodingDemo {public static void main(String[] args){try {byte[] b = "一起来学习Java吧!".getBytes("GB2312");OutputStream out = new FileOutputStream(new File("encode.txt"));out.write(b);out.close();}catch (IOException e){System.out.println(e.getMessage());}}
}


5. 对象序列化

对象序列化,是指将对象转换成二进制数据流的一种实现手段。
通过将对象序列化,可以方便地实现对象的传输及保存。

在Java中提供有 ObjectInputStreamObjectOutputStream 这两个类用于序列化对象的操作。

ObjectInputStreamObjectOutputStream 这两个类,用于帮助开发者完成保存和读取对象成员变量取值的过程,但要求读写或存储的对象必须实现Serializable 接口,但 Serializable 接口中没有定义任何方法,仅仅被用做一种标记,以被编译器作特殊处理。

import java.io.*;class Person6 implements Serializable{ // 实现了Serializable,可序列化private String name;private int age;public Person6(String name, int age){this.name = name;this.age = age;}public String toString(){return "name: " + name + ", age: " + age;}
}public class SerializableDemo {public static void serialize(File f) throws Exception{OutputStream outputFile = new FileOutputStream(f);ObjectOutputStream cout = new ObjectOutputStream(outputFile);cout.writeObject(new Person6("Michael", 18));cout.close();}public static void deserialize(File f) throws Exception{InputStream inputFile = new FileInputStream(f);ObjectInputStream cin = new ObjectInputStream(inputFile);Person6 p = (Person6) cin.readObject();System.out.println(p);// name: Michael, age: 18}public static void main(String[] args) throws Exception{File f = new File("SerializedPersonInfo.txt");serialize(f);deserialize(f);}
}
  • 如果不希望类中属性被序列化,加入关键字 transient
private transient String name;
private transient int age;输出: name: null, age: 0

Java 文件 IO 操作相关推荐

  1. Java文件IO操作应该抛弃File拥抱Paths和Files

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream; import java.nio.file.FileSystem ...

  2. java 文件io操作_java文件IO操作。

    public static void main(String[] args) { String filename = "F:/zhangming_test/test.txt"; S ...

  3. java安全编码指南之:文件IO操作

    文章目录 简介 创建文件的时候指定合适的权限 注意检查文件操作的返回值 删除使用过后的临时文件 释放不再被使用的资源 注意Buffer的安全性 注意 Process 的标准输入输出 InputStre ...

  4. import java文件,java 文件夹操作,java文件夹,import java.

    java 文件夹操作,java文件夹,import java.import java.io.File; import java.io.FileInputStream; import java.io.F ...

  5. NIO详解以及NIO的文件IO操作

    一.NIO概述 java.nio全称java non-blockingIO,是指JDK1.4开始提供的新API.从JDK1.4开始,Java提供了一系列改进的输入/输出的新特性,被统称为NIO(即Ne ...

  6. Java文件读写操作(c站最全!一篇就够了)

    写在前面   你们好,我是小庄.很高兴能和你们一起学习Java.如果您对Java感兴趣的话可关注我的动态.   写博文是一种习惯,在这过程中能够梳理和巩固知识. Java文件读写操作 一.File类 ...

  7. python和R文件IO操作对比及dataframe创建方式对比:read_csv、to_csv、write.csv、 data.frame、pd.DataFrame

    python和R文件IO操作对比及dataframe创建方式对比 很多工程师可能刚开始的时候只熟悉python或者R其中的一个进行数据科学相关的任务. 那么如果我们对比这学习可以快速了解语言设计背后的 ...

  8. linux的文件io操作(转)

    linux文件IO操作有两套大类的操作方式:不带缓存的文件IO操作,带缓存的文件IO操作.不带缓存的属于直接调用系统调用(system call)的方式,高效完成文件输入输出.它以文件标识符(整型)作 ...

  9. Java - 文件(IO流)

    Java - 文件 (IO) 流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     & ...

最新文章

  1. Ubuntu画图工具
  2. iOS 让视图UIView 单独显示某一侧的边框线
  3. matlab导向滤波磨皮,图像处理(七)导向滤波磨皮
  4. (引)ajax 经验-保留自己使用
  5. python理论知识选择题_python基础知识练习题(一)
  6. 回溯的问题合集(Leetcode题解-Python语言)
  7. Android layout优化
  8. Adobe Flash Builder 四 序列号
  9. android 反编译 dex2jar,Android反编译教程:apktool,dex2jar的使用
  10. Redis高可用群集——主从复制+哨兵模式
  11. 【无标题】单分子纳米孔测序技术及其应用研究进展
  12. 入侵检测工具AIDE极简教程
  13. RK1109_RK1126GMAC以太网配置问题
  14. windows上配置IIS全过程
  15. MySQL数据库安装包官网下载地址
  16. 在java中jkd中文意思_Java JDK是什么意思?有什么作用?
  17. 蘑菇街java,猎聘邀请蘑菇街觉知分享Java异步化与高性能网络应用
  18. 收银管理系统多少钱一套?去哪里买比较靠谱?
  19. 解决控制台Could not resolve view with name ...
  20. 物联大世界 2019年国际物联网展会5月在北京盛大召开

热门文章

  1. Day12-date time
  2. 安装Hbase(分布式)遇到一些问题及解决方法
  3. eclipse的java帮助文档_java在Eclipse中文apidoc帮助文档导入.doc
  4. 第七季4:网络telnet调试、海思proc文件系统调试接口
  5. framebuffer驱动详解0——framebuffer介绍
  6. 1、绪论初识机器学习
  7. 1000瓶药水,1瓶有毒药,几只小白鼠能够找出毒药
  8. combobox之下拉宽度自适应
  9. Android中的IPC机制
  10. bzoj 1295: [SCOI2009]最长距离