文章目录
- 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());
}
}
输出:
代码语言:javascript复制getName()获取文件名:file.txt
getPath()获取文件路径:file.txt
getAbsolutePath()绝对路径:D:gitcodejava_learningfile.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();
}
}
输出:
代码语言:javascript复制第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 字节流
代码语言:javascript复制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 字符流
代码语言:javascript复制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 管道流
- 主要用于连接两个线程间的通信
PipedInputStream
、PipedOutputStream
、PipedReader
、PipedWriter
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
代码语言:javascript复制class SystemPrintDemo{
public static void main(String[] args){
PrintWriter out = new PrintWriter(System.out);
out.print("hello Michael");
out.println("hello Michael");
out.close();
}
}
输出:
代码语言:javascript复制hello Michaelhello Michael
进程已结束,退出代码为 0
代码语言:javascript复制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
代码语言:javascript复制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();//跳过tab
unit = in.readInt();
in.readChar();//跳过tab
char 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
用于将写入的字符编码成字节后写入一个字节流
为了效率最高,最好不要直接用这两个类来读写,而是如下方法:
代码语言:javascript复制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("输入内容不是整数,请重新输入!");
}
}
}
}
输出:
代码语言:javascript复制请输入数字:
abc
输入内容不是整数,请重新输入!
请输入数字:
123
1 后的数字为:124
进程已结束,退出代码为 0
3.10 IO包类层次关系
4. 字符编码
代码语言:javascript复制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中提供有 ObjectInputStream
与 ObjectOutputStream
这两个类用于序列化对象的操作。
ObjectInputStream
与 ObjectOutputStream
这两个类,用于帮助开发者完成保存和读取对象成员变量取值的过程,但要求读写或存储的对象必须实现了 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