ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java I/O流 序列化与反序列化

2022-07-18 19:35:20  阅读:132  来源: 互联网

标签:java String Java io import catch new 序列化


 使用FileInputStream读取文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        File dir = new File("/tmp/Temp/temp");
        File file = new File("/tmp/Temp/temp/test.txt");
        if (!dir.exists()) {
            dir.mkdir();//新建目录
        }
        if (!file.exists()) {
            try {
                file.createNewFile();//新建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //读取文件
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            int num = fileInputStream.read();//一次读取一个字节
            System.out.println(num);
            int contentLength;
            byte[] bytes= new byte[1024];
            String content=null;
            //超过1kb
            while ((contentLength = fileInputStream.read(bytes))!=-1){
                content=new String(bytes,0,contentLength);//转化成字符串
            }
            System.out.println(content);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 使用FileOutputStream写文件

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileWriteDemo {
    public static void main(String[] args) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("/tmp/Temp/temp/good.txt");
            String msg="weekend";
            byte[] bytes=msg.getBytes(StandardCharsets.UTF_8);
            fileOutputStream.write(bytes,0,bytes.length);//写文件
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

使用BufferedReader和FileReader读取文件

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BufferReaderDemo {
    public static void main(String[] args) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("/tmp/Temp/temp/test.txt"));
//            int read = bufferedReader.read();//再次读取有可能丢失首字母
//            System.out.println(read);
            //按行读取
//            String readLine = null;
//            while ((readLine = bufferedReader.readLine()) != null) {
//                System.out.println(readLine);
//            }
            //读取十个字符
//            char[] chars = new char[10];
//            int length = bufferedReader.read(chars, 0, chars.length);
//            System.out.println(length);
//            System.out.println(new String(chars,0,length));
            //超过十个字符
//            char[] chars = new char[10];
//            int length=0;
//            while ((length=bufferedReader.read(chars,0,chars.length))!=-1){
//                System.out.println(new String(chars,0,length));
//            }

            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

使用BufferedWriter和FileWriter写文件

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferWriterDemo {
    public static void main(String[] args) {
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/tmp/Temp/temp/new.txt"));
            bufferedWriter.write("first line");
            bufferedWriter.newLine();//换行
            bufferedWriter.write("second line");
            bufferedWriter.newLine();
            bufferedWriter.flush();//清楚空余
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

使用ObjectOutStream序列化和ObjectInputSstream反序列化

import java.io.*;

public class ObjectDemo {
    public static void main(String[] args) {
//        Student student = new Student("jack",20);
        try {
            //对象的序列化
//            FileOutputStream fileOutputStream = new FileOutputStream("/tmp/Temp/temp/student.bin");
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//
//            objectOutputStream.writeObject(student);
//            objectOutputStream.close();
//            fileOutputStream.close();

            //反序列化

            FileInputStream fileInputStream = new FileInputStream("/tmp/Temp/temp/student.bin");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Student student = (Student) objectInputStream.readObject();
            System.out.println(student);
            fileInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

 

标签:java,String,Java,io,import,catch,new,序列化
来源: https://www.cnblogs.com/linlinmailbox/p/16489452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有