JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码
一.打印流PrintWriter
- 该流提供了打印方法,可以将各种数据类型原样打印
- file对象 File
- 字符串路径 String
- 字节打印流
- 字符打印流
package com.lgl.hellojava;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class HelloJJAVA {
public static void main(String[] args) {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
PrintWriter oWriter = new PrintWriter(System.out, true);
String line = null;
while ((line = bufr.readLine()) != null) {
if (line.equals("over")) {
break;
}
oWriter.write(line);
}
oWriter.close();
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二.合并流
package com.lgl.hellojava;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class HelloJJAVA {
public static void main(String[] args) {
try {
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("1.txt"));
v.add(new FileInputStream("2.txt"));
Enumeration<FileInputStream> elements = v.elements();
SequenceInputStream sis = new SequenceInputStream(elements);
FileOutputStream fos = new FileOutputStream("3.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len = sis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三.切割文件
// 切割文件
public static void splitFile() {
try {
FileInputStream fis = new FileInputStream("1.jpg");
FileOutputStream fos = null;
byte[] buf = new byte[1024 * 1024];
int len = 0;
int count = 1;
while ((len = fis.read(buf)) != -1) {
fos = new FileOutputStream((count++) + ".patch");
fos.write(buf, 0, len);
fos.close();
}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 合并文件
public static void merge() {
ArrayList<FileInputStream> al = new ArrayList<>();
for (int i = 1; i <= 2; i++) {
try {
al.add(new FileInputStream(i + ".patch"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Iterator<FileInputStream> iterator = al.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return iterator.hasNext();
}
@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return iterator.next();
}
};
try {
SequenceInputStream seq = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len = seq.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
seq.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
四.对象的序列化Serializable
package com.lgl.hellojava;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class HelloJJAVA {
public static void main(String[] args) {
// writeObj();
readObj();
}
private static void readObj() {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"obj.txt"));
Person p = (Person) ois.readObject();
System.out.println(p);
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void writeObj() {
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("obj.txt"));
oos.writeObject(new Person("zhangsan", 20));
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Person implements Serializable {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
五.管道流
package com.lgl.hellojava;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class HelloJJAVA {
public static void main(String[] args) {
PipedInputStream pi = new PipedInputStream();
PipedOutputStream po = new PipedOutputStream();
try {
pi.connect(po);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read read = new Read(pi);
Write write = new Write(po);
new Thread(read).start();
new Thread(write).start();
}
}
class Read implements Runnable {
private PipedInputStream in;
public Read(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
try {
byte[] buf = new byte[1024];
int len = in.read(buf);
String s = new String(buf, 0, len);
System.out.println(s);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Write implements Runnable {
private PipedOutputStream out;
public Write(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
out.write("yes".getBytes());
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
六.RandomAccessFile
- 该类不是IO体系中的子类
- 但是他是IO包中的成员,他同时具备读写功能
- 内部封装了一个数组,而且通过指针对数组中的元素进行操作
- 可以通过getFilePointer或者指针位置同时可以通过seek改变指针的位置
package com.lgl.hellojava;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class HelloJJAVA {
public static void main(String[] args) {
writeFile();
}
public static void writeFile() {
try {
RandomAccessFile raf = new RandomAccessFile("1.txt", "rw");
raf.write("zhangssan".getBytes());
raf.writeInt(99);
raf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void ReadFile() {
try {
RandomAccessFile raf = new RandomAccessFile("1.txt", "r");
// 调整对象指针
raf.seek(8 * 0);
byte[] buf = new byte[1024];
raf.read(buf);
String s = new String(buf);
System.out.println(s);
raf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
七.IO其他类
- 操作基本数据类型
- 字节数组
- 字符数组
1.基本数据类型
public static void readData() {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(
"data.txt"));
int num = dis.readInt();
boolean d = dis.readBoolean();
System.out.println(num + "" + d + "");
dis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 基本数据类型
public static void writeDatea() {
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
"data.txt"));
dos.writeInt(55);
dos.writeBoolean(true);
dos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
二.字节数组
ByteArrayInputStream: 在构造的时候,需要接收数据源,而且数据源是一个字节数组
ByteArrayOutputStream:在构造的时候,不用定义数据的目的,因为该对象中已经内部封装了可变程度的字节数组
package com.lgl.hellojava;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class HelloJJAVA {
public static void main(String[] args) {
// 数据源
ByteArrayInputStream bis = new ByteArrayInputStream("123".getBytes());
// 数据目的
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = bis.read()) != -1) {
bos.write(ch);
}
System.out.println(bos.size());
System.out.println(bos.toString());
}
}
八.字符编码
String s = "hello java";
byte [] b1 = s.getBytes();
System.out.println(Arrays.toString(b1));
byte [] b1 = s.getBytes("GBK");
九.练习
- 有五个学生,每个学生有三门课程,从键盘输入以下数据(包括姓名,三门课成绩),输入的格式,如zhangsan,30,60,96计算出总成绩,并把学生的信息和计算出的总分数高低顺序存入文件student.txt中去;
package com.lgl.hellojava;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 逻辑:
* 1.通过获取键盘录入一行数据,然后取出数据封装成学生对象
* 2.需要从高到低排列,需要排序,使用集合TreeSet就可以了
* 3.写入文件
*/
try {
Set<Student> stus = StudentInfoTool.getStudents();
StudentInfoTool.writeFile(stus);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 比较数据,Comparable接口
class Student implements Comparable<Student> {
// 姓名
private String name;
// 三科分数
private int n1, n2, n3;
// 总分
private int sum;
// 构造函数
public Student(String name, int n1, int n2, int n3) {
this.name = name;
this.n1 = n1;
this.n2 = n2;
this.n3 = n3;
sum = n1 + n2 + n3;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public int compareTo(Student o) {
int num = new Integer(this.sum).compareTo(new Integer(o.sum));
if (num == 0) {
return this.name.compareTo(o.name);
}
return num;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return name.hashCode() + sum * 78;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Student)) {
throw new ClassCastException("Type Error");
}
Student s = (Student) obj;
return this.name.equals(s.name) && this.sum == s.sum;
}
}
// 工具类
class StudentInfoTool {
public static Set<Student> getStudents() throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
// 集合
Set<Student> stus = new TreeSet<>();
while ((line = bufr.readLine()) != null) {
if (line.equals("over")) {
break;
}
String[] info = line.split(",");
Student student = new Student(info[0], Integer.parseInt(info[1]),
Integer.parseInt(info[2]), Integer.parseInt(info[3]));
stus.add(student);
}
bufr.close();
return stus;
}
// 写入文件
public static void writeFile(Set<Student> stus) {
try {
BufferedWriter bufw = new BufferedWriter(new FileWriter(
"student.txt"));
for (Student stu : stus) {
bufw.write(stu.getName());
bufw.write(stu.getSum() + "");
bufw.newLine();
bufw.flush();
}
bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}