字符流
字符流的读,第一种
public static String read(String path) throws IOException {
FileReader fr = new FileReader(path);
int temp = 0;
StringBuilder sb = new StringBuilder();
while (-1 != (temp = fr.read())) {
sb.append((char) temp);
}
fr.close();
return sb.toString();
第二种
char[] c = new char[(int) new File(path).length()];
fr.read(c);
int i = 0;
for (i = 0; i < c.length; i++) {
if (c[i]=='\u0000'){
break;
}
}
fr.close();
return new String(c,0,i);
写
public static void write(String path,String str,boolean isAppend)throws IOException{
FileWriter fw = new FileWriter(path,isAppend);
char[] c = new char[(int) new File(path).length()];
fw.write(c);
fw.close();
}
public static void main(String[] args) {
try {
String s = read("a.txt");
System.out.println(s);
write("a.txt",s,true);
} catch (IOException e) {
e.printStackTrace();
}
}
Buffered类的读写
public static String readBuffer(String path) throws IOException {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String temp;
StringBuffer sb = new StringBuffer();
while (null!=(temp=br.readLine())){
sb.append(temp+"\n");
}
//后开先关
br.close();
fr.close();
return sb.toString();
}
public static void writeBuffer(String path,String str,boolean isAppend) throws IOException{
FileWriter fw = new FileWriter(path);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str);
bw.close();
fw.close();
}
public static void main(String[] args) {
try {
String s = readBuffer("a.txt");
System.out.println(s);
writeBuffer("a.txt",s,true);
}catch (IOException e){
e.printStackTrace();
}
}
转换流
public static String readCharset(String path) throws IOException {
//先用文本接收在转换
FileInputStream fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
String temp;
StringBuffer sb = new StringBuffer();
while (null!=(temp = br.readLine())){
sb.append(temp);
}
br.close();
isr.close();
fis.close();
return sb.toString();
}
public static void writeCharset(String path,String str,boolean isAppend,String charsetName) throws IOException{
FileOutputStream fos = new FileOutputStream(path,isAppend);
OutputStreamWriter osw = new OutputStreamWriter(fos,charsetName);
osw.write(str);
osw.close();
fos.close();
}
public static void main(String[] args) {
try {
String s = readCharset("E:\\ideaproject\\ideacode\\a.txt");
System.out.println(s);
writeCharset("a.txt",s,false,"GB2312");
}catch (IOException e){
e.printStackTrace();
}
}
读写二进制文件
DataInputStream类
与FileInputStream类结合使用读取二进制文件
public static void copyDataFile(String inPath,String outPath) throws IOException {
FileInputStream fis = new FileInputStream(inPath);
DataInputStream dis = new DataInputStream(fis);
byte[] b = new byte[dis.available()];
dis.read(b);
System.out.println(new String(b));
DataOutputStream类
与FileOutputStream类结合使用写二进制文件
FileOutputStream fos = new FileOutputStream(outPath);
DataOutputStream dos = new DataOutputStream(fos);
dos.write(b);
dos.close();
fos.close();
dis.close();
fis.close();
public static void main(String[] args) throws IOException{
// copyDataFile();
}
序列化和反序列化(对象流)
Student类实现可序列化接口Serializable
//可序列化接口
public class Student implements Serializable {
private int id;
private String name;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student() {
}
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
public static void writeObject(String path,boolean isAppend,Object obj) throws IOException {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.close();
}
public static Object read(String path) throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
ois.close();
return o;
}
public static void main(String[] args) {
Student s = new Student(1,"张三",98.5);
try {
writeObject("obj.txt",true,s);
Object o = read("obj.txt");
if (o instanceof Student){
Student stu = (Student) o;
System.out.println(o);
}
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}